Help with events: How to increase money if sprite 1 is live

Hi guys,
How can I code the below:
When ‘sprite1’ is created, add 1 to global variable ‘number’, I would also like to ‘add 1’ every 1 second.
I have tried looking but the tutorials I have found are only on scoring & player health.
Thanks in advance.

Do you want 1 to be added every second only when sprite 1 exists? If so you would have conditions something like:

The number of sprite1>0
The timer “incscore” is greater than 1 seconds

and actions:

Do +1 to global variable number
Reset the timer “incscore”

An alternative any time you want to do something per second is to use TimeDelta(). This simplifies the event to
Conditions:
The number of sprite1>0
Actions:
Do +1*TimeDelta() to global variable number

Thanks Matt,
I will try that, thanks again bud.
Jim.

Thanks, but I am stuck at the beggining, whats the first condition I should use for ‘The number of sprite1>0’
I cannot find anyhting like ‘at the begging’ - ‘number’
Sorry,
Thanks
Jim

In case you want two events separated:

There’s no condition to check if an object has been created, the best way is to add the actions (in this case increase the variable) in the same event where you create the object:

Conditions: The conditions in the event you create the Sprite Actions: Create object Sprite Do + 1 to scene variable "number"

As explained by MattLB, every second increase the variable:

Conditions: Timer "increase_variable" is greater than 1 second Actions: Do + 1 to scene variable "number" Reset timer "increase_variable"

Thanks guys, nearly there!
Still not working though?
Please see attached image.
‘money’ is a text sprite with ‘0’ in it, I dont have the option to update the GV ‘number’.
Thanks again

You aren’t increasing the variable in the second event but in the first one only.
Also, how do you know it isn’t working?, note that you’re increasing a global variable “number”, not a scene variable :slight_smile:

I have added the code to the 2nd event, thanks.
It is only 1 level, so I thought Global would be fine, is it?
I can swap to scene if that works.
I know it not working, as I am waiting 1 second and the ‘money’ is not going up.
Thanks again.

Using global variables is ok, just want to make sure that you know what you’re doing :wink:

But what is the action that updates the money object?, there’s no magic, you update the variable but you’ve to do something extra to show that on screen (or use the debugger), that’s why I’m asking how you know it doesn’t work.

For example if “money” is a text object, after updating the variable you can add this action to update the money text:

Do = GlobalVariableString(number) to the text of money (text object)

:slight_smile:

Thanks again.
I can’t get the right expression!
That’s the hardest bit in Gdevelop.
I have tried all these (I know I should only have 1 but I was trying to replicate your code)

If “money” is a text object you can use the action to change the text object string (Actions > Text object > Modify the text), and in the new text parameter use GlobalVariableString(number).

Check the platformer example, something similar is done to show the score :wink:

Done it!
Thanks again guys
Here is my code:

I’m not sure why you are deleting and recreating the “money” object every second. I suspect that you should have

=GlobalVariableString(number)

rather than

+GlobalVariableString(number)

and then you wouldn’t need to delete and recreate it.