Howdy, Stranger!

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

Check value in two dimensional array in Harlowe.

Hello, I'm new here.
How can I check value in two dimensional array like: (this obviously don't work for me)

(if: $levelMap[$posX][$posY+1] is > 0)Go South

The $levelMap is 10 : 10 array:
(set: $levelMap to (a:
[0,0,0,0,0,0,0,0,0,0],
[0,1,1,1,1,1,1,1,1,0],
[0,1,0,0,0,0,0,0,1,0],
[0,1,0,1,1,1,1,0,1,0],
[0,1,0,1,1,1,1,1,1,0],
[0,1,0,0,0,0,0,0,1,0],
[0,1,1,1,1,1,1,1,1,0],
[0,1,0,0,0,0,0,1,1,0],
[0,1,1,1,1,1,1,1,1,0],
[0,0,0,0,0,0,0,0,0,0]))

Comments

  • 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. I will assume you are using Harlowe v1.2.3 as it is the default story format in Twine v2.1.1

    The third paragraph of Harlowe's Array Data documentation explains you can use brackets (parentheses) to access element using expressions.
    (set: $array to (a:
    	(a: 1,2,3,4,5,6,7,8,9,0),
    	(a: 11,12,13,14,15,16,17,18,19,20)
    )) \
    array: (print: $array)
    
    element 1,1: \
    (set: $X to 1, $Y to 1)\
    (print: $array's ($X)'s ($Y))
    
    element 2,3: \
    (set: $X to 2, $Y to 3)\
    (print: $array's ($X)'s ($Y))
    
    element 2,last: \
    (print: $array's ($X)'s last)
    
    (if: $array's ($X)'s ($Y) >= 10)[ [[Go South->Cave Entrance]]]
    

    notes:
    a. some care needs to be taken when mixing Javascript syntax with Harlowe's because they are not fully compatible languages. eg. you are mixing Javascript array declaration with Harlowe's

    b. You shouldn't mix the is operator (which is equivalent to Javascript's === operator) with the greater-than > or less-than < operators, the Boolean Data documentation lists the correct operators to use.

    c. Your (if:) macro example is missing it's associated hook.
  • Thank you for your suggestions! It was really helpful :)
Sign In or Register to comment.