Hi,
For my game I need to be able to assign commanders with pre determined stats to a specific location at random.
What I mean by this is: I have a commander spot (Commander1) which has to be filled from the StoryInit. This is an object with several keys, which are similar between all commanders but with different values.
Instead of assigning the same commander with the same stats to this at every game, I instead have a list of different commanders, each with their own stats.
What I now want is that I'm able to randomly pick a commander from that list (of about 150 commander types), then assign that commander with it's stats to the spot Commander1.
The only way I can think of would require about 150 if statements per spot, so with over 190 spots that would become a very long and slow process.
Is there a more efficient way to do this?
Another way (which I tried but didn't work) would use a sort of variable in a variable. Like this:
<<set $random to random(10)>>
<<set $object1 ={Attack: $commander[$random].Attack, Defence: $commander[$random].Defence}>>
Thanks in advance
Comments
If the commanders are supposed to not be shared between locations, then you can use <Array>.pluck().
If, as in your example code, the values of the chosen commander object's attack and defence properties should be assigned to the location's own versions of those properties, then you may do so with something like the following: And to later access the attack value:
On the other hand, if you want to assign the chosen commander object itself to the location, then you'd probably want to do something like the following instead: And to later access the attack value:
I tried using what you said, but using Array.random() instead of pluck, as I want one commander type to be assigned to multiple spots.
Everything worked fine up to the point where it attempts to assign the values to the new commander. It then gives an "Error: <<set>>: bad evaluation: Cannot read property 'Defence' of undefined".
My code looks like this
Thanks a lot in advance again
Try changing the example to something like the following:
note: It is also a good idea to be consistent in which operator = or to you use for assignment.
I just copied exactly what you posted, but somehow I still get the same error. I literally have no clue where it's going wrong anymore.
Edit: I just changed all _Commander to $COD and suddenly it's working, even though _COD was not working. So I have no clue what was going wrong but it's working now. Thanks a lot to both of you for your help!
The easiest way I found now was to use.
Entirely removing the need to set a variable in between.