Getting a variable to keep adding?

Hello,

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

We 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.


So 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

I 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!


Found 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)


Well 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

Hmm, from what you describe it SHOULD work, but admittedly I don't work with counters, only attributes, so it might be some silliness there.


That 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.


This 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.


If 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...


Awesome 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


The best place for SetCounter ("Currency", 0) would be in the first page where you would set up all you variables...


Yea that's what I found out. took me a while but after re reading everything here I finally figured it out


Mistakes are the stepping stones to success...


Io

@DarkLizerd

And man, is the staircase tall or what?


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

Support

Forums