Howdy, Stranger!

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

Running up a random result from a table

I'd like to make a table that goes:

1 Something
2 Something else
3 Another thing

and reference it in other passages where $whatever is displayed. How do I do that? One way is to have
<<set $whatever to random(X,X)>><<if $whatever eq 1>><<print "Zoooba">><<elseif $whatever eq 2>><<print "Yowzy">>... <endif>
But this is hard and it's easy to make an error. Any better way?

Comments

  • You could use an array variable to store your list of things.

    <<set $list to ["item 1", "item 2", "item 3"]>>
    As you can see an array starts / ends with square brackets and it uses a comma to separate each element of the array. You use a number (index) to reference, the first element has an index of zero and the last element has an index of the number of elements in the array minus one, so the last element of the above array would be two. (three elements minus one)

    eg. Place the following in a Start passage, run Test and refresh the browser to see different items.

    <<set $list to ["item 1", "item 2", "item 3"]>>

    <<set $whatever to random(0,2)>>

    The item is <<print $list[$whatever]>>
  • greyelf, I want to marry you. You've just spared me hundreds of kilowatts of effort. I was going to start working on an inventory system... wait, wait. I just realized it's a different thing. Thanks for the answer, I'll give it a try. Here is what I was going to get busy with: the inventory system. Any ready-made solutions for that? I'd like my items listed with commas, that's the thing. But how do you "slot them," if the inventory size is infinite? I mean, the last one has to end with the .

    Such as: if I have nothing, there is nothing on the screen. When I pick anything up, it prints: "You are carrying [anything]." Then the next things would go with commas. And if the inventory is empty again, the whole sentence would vanish.

  • There may be a more sophisticated solution, but the easiest way is to give the player an inventory item that they always possess so the inventory is always on and then name all your items with a comma and space to start, like ", knife"

    For example:

    You are carrying: Sword.

    Could be added to:

    You are carrying: Sword <<print ", knife" ", other knife">>.

    Would display as:

    You are carrying: Sword, knife, other knife.


    But this requires the player always has a sword.
  • Another option is to use the forums search feature while viewing the Workshop area, I would suggest searching for either "RPG" or "inventory".

    One possible link Example Turn-Based RPG (Pre-Alpha v. 0.4)
  • I've realized the problem is with infinite inventory size, or arsenal size. How do you put "and a sword" at the end of the list when the player picks up a sword, then have that same sword go after a comma to free space for the next weapon, e.g. "and a mace"? That's the trick. greyelf's solutions may work, but they are too complicated for me. Well, in my case I'll only allow two weapons at once, a small and a large one. So for me the code is simple.

    On acquisition of a Large weapon:
    <<set $arsenal to $arsenal +1>><<if $arsenal > 2>><<set $arsenal to 2>>You are already armed to the teeth.<<else>><<if $Largeweaponis > 0>>It would be hard to conceal another long weapon.<<else>><<set $Largeweaponis to "sword">>You pick up the sword.<<endif><<endif>>
    And the same for $Smallweaponis. Notice my idea about measuring string length found use, with <<if $Largeweapon eq 0>>. It's only going to be 0 if the string is empty, and I can reset it to "" every time the player discards the Large weapon. I don't need a separate size check.

    Now the indicator message for this in the status bar would be:
    <<if $arsenal eq 0>>You are unarmed.<<else>>You are carrying a <<if $arsenal < 2>><<if $Largeweaponis > 0>><<print $Largeweaponis>><<else>><<print $Smallweaponis>><<endif>><<else>><<print $Largeweaponis + "and a" + $Smallweaponis + ".">><<endif>><<endif>>
Sign In or Register to comment.