Lua class instance with nested tables
Posted
by Anonnobody
on Stack Overflow
See other posts from Stack Overflow
or by Anonnobody
Published on 2010-05-30T05:07:02Z
Indexed on
2010/05/30
5:12 UTC
Read the original article
Hit count: 298
Hello,
Simple lua game with simple class like so:
creature = class({ name = "MONSTER BADDY!",
stats = { power = 10, agility = 10, endurance = 10, filters = {} },
other_things = ... })
creatureA = creature.new()
creatureB = creature.new()
creatureA.name = "Frank"
creatureB.name = "Zappa"
creatureA.stats.agility = 20
creatureB.stats.power = 12
-- blah blah blah
Non table values are individual to each instance, but table values are shared among all instances and if I modify a stats.X value in one instance, all other instances see the same stats table.
Q1: Is my OO implementation flawed? I tried LOOP and the same result occured, is there a fundamental flaw in my logic?
Q2: How would you have each instance of creature have it's own stats table (and sub tables)?
PS. I cannot flatten my class table as it's a bit more complicated than the example and other parts of the code are simplified with this nested table implementation.
Thanks a bunch.
© Stack Overflow or respective owner