I'm making a simple game and I'll have a few types of enemies with slightly different properties (amount of health, damage, etc). How are people handling this? I can think of two options: having a number of tables like enemyHealth, enemyDamage etc that store these values or to go with classes and inheritance.
I've been leaning towards using tables to keep it simple but am all ears for better approaches.
Thanks!
Enemy table vs inheritence
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Enemy table vs inheritence
If you use tables, I'd use it in the other direction: instead of having a bunch of tables, I'd have a table enemies, containing tables of the form {health = ..., damage = ..., ...}. Otherwise it tends to get unwieldy if the enemies need to be reordered (e.g. because of enemies being deleted).
Help us help you: attach a .love.
-
- Prole
- Posts: 2
- Joined: Thu Jun 11, 2015 4:38 pm
Re: Enemy table vs inheritence
Thanks - that makes a lot of sense and keeps it much more organized. I'm leaning towards this sense my game is so simple and I think I can get away without using classes etc.
Re: Enemy table vs inheritence
there are no classes or inheritance in Lua. What Robin outlined is close to those concepts already, and most likely the way to go.dandypants wrote:Thanks - that makes a lot of sense and keeps it much more organized. I'm leaning towards this sense my game is so simple and I think I can get away without using classes etc.
Re: Enemy table vs inheritence
You could make something similar to prototype based OO, where you have a table describing every enemy type, and then for each enemy you reference a single enemy type, which you can then use to look up info about the enemy.
Also, about inheritance, you can add inheritance with three lines of code if you want to, like this:
(not tested and typed from a phone)
Also, about inheritance, you can add inheritance with three lines of code if you want to, like this:
Code: Select all
function inherit(parent, child)
setmetatable(child, {__index = parent})
end
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
Re: Enemy table vs inheritence
That's pretty much my class approach in every project.T-Bone wrote:You could make something similar to prototype based OO, where you have a table describing every enemy type, and then for each enemy you reference a single enemy type, which you can then use to look up info about the enemy.
Also, about inheritance, you can add inheritance with three lines of code if you want to, like this:
(not tested and typed from a phone)Code: Select all
function inherit(parent, child) setmetatable(child, {__index = parent}) end
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 1 guest