How to add value to an existing variable? (Gamebook) [Solved!]

Thank you to all those who helped me solve my previous problems. I was able to have progress with my game. Thank you very much!

I have a new question though. I tried several experiments to learn to add a value to an attribute but so far I am not successful.
For example,
Page1: Player chooses to learn Dance Skill [Dance +1]
And in future pages, let's say Page12, the player gets a chance to improve his/her dance skill.
Player clicks Yes and goes to Page13 where the trigger to a +1 awaits him/her.
So it'll be [Dance +2] now.

Oh and I set

dance = 0

in the game > script part. That's in code view.
And then I used

set (player, "dance", "1")

When I added 1 value to the dance attribute.
(I hope this helps you understand my case.)

In future pages, let's say Page20, there will be a condition to find a job. If the player's dance skill is +2 above, the player can apply but if not, the player cannot apply.

How do I do that? I am a beginner and I learn fast in screenshot/code view. It helps me study and compare the difference of this and that and how it works. Thank you in advance for your help!


I'm only used to code view, so I don't know how it'll look in the GUI. But if you want to look at both, you can paste in the code and see how it appears.

The basics of using attributes:

  • player.dance = 1 - this sets the player's "dance" attribute to 1.
  • set (player, "dance", 1) - this is exactly the same thing as the one above

You probably do not want to do:

  • set (player, "dance", "1") - this sets it to the piece of text "1", not the number 1
  • player.dance = "1" - this is the same. You can display the value to the user, but you can't add something to it or test if it's bigger/smaller than another number

If you want to increase an attribute, you could use:

  • player.dance = player.dance + 1 - sets the "dance" attribute to 1 more than it was before
  • set (player, "dance", player.dance + 2) - alternate way of writing it, this time adding 2

Then to use the variable, you would do:

  • if (player.dance >= 2) {
    • the script here will only happen if the "dance" attribute is 2 or more
    • You could put the script here to display some extra text, or to add a link to the page
  • }

It's up to you if you say >= 2 ("two or more") or > 1 ("more than one").


Thank you!
I tried it and it works but for some reason the value is not what I expect it to be.
I placed a player.dance indicator per page so I can check the current stats and whenever I try either of the choices you mentioned set (player, "dance", "1") and set (player, "dance", player.dance + 2)
The results of the stats becomes

Dance +11

Instead of [Dance +2] And when I try adding it with 2, it becomes,

Dance +12

Instead of [Dance +3] It's like it adds +10 along the way. How do I fix that?


Please, look at my post.

I said your line should be:

  • set (player, "dance", 1)
    or
  • player.dance = 1

and that you should be careful not to do:

  • set (player, "dance", "1")
    or
  • player.dance = "1"

The quotes around the number turn it from a number into a string, a piece of text.
Once it is a string, you can't do sums with it. + works differently for strings.

With the quotes:

  • "cat" + "fish" is equal to "catfish"
  • "bob" + 3 is equal to "bob3"
  • "1" + "2" is equal to "12"

Without quotes:

  • 1 + 2 is equal to 3

I see! Thank you thank you thank you very much!
I always make the same mistake with the details. Thank you for the help! It helped me a lot!


The codes which are given to add the values to the variables are apt to the programmer needs to apply that if this the programmer does not know. ( https://quickbooksupports.co/ ) Quickbooks customer service also applies the same to add the value to the variable.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums