Howdy, Stranger!

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

Recalling ARRAYS

First Off I will be honest, I don't know much about what I am doing.
So I will give you what may be important information first. I am using "Twine 2.1.0 & Sugarcube 2.12.1."
Now off to the question.
I am looking to use arrays or recall their information multiple times, for example, the array:
<<set $Random to either("Koolaid","Pie","Can food")>> and then use the array to give a specific variable an identity <<set $Item1 to "$Random">> and then use that same array again but to be set with another variable <<set $Item2 to "$Random">>, This worked if I use the array once again in the middle of the passage, however, the problem came up when displaying the "Item1 and Item2" variable again in another passage.

I am not sure if I am doing this right, so feel free to recommend an alternative for what I am trying to do, the concept is that once a player clicks on a link the variables would change each time he clicks the same link again, becoming something different each time.

Comments

  • <<set $Quirks = [$Agile,$Strong,$Smart,]>>

    <<set $PlyrTrait to Array.random($Quirks)>>

    <<print $PlyrTrait>>

    Did some fiddling around and came up with this, still doesn't work but at least it is an actual array. (I think before was just a basic either function) I need to be able to change the Var inside the array what's it's pulled from the array. The only way I think of it ends up with a hundred if-then statements/
  • edited February 2017
    Kkcajj wrote: »
    I am looking to use arrays...

    ...<<set $Random to either("Koolaid","Pie","Can food")>> and then use the array to give a specific variable an identity <<set $Item1 to "$Random">> and then use that same array again but to be set with another variable <<set $Item2 to "$Random">>, This worked if I use the array once again in the middle of the passage, however, the problem came up when displaying the "Item1 and Item2" variable again in another passage.
    Problem 1 is likely that there aren't any arrays in your example: either() is a function, and it's passing only one value, the randomly selected string from those provided, to the variable $Random. You're essentially telling the computer to pick a card, and then put that card in a box. The card in the box will always be what the computer chose initially; it won't change once picked. What you seem to want to do is put all the cards in a box, and have the computer pull one out at random.

    There's a few way to do that; one is to have the either() function run as a part of the link you describe later. Another is to use an array. I would recommend the array method personally. There's one other choice you'll need to make, and that's whether you want the computer to put the card back in the box after picking it, meaning the card can come up again, or whether you want it to be left out of the box. I'll detail how to do both below.

    One other thing:
    <<set $Item1 to "$Random">>
    

    This code works, but probably not the way you want it to or for the reasons you want it to. You're storing a the name of the variable $Random as the variable $Item1. This is probably not what you're trying to do. The code you probably want is without quotes; this will save the referenced variable's value to the new variable instead of its name.
    <<set $Item1 to $Random>>
    

    For example:
    <<set $x to 1>>
    <<set $y to "$x">>
    <<set $y to $y + 1>>
    <<print $y>>
    

    Yields "$x1", while
    <<set $x to 1>>
    <<set $y to $x>>
    <<set $y to $y + 1>>
    <<print $y>>
    

    Yields "2"
    Kkcajj wrote: »
    ...the concept is that once a player clicks on a link the variables would change each time he clicks the same link again, becoming something different each time.

    So here's some code:

    Using either():
    Item1: <span id="Item1"></span>
    Item2: <span id="Item2"></span>
    
    <<link "Randomize Item1">>
      <<set $Item1 to either("option1","option2","option3")>>
      <<replace "#Item1">>\
        <<print $Item1>>\
      <</replace>>
    <</link>>
    
    <<link "Randomize Item2">>
      <<set $Item2 to either("option1","option2","option3")>>
      <<replace "#Item2">>\
        <<print $Item2>>\
      <</replace>>
    <</link>>
    

    Using <array>.random():
    <<set $Random to ["option1","option2","option3"]>>
    
    Item1: <span id="Item1"></span>
    Item2: <span id="Item2"></span>
    
    <<link "Randomize Item1">>
      <<set $Item1 to $Random.random()>>
      <<replace "#Item1">>\
        <<print $Item1>>\
      <</replace>>
    <</link>>
    
    <<link "Randomize Item2">>
      <<set $Item2 to $Random.random()>>
      <<replace "#Item2">>\
        <<print $Item2>>\
      <</replace>>
    <</link>>
    

    Using <array>.pluck():

    In the code below, the value, after being selected, is removed from the array completely. This means you need a contingency plan for when the array runs out of values. We'll use a simple <<if>> statement in the below example.
    <<set $Random to ["option1","option2","option3"]>>
    
    Item1: <span id="Item1"></span>
    Item2: <span id="Item2"></span>
    
    <<link "Randomize Item1">>
      <<if $Random.length>>/%0 evaluates to false, so nothing else is needed%/
        <<set $Item1 to $Random.pluck()>>
        <<replace "#Item1">>\
          <<print $Item1>>\
        <</replace>>
      <<else>>
        <<replace "#Item1">>\
          Array is empty.\
        <</replace>>
      <<endif>>
    <</link>>
    
    <<link "Randomize Item2">>
      <<if $Random.length>>
        <<set $Item2 to $Random.pluck()>>
        <<replace "#Item2">>\
          <<print $Item2>>\
        <</replace>>
      <<else>>
        <<replace "#Item2">>\
          Array is empty.\
        <</replace>>
      <<endif>>
    <</link>>
    
  • edited February 2017
    Kkcajj wrote: »
    <<set $Quirks = [$Agile,$Strong,$Smart,]>>

    <<set $PlyrTrait to Array.random($Quirks)>>

    <<print $PlyrTrait>>

    Did some fiddling around and came up with this, still doesn't work but at least it is an actual array. (I think before was just a basic either function) I need to be able to change the Var inside the array what's it's pulled from the array. The only way I think of it ends up with a hundred if-then statements/

    I just saw this. I posted a big ol' thing.

    Anyway,
    <<set $PlyrTrait to Array.random($Quirks)>>
    

    Is not correct syntactically, at least for what you want to do. The value in the parentheses of the <array>.random() method is for defining a range of the values in the array to include. The <array> part of the method should be the name of the array variable. Note that arrays are zero-based, meaning the first value is 0, not 1.
    <<set $PlyrTrait to $Quirks.random()>>
    

    Will select a random value from the $Quirks array and pass it to the $PlyrTrait variable.

    By contrast:
    <<set $PlyrTrait to $Quirks.random(9)>>
    /%this statement randomly selects from the first 10 values of the array.%/
    
    <<set $PlyrTrait to $Quirks.random(4, 19)>>
    /%this statement randomly selects from the first 20 values of the array but omits the first 5%/
    

    etc.

    Also
    <<set $Quirks = [$Agile,$Strong,$Smart,]>>
    

    That extra comma at the end isn't going to do you any favors, so get rid of it.
  • This looks amazing, tried a few out but the outcome isn't quite what I was looking for, still may use them later but I was hoping to pull a variable out of the array, not just replace names. Like having a pool of light bulb except when you pull them out they turn on. This is the right direction except changing pulling the options is akin to rewriting a name.
  • bulbs correction
  • Kkcajj wrote: »
    This looks amazing, tried a few out but the outcome isn't quite what I was looking for, still may use them later but I was hoping to pull a variable out of the array, not just replace names. Like having a pool of light bulb except when you pull them out they turn on. This is the right direction except changing pulling the options is akin to rewriting a name.
    I'm printing the values, but the values are very real, they're stored as $Item1 and $Item2, and you can reference them later. Let's take a step back though. Can you tell us exactly what you're trying to do? No more metaphors or stand-ins: describe what you want to have happen in terms of how the final product will look. No code, just the end-user experience.
  • The end user experience hmm.... Well, explaining might be better than having a metaphor involving a pool filled with a single light bulb. I guess in terms of gameplay it would be best to look at it like "Neo scavenger" or search systems in any game. The end user experience would have the player (on a standard screen) where the side UI (Sugarcube) would be a list of actions for the player. IE the character screen, Search, and one two others I may include but aren't important atm. Search is the center of my discussion, the player would click this option and the game cycles through all the potential objects finally granting him something, at random. The object itself would be a variable, not "becoming" a variable or a variable becoming it, just the object itself. I also figure the same could be used for a trait system.

    I hope this makes sense, I am generally new at this but thought it would be a unique project.
  • Oh is it possible to have If/then functions cycle check through a list? Like having <<if $Item1 is ("Important","Boring","Example3")>>
    Also the above is just an example not an effort filled attempt
  • Kkcajj wrote: »
    Like having <<if $Item1 is ("Important","Boring","Example3")>>
    Try:
    <<set $item to "Boring">>
    
    <<if ["Important","Boring","Example3"].contains($item)>>I found $item<</if>>
    
  • edited February 2017
    @greyelf
    Thank you! I have seen your responses on several other discussions it seems such a waste for you to have to answer such a simple question, yet still. Thankyou.
    That's one question answer to my confusing game theory above.
    Oh and thank @Chapel
  • edited February 2017
    Also does this work <<if ["Smoothtalker","Socialite"].contains($PlyrTrait)>> <<set $Soc += (5)>> <<endif>> hoping the 5 would be a random number between 1-5
    Currently try to get a hang of things
    NVM fix was (random(5))
  • @greyelf @Kkcajj

    The <Array>.contains() method has been deprecated for a while now. Use <Array>.includes() instead.
Sign In or Register to comment.