Hello, I spent maybe 4 hours on different approaches to solve this, but unsuccesfuly. So I am kindly asking for help…
How do I…
I have two players summoning monsters into the scene. They are picking monsters of same kind (goblin, skeleton…) and sending them to fight with each other.
When players summons monster, I store id of player (owner) to monsters instance variable.
What is the expected result
I need to run logic, where every monster of one player will check distance to enemies monsters (by attack range), then pick the nearest one and perform actions towards it (stops and starts attacking).
I expect this logick to run for every player involved.
I don’t want to create duplicated monster objects (like goblin1, goblin2, skeleton1, skeleton2…), becouse they will carry many attributes, events and logic.
What is the actual result
Actually I don’t know how to iterate through all monsters by conditioning it with instance variables.
Related screenshots
Note: I have intentionally wrote into the code conditions as I would expect to work conditioning of monsters group by players (monster owners) id.
Something like this may work - it’s for monsters for player 0, but you can repeat it and replace “variable player of monster = 0” with “variable player of monster = 1” and vice versa:
You might need 3 for each object events. Go thru players, go thru player monster, go thru non-player monsters.
For simplicity and clarity I’m going to use ID instead of player variable. Dashes used for sub event in detention.
For each player
–ID of monster = ID of player
–for each monster
-----set X, Y to the monster X() and Y()
------pick all monsters
------ID of monster ≠ player.ID
------for each monster
---------if monster distance X, Y do something
I missed a few steps but it’s close.
It would make life easier if each player had their own monster object. It’s easier to compare objects instead of the same instances. With different objects you could link attacking monsters together. Maybe 1 monster object with different animations per player.
It would make life easier if each player had their own monster object. It’s easier to compare objects instead of the same instances.
I undestand this, but this is exactly a solution that I am trying to avoid. Imagine that every player has accesible same monsters - they are not specific or unique per player. But every monster has many specifics (attributes, variables, settings, events, logic behind…) and you will have to copy all of it for every monster type. This would make maintainability of code very difficult.
Maybe 1 monster object with different animations per player.
Could be a solution for grouping all objects into one, but I don’t see how this would help me to avoid code duplicities (in previous suggestion) or solution with instance to instance conditioned events and comparison.
I believe that solution for this siituation would help more people, that would like to create things avoiding duplicating objects and code.