This issue has been driving me crazy for several levels, because I want to do the following:
- Summon a unit
- Add new unit to an existing array at the end of the array
- Provide instruction to array of units, based upon their (permanent) position in the array
At first, I thought I was messing up the array or somehow not using .push properly, but that definitely isn’t it. I managed to isolate at least part of the issue
var soldiers = [];
var friend = hero.summon("soldier");
hero.command(friend, "move", {x: friend.pos.x + 10, y: friend.pos.y +10});
soldiers.push(friend);
I included the “hero.command… move” order as a test, and it breaks right there, saying that the hero has nothing to command, or that the friend.pos is undefined. In other words, the variable I set to equal the brand new summon doesn’t capture the new unit.
It is no good to just summon a unit and then create a new array each time of all similar units. Not only is that profoundly inefficient, it changes the index place assigned to a given unit, which means that their individual instructions keep changing based upon their place in the index.
Using ‘findNearestFriend’ sometimes returns my pet, or an alternate unit, also no good.
So how do I capture new summons as a variable directly? Is this intended behavior (and if so, why?!)?
To save time, the code is a snippet. There are 100% no problems ensuring enough money to summon, that the arrays are set up correctly, that all the variables are called, etc. It took me many days to isolate the problem to the variable not actually grabbing the unit object, so trust me when I say I tried many other things and worked around them first. It never occurred to me that a variable wouldn’t actually accept the input of a method, until I ran this last test.