Is there a better way? It looks a little spaghetti and confusing though it works. It does however prevent global variable use and make logical class partitions.
I need some creature objects to know about each other, so creatureS are passed down to each creature.
"Man, you're like school during the summer. No class." -Rudy (Fat Albert)
If I understand right, I do this all the time. Often I have a “collection” class which consists of a table of “things”, then each “thing” knows which “collection” it is in as it might have to pass information back up (for example, if the “thing” “dies” it’s easier for the “thing” object to inform the “collection” than for the collection to repeatedly poll its “things” as to whether or not they are still alive). So many of my classes have “:addThing” methods along the following lines:
function Parent:addChild(...)
local t = Child(...)
t.parent = self
table.insert(self.children,t)
end
and then the child class might have:
function Child:breakGlass()
self.introuble = true
self.parent.angry = true
end
function Lim:move(f,s,l)
....
self:eat(f.t[k],l,s)
function Lim:eat(f,l,s)
--(food, lims, scale)
f:rand(s)
local d = l:oldest()
self:divide(d)
It’s this “oldest” function that is from the Lims class that is passed down as the letter l (it’s like the zoo class).
I want to replace the oldest with a newest. So, it’s like addchild.
The child “moves” and if the move results in "eat"ing it adds a child (using to the result of oldest).
Do you think the right way to go is to have the parent/zoo call the child’s move that would return a result that says “eat/oldest” then the parent executes eat/oldest?