Enemy table vs inheritence

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
dandypants
Prole
Posts: 2
Joined: Thu Jun 11, 2015 4:38 pm

Enemy table vs inheritence

Post 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!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Enemy table vs inheritence

Post 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).
Help us help you: attach a .love.
dandypants
Prole
Posts: 2
Joined: Thu Jun 11, 2015 4:38 pm

Re: Enemy table vs inheritence

Post 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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Enemy table vs inheritence

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Enemy table vs inheritence

Post 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)
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Enemy table vs inheritence

Post 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.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 0 guests