Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Assigning values to an object from a list of random objects in sugarcube

edited November 2016 in Help! with 2.0
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

  • edited November 2016
    Please use the code tag when posting code—it's C on the editor bar. You should also specify the specific version of SugarCube.

    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:
    /* Assuming that $commanders is an array of commander objects. */
    <<set _commander to $commanders.pluck()>>
    <<set $location1 to {
    	attack  : _commander.attack,
    	defence : _commander.defence
    }>>
    
    And to later access the attack value:
    $location1.attack
    


    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:
    /* Assuming that $commanders is an array of commander objects. */
    <<set $location1 to { commander1 : $commanders.pluck() }>>
    
    And to later access the attack value:
    $location1.commander1.attack
    
  • edited November 2016
    Thanks a lot for your reply!

    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
    <<set $Commander =["$Goblin", "$Elf", "$Human"]>>
    <<set $Goblin ={Attack: 10, Defence: 5, Troops: 900}>>
    <<set $Elf ={Attack: 5, Defence: 10, Troops: 900}>>
    <<set $Human ={Attack: 15, Defence: 15, Troops: 900}>>
    <<set _Commander to Array.random($Commander)>>
    <<print _Commander>>
    
    <<set $CommanderAlpha to { 
    Attack: _Commander.Attack,
    Defence: _Commander.Defence, 
    Troops: _Commander.Troops}>>
    <<print $CommanderAlpha.Defence>>
    

    Thanks a lot in advance again
  • You need to define your $Goblin, $Elf, and $Human objects before you add then as elements to your $Commander array.

    Try changing the example to something like the following:
    <<set $Goblin to {Attack: 10, Defence: 5, Troops: 900}>>
    <<set $Elf to {Attack: 5, Defence: 10, Troops: 900}>>
    <<set $Human to {Attack: 15, Defence: 15, Troops: 900}>>
    
    <<set $Commander to [$Goblin, $Elf, $Human]>>
    
    <<set _Commander to Array.random($Commander)>>
    
    <<set $CommanderAlpha to { 
    	Attack: _Commander.Attack,
    	Defence: _Commander.Defence, 
    	Troops: _Commander.Troops}>>
    <<print $CommanderAlpha.Defence>>
    

    note: It is also a good idea to be consistent in which operator = or to you use for assignment.
  • edited November 2016
    Thanks a lot for the help.

    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!
  • What version of SugarCube are you using? Temporary variables were added in v2.3.0.
  • What version of SugarCube are you using? Temporary variables were added in v2.3.0.
    I'm using 2.6.2. I apologise for not mentioning that earlier, I didn't know there were such big differences between versions.
  • edited November 2016
    Then you've got something else going on because the temporary variable should have worked fine. Are you splitting the code you showed between passages or something?
  • Then you've got something else going on because the temporary variable should have worked fine. Are you splitting the code you showed between passages or something?
    I had the second part in a new passage yes, as for my story I need to be able to make a call to the
    Array.random($Commander)
    
    bit multiple times through out the story.

    The easiest way I found now was to use.
    <<set $CommanderTest to clone(Array.random(CommanderList, 327))>>
    

    Entirely removing the need to set a variable in between.
Sign In or Register to comment.