Howdy, Stranger!

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

How can I make one variable be the same as another?

Hi, just to clarify things, I am using Harlowe. The question I want to ask is a bit of a complicated one.
It's easiest for me if I just start at the beginning and work towards my question.

I am making a game where you collect Characters and use them to battle others. You can get multiple of the same character What I want to happen is when you lose a battle, you lose the character that you were using. Lets say I had a mummy, a vampire, and 2 ghosts. I want to fight the werewolf using the ghost. I have figured out that part of the code. I just set the variable $character to "Ghost". However, if I lose, I want to be able to make the code to where I lose just one of the ghosts. What I did was every time I obtained a new ghost, the variable $ghost would go up by one. What I want is when I lose a battle, for the variable $ghost to go down. It's pretty tricky because if I was using the vampire, then I would have to have the variable $vampire go down. So what I did was when I wanted to use the ghost, I would click on a button that would bring me to a page that would do the following: (set: $character to $ghost). When I lose a battle, I did: (set: $character to $character - 1). I thought that since I set $character to $ghost, $ghost would decrease by 1. But all it does is decrease $character by 1. The only other way I could think was do this:
(if: $character is $ghost)[ (set: $ghost to $ghost - 1)]
(elseif: $character is $vampire[ ( set: $vampire to $vampire - 1]
ect. ect.

However that would be extremely long and tedious since I plan to have over fifty characters when this is done. Is there a simpler way to do it? Thanks for your help.

Comments

  • Actually, I just now am starting to use sugarcube, so if someone could give me the answer in Sugarcube coding, that would be great!
  • edited March 2016
    You need an array.

    StoryInit:
    <<set $ix_ghost to 0>>
    <<set $ix_vampire to 1>>
    <<set $ix_werewolf to 2>>
    <<set $ix_boggart to 3>>
    <<set $ix_ghoul to 4>>
    <<set $m_count to [ 0, 0, 0, 0, 0 ]>>
    <<set $m_name to [ "Ghost", "Vamprie", "Werewolf", "Boggart", "Ghoul">>
    

    Then when you fight you say $player_ix to the ix value of the character they are using.

    If they loose:
    <<set $m_count[$player_ix] -= 1>>
    <<print "Your " + $m_name[$player_ix] " has been defeated!">>
    

    There is more complex structure using an array of structures you could consider:
    <<set $monsters to [
    { posessed: 0,
      name: "Ghost" },
    { possessed: 0,
      name: "Vampire" },
    { possessed: 0,
      name: "Werewolf" },
    { possessed: 0,
      name: "Boggart" },
    { possessed: 0,
      name: "Ghoul" }
    ]>>
    <<set $m_index to { Ghost: 0, Vampire: 1, Werewolf: 2, Boggart: 3, Ghoul: 4 }>>
    

    Referencing the values is a little more complex:
    <<set $ix to $m_index.Ghoul>>
    <<set $monsters[$ix].possessed -= 1>>
    

    The advantage is that you can put other attributes for the monsters into the data structure - which is easier that trying to maintain multiple cross matched arrays.

    You can also do:
    <<set $monster to $monsters[$ix]>>
    

    which makes $monster point to the index monster structure so you can reference it's values directly:
    <<set $monster.possessed -= 1>>
    <<print "Your " + $monster.name + " has been defeated!">>
    

    (Samples are indicative and have not been tested.)
  • Thank you so much!!! This will help me big time!
Sign In or Register to comment.