Page 1 of 1

Enemy table vs inheritence

Posted: Thu Jun 11, 2015 4:42 pm
by dandypants
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!

Re: Enemy table vs inheritence

Posted: Thu Jun 11, 2015 4:47 pm
by Robin
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).

Re: Enemy table vs inheritence

Posted: Thu Jun 11, 2015 5:25 pm
by dandypants
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

Posted: Fri Jun 12, 2015 1:24 am
by s-ol
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.
there are no classes or inheritance in Lua. What Robin outlined is close to those concepts already, and most likely the way to go.

Re: Enemy table vs inheritence

Posted: Wed Jun 17, 2015 5:51 pm
by T-Bone
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:

Code: Select all


function inherit(parent, child)
    setmetatable(child, {__index = parent})
end

(not tested and typed from a phone)

Re: Enemy table vs inheritence

Posted: Thu Jun 18, 2015 7:21 am
by Nixola
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:

Code: Select all


function inherit(parent, child)
    setmetatable(child, {__index = parent})
end

(not tested and typed from a phone)
That's pretty much my class approach in every project.