Quantcast
Channel: Help! with 2.0 - Twine Forum
Viewing all articles
Browse latest Browse all 1844

Using variables and function return values as the index of an array.

$
0
0
I just started using Twine a few days ago, but have apparently stumbled upon some unanswered weirdness (as far as I can see). So I've been trying a few things and I've noticed it's seems unnecessarily difficult to use a variable as the index in an array.

I'm using Twine 2.0's Sugarcube (the standalone program), for reference.


First the normal "variable as array index" setup::
	<<set $holder = 0>>
	<<set $Weekdays =["Aday", "Baday", "Seeday"]>>
        $Weekdays[$holder]

This prints (as expected):
Aday


Then the "variable as array index in nested arrays" setup:
	<<set $holder = 0>>
        <<set $StatsA =[]>>
	<<set $PCArrayA =["PC", "Archer", "bow and arrow", "leather", "7", "royalblue"]>>
        <<set $Stats[0] = $PCArrayA>>
        $StatsA[$holder][0]
        $StatsA[0][$holder]
(I know the 7 ends up as a string and this may not be the most efficient way of holding such information but play along.)

This prints:
$StatsA[$holder][0]
PC

Alright, for some reason the first index of a pseudo multidimensional array can't take a variable, for whatever reason. So the first question: Is there a way to force Twine to accept $holder as a variable instead of (I guess) part of just as some weird text with brackets? I tried quotes but that doesn't help (and doesn't seem like it should anyway, because then the index is literally $holder instead of the number in $holder).


Secondly, I figured I'd also ask if it's possible to make classes? Or if I need to learn more Javascript and/or Twine+Javascript interaction to do something like the code below. I'd appreciate some links on how to actually make and use these if they exist.

(syntax is basically made up, since I couldn't find anything about this searching for classes, functions or methods in twine that were coder/author-defined):
<<set $time={
	year: 0,
        month: 0,
        day: 0,
	weekday() {return this.day % 3} 
>>
<<set $time.day += 1>>
<<set $Weekdays =["Aday", "Baday", "Seeday"]>> 
Today is $Weekdays[$time.weekday()]


To essentially return:
Today is Baday.


Thanks for any help after slogging through that wall of text.


Viewing all articles
Browse latest Browse all 1844

Trending Articles