Getting a variable to keep adding?
Cdog0292
24 Jan 2019, 04:28Hello,
I am recently new to quest, I been using gamebook and so far has been pretty successful. I am now starting to have a problem, my game consists of a character wanting to work for money. When I create a page, and this page is set to where I click on it in the game its suppose to keep adding the x amount of dollars I earned, I get it to where if I check my money, it start at zero, when I click the link and then re check money it goes to 25. When I click the link again it doesn't add it to previous amount.
How do I go about doing this? Thank you

Io
24 Jan 2019, 04:48We can help you more if you post your code. But the way I'd do is just sort of do something like, and pardon my psuedocode:
Player.Money=Player.Money+25
whenever you want to give the guy money. Not Player.Money=25, but Player.Money=Player.Money+25.
Cdog0292
24 Jan 2019, 05:02So this is what I have, I have
SetCounter ("Currency", 0)
and what I want to do is get the starting Currency to keep adding after clicking a link to go to a work page which is set like this
SetCounter ("Work", 50)
if (GetInt(game, "Currency") = 0) {
SetCounter ("Currency", game.Currency + game.Work)
msg ("Money + " + game.Currency)
So it works with the initial set up, when I look at my inventory at the beginning, I see
if (GetInt(game, "Currency") = 0) {
msg ("Money: " + game.Currency)
}
else if (GetInt(game, "Currency") > 0) {
msg ("Money: " + game.Currency)
}
So it works but I'm trying to find a way too keep adding money like
0 +50 = 50
50+50 = 100
100+50= 150
etc

Io
24 Jan 2019, 05:08I see the problem. You're overdoing the 'if'. For instance, in the second half:
if (GetInt(game, "Currency") = 0) {
msg ("Money: " + game.Currency)
}
else if (GetInt(game, "Currency") > 0) {
msg ("Money: " + game.Currency)
}
There's no reason for the if. Both conditions - no money and some money - do the exact same thing.
The problem is in your top part.
SetCounter ("Work", 50)
if (GetInt(game, "Currency") = 0) {
SetCounter ("Currency", game.Currency + game.Work)
msg ("Money + " + game.Currency)
You told the game "If my currency is Zero, set my currency to Currency+Work." Which is great, except now your currency isn't zero, and the game will go "Well you told me to give you the money if Currency is zero, but it's not zero, so I guess you don't want money."
Simply remove the if, like so:
SetCounter ("Work", 50)
SetCounter ("Currency", game.Currency + game.Work)
msg ("Money + " + game.Currency)
Hope this helps!

DarkLizerd
24 Jan 2019, 05:09Found it!
if (GetInt(game, "Currency") = 0) {
you are only adding money IF money is 0...
I don't think you need this check...
Also, this is redundant...
if (GetInt(game, "Currency") = 0) {
msg ("Money: " + game.Currency)
}
else if (GetInt(game, "Currency") > 0) {
msg ("Money: " + game.Currency)
}
Just do:
msg ("Money: " + game.Currency)
Cdog0292
24 Jan 2019, 05:17Well that did simply my coding by a whole lot, Thank you for that explanation, the problem for me adding more to it remains. It still starts with 0, then when I click the link to work, it increases it by 50. If I click it again it will not increase anymore.
any solution to that?

Io
24 Jan 2019, 12:44Hmm, from what you describe it SHOULD work, but admittedly I don't work with counters, only attributes, so it might be some silliness there.
mrangel
24 Jan 2019, 16:57That should work.
But it might be simpler to write:
ChangeCounter ("Currency", 50)
to add 50 to the value.
SetCounter sets the value; ChangeCounter adds to what is already there.
Cdog0292
24 Jan 2019, 17:32This is what I have under my game tab
SetCounter ("Currency", 0)
What I have is this under my "Work" Link
SetCounter ("Work", 50)
ChangeCounter ("Currency", game.Currency + game.Work)
msg ("Money + " + game.Work)
So when I go to look at my money, which is msg ("Money: " + game.Currency)
It starts with 0, When I click my work link, the 0 stays 0 now.

DarkLizerd
25 Jan 2019, 04:19If you are setting the counter Currency to 0, then adding 50 to it, you are resetting it...
In your code, before SetCounter("Currency",0)
Add :
msg("Currency: " + game.Currency)
Then after SetCounter, add it again...
You should have 2 messages...
Currency: 50
then:
Currency: 0
This will show you are resetting it to 0 before you add 50 to it, which would make it look like it is not working...
This is one way to add debugging messages to find logic errors...
Cdog0292
25 Jan 2019, 04:49Awesome this has helped me greatly, what I had to do was do my SetCounter ("Currency", 0) in another page where I don't revisit. this way it doesn't keep setting my currency to 0. Thank you so much everyone

DarkLizerd
25 Jan 2019, 18:11The best place for SetCounter ("Currency", 0) would be in the first page where you would set up all you variables...
Cdog0292
25 Jan 2019, 22:26Yea that's what I found out. took me a while but after re reading everything here I finally figured it out

DarkLizerd
26 Jan 2019, 03:52Mistakes are the stepping stones to success...

Io
26 Jan 2019, 04:12@DarkLizerd
And man, is the staircase tall or what?