Assigning a property to an instanced object

Ok, so I have this map where I have several farmers and farms. Each farmer is an instantiation of the farmer node and every farm is an instantiation of the farm node. I have stored all farmers in an array and all farms in a different array.

With every delta, the game should check for idle farmers and empty farms. If there is an idle farmer and an empty farm, the farmer should go to the farm and stay there.

I figure I need to have a bool variable for each farmer and each farm that lets the game identify the object’s condition (idle/busy, empty/full). Then I would cycle through both arrays in order, to match idle farmers to empty farms. That’s easy enough. The only way I can think to do this is to make both arrays bidimensional, so that I can store that property for each object. However, this seems clunky and would get messy if a farm is destroyed or the peasant is killed.

So the question is, is there an easy way to assign a boolean property to each element in the arrays? i.e. Can I somehow add custom properties to my nodes?

As always, thanks very much!

Would your farmer and farm nodes not have a state variable:

var state: String = "idle"

Then your arrays, which I am guessing hold node references, would just check that variable.

for farmer in all_farmers:
    if farmer.state == "idle"
         # deal with idle farmer

If your arrays are holding node references, you can access all their variables. Or use a setter or getter. So use farmer.get_state() rather than accessing the variable directly.

I have no idea if this helps in any way. I would prefer to use a dictionary to store your farmer and farm node refs, rather than an array.

If I have missed something crucial in your question and missed the point entirely, sorry in advance.

Best wishes,

Paul.

1 Like

Thanks very much for your answer. I have never really used state variables, but that might work. I’ll give it a try.

I am also reading up on metadata right now, which seems kind of equivalent to your solution.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.