Howdy, Stranger!

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

SugarCube 2 How to use arrays?

edited May 2017 in Help! with 2.0
I have this:
StoryInit
<<set $LOW_SKILL = 1>>
<<set $MID_SKILL = 2>>
<<set $HIGH_SKILL = 3>>
<<set $SALARY[$LOW_SKILL] = 100>>
<<set $SALARY[$MID_SKILL] = 200>>
<<set $SALARY[$HIGH_SKILL] = 300>>
Somewhere in passages:
<<set $skill = $LOW_SKILL>> (or $MID_SKILL or $HIGH_SKILL)
<<set $money += $SALARY[$skill]>>
I have error: <<set>> bad evaluation. Cannot set property "1" of undefined.
In documentation note like $obj[$index] is something about members. But I need only numeric arrays for my constants.
What need I do?

Comments

  • edited May 2017
    You need to state the name and full version number of the Story Format you are using, as answers can be different for each one.

    You need to declare that Javascript Array before you can use it, and unless you add all it's elements at the time of declaration you will also need to the push() method to add new elements. You can access (and change) any existing element of the array using the syntax in your example.

    WARNINGS:
    a. Array index's are zero base, so the first element is 0 not 1.
    b. Story variable don't automatically default to zero, so you need to assign zero to a variable before adding a number to it's current value.

    eg. Creating an $salary array with two elements, then adding a third element and changing the current value of the second element.
    <<set $LOW_SKILL to 0>>
    <<set $MID_SKILL to 1>>
    <<set $HIGH_SKILL to 2>>
    <<set $money to 0>>
    
    <<set $salary to [100,0]>>
    <<set $arrayWithNoElements to []>>
    
    current value: $salary
    
    <<set $salary.push(300)>>
    <<set $salary[$MID_SKILL] to 200>>
    
    new value: $salary
    
    <<set $skill = $LOW_SKILL>>
    <<set $money += $salary[$skill]>>
    
    values: skill: $skill money: $money
    
    NOTE: I used upper case variable names for those who's value is consistent, and changed the other to mixed case with the first letter being lower case. eg. $someLongName.
  • You need to initialize the array (setting it to empty brackets '[]' should do fine).
    ::StoryInit
    <<set $money to 0>>
    <<set $SALARY to []>>
    <<set $LOW_SKILL = 1>>
    <<set $MID_SKILL = 2>>
    <<set $HIGH_SKILL = 3>>
    <<set $SALARY[$LOW_SKILL] = 100>>
    <<set $SALARY[$MID_SKILL] = 200>>
    <<set $SALARY[$HIGH_SKILL] = 300>>
    
    ::Somewhere in passages:
    <<set $skill = $LOW_SKILL>> (or $MID_SKILL or $HIGH_SKILL)
    <<set $money += $SALARY[$skill]>>
    Money: <<print $money>>
    

    Note that arrays are 0 based, so setting your first value to index 1 means that certain methods may work oddly.
  • You're attempting to use $SALARY as an array before having initialized it as an array, thus the complaint about it being undefined. Put something like the following before any <<set>> macro invocations which reference $SALARY in your StoryInit special passage:
    <<set $SALARY to []>> /* Initialize $SALARY as an empty array. */
    


    Additionally. Arrays are 0-based, not 1-based. Meaning, unless you're using it for something not shown in your example, you're wasting the first element in the array by defining $LOW_SKILL as 1 instead of 0.
  • Thank you very much. I understood that I needed to define array but didn't know sintax.
    $SALARY to [] - exactly what I need. Maybe it's good example of usege for this page http://www.motoslave.net/sugarcube/2/docs/markup.html#naked-variables
Sign In or Register to comment.