Howdy, Stranger!

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

Splice in Harlowe

edited September 2015 in Help! with 2.0
Hi,

Still pretty new to all this. I haven't been able to find much guidance about how to splice an array in Harlowe. It would be useful to be able to change the value of an element in the middle of an array. E.g. I'm trying to do something like this:

Start

(set:$arraytest to (a:0,0,0,0,0,0,0,0,0))
(goto:"Splice Test")

Splice Test

{(set:$position to (random:0,8))
(set:$newvalue to (random:1,9))
}
Current array: $arraytest

(Link:"Change the element at position $position to $newvalue")[(set:$arraytest to (a:$arraytest.splice($position,1,$newvalue))]
Splice Test

... but of course it doesn't work and I've been reduced to just guessing and changing things randomly ^_^

Thank you!

EDIT:
Okay, I got more or less the effect I was looking for with ...
[(set:$array's $position to $newvalue)]
... although if anyone out there wants to explain splice in Twine for a newbie I'd still be grateful ^_^

Comments

  • edited September 2015
    splice is a javascript function, so you can the documentation for it in the Mozilla Developer Network: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

    According to those docs the splice function returns an array containing any deleted elements. so
    (set:$arraytest to (a:$arraytest.splice($position,1,$newvalue)))
    

    Will set $arraytest to an array holding an array holding whatever is at location $position. BTW there was a bracket missing from your code.

    To just change $arraytest you would need to use
    (set:$dummy to $arraytest.splice($position,1,$newvalue))
    
Sign In or Register to comment.