Howdy, Stranger!

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

Very basic problem with arrays

I decided to try to learn how arrays work and I seem to have done something wrong. I'm trying to get an array to return the text 1,2,3,4

So far I have:

Start

<<set $test = "answer">>

<<print $test>>

<<set $inventory = [1,2] >>

<<print $inventory>>

page2

and:

page2

<<set $inventory.push (3)>>

<<set $inventory.push (4)>>

Start

but it only prints 1,2 even after visiting page 2. If anyone can tell me what I'm doing wrong I would be very thankful!

Comments

  • Should mention I'm using Sugarcube 1.0.32
  • You're assigning an array to $inventory on the Start passage. So, if you're using the Start link on page2 to go to the starting passage again, then you're resetting the value of $inventory to a new array (value: [1, 2]) as soon as you get there. Try moving your initialization to the StoryInit special passage. For example:

    StoryInit
    <<set $test to "answer">>
    <<set $inventory to [1, 2]>>
    
    Start
    $$test: $test
    
    $$inventory: $inventory
    
    [[page2]]
    
    page2
    <<set $inventory.push(3)>>\
    <<set $inventory.push(4)>>\
    [[Start]]
    
  • Haha I had the feeling it was something simple! Thank you so much for letting me know what I was doing wrong!
Sign In or Register to comment.