Howdy, Stranger!

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

Creating random items

edited September 2016 in Help! with 2.0
Hello everyone!
Currently I'm working on creating a small game and I am rather fond of random things but I am currently running into a problem.

I try to create random things, let's say like this:

Weapon - can be a spear, a sword, an axe
made out of - wood, iron, bronze
with a random crystal attached - namely sapphire, ruby, etc.

so it would look like this:
<<set $Weapon to ["Sword", "Spear", "Bow", "Axe", "Staff"]>>
<<set $Material to ["Stone", "Bronze", "Silver", "Iron", "Wood"]
<<set $Gem to ["Ruby", "Emerald", "Saphire"]>>

These would allow for randomly created items with one characteristic from each of these three categories. Now I only need to know on how to incorporate all of these into a single item.

I think I have to work with arrays to get this done, which actually isn't such a problem to set up - I just need to understand how these randomly generated items can be added into the game. It might be necessary to use the .push - thingy, but I am still puzzled on how to do it right.

Oh, I am using sugarcube by the way but don't let that stop you.

Comments

  • edited September 2016
    I'd recommend not putting your lists of item attributes into story variables. You're bloating the history for no good reason. You should use the special setup object or make them globals of some sort.

    Anyway. You could do something like the following in your StoryInit special passage:
    /*
    	Set up various attributes used by items.
    */
    <<set setup.Weapons = [
    	"Sword",
    	"Spear",
    	"Bow",
    	"Axe",
    	"Staff"
    ]>>
    <<set setup.Materials   = [
    	"Stone",
    	"Bronze",
    	"Silver",
    	"Iron",
    	"Wood"
    ]>>
    <<set setup.Gemstones   = [
    	"Ruby",
    	"Emerald",
    	"Sapphire"
    ]>>
    

    Then to create a new random item, weapon in this case, you could do something like the following:
    <<set $weapon to {
    	type: setup.Weapons.random(),
    	mat: setup.Materials.random(),
    	gem: setup.Gemstones.random()
    }>>
    

    Now, let's say you wanted to print out the details of the new weapon (object):
    You have a $weapon.mat $weapon.type with $weapon.gem inlays.
    
    You could also wrap that up in a widget macro for ease of use.

    Or, say you wanted to so something based on one or more of the attributes:
    <<if $weapon.type is "Sword">>\
    Sword stuff….
    <<elseif $weapon.type is "Axe">>\
    Axe stuff….
    <<else>>\
    Any other weapon….
    <</if>>
    
    <<if $weapon.gem is "Ruby">>\
    Rubies!
    <<elseif $weapon.gem is "Sapphire">>\
    Sapphires!
    <<else>>\
    Any other gemstone….
    <</if>>
    
  • Wow, thank you for your work! This really helps me out.
  • edited November 2016
    What does using the 'setup;' keyword/ special object type add? I tried looking up documentation about this 'setup' variable word but couldn't find anything.
  • A brief description of the setup variable appears in the Variable Names sub-section of the Special Names section of the documentation.

    It is a global scoped variable that you can add your own custom properties/methods to so that they are available everywhere in your story, these custom values are not tracked or stored with either History or Saves.
  • greyelf wrote: »
    It is a global scoped variable […]
    As a minor quibble, and strictly speaking, it is scoped to the SugarCube module, rather than being globally scoped. Basically, any user code evaluated as normal will have access to it, whereas code evaluated externally will not.

    The difference is largely moot for the vast majority of users, however, some occasionally do strange things—e.g. using the on… event properties—so it does pay to keep the difference in mind.
Sign In or Register to comment.