Page 1 of 2

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

PostPosted: Sun 10 Feb 2019, 19:05
by JimmyD
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.

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

PostPosted: Mon 11 Feb 2019, 02:46
by MattLB
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

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

PostPosted: Mon 11 Feb 2019, 11:19
by JimmyD
Thanks Matt,
I will try that, thanks again bud.
Jim.

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

PostPosted: Mon 11 Feb 2019, 13:43
by JimmyD
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

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

PostPosted: Mon 11 Feb 2019, 14:56
by Lizard-13
In case you want two events separated:

When 'sprite1' is created, add 1 to global variable 'number'

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:
Code: Select allConditions: The conditions in the event you create the Sprite
Actions: Create object Sprite
         Do + 1 to scene variable "number"


I would also like to 'add 1' every 1 second

As explained by MattLB, every second increase the variable:
Code: Select allConditions: Timer "increase_variable" is greater than 1 second
Actions: Do + 1 to scene variable "number"
         Reset timer "increase_variable"

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

PostPosted: Mon 11 Feb 2019, 15:19
by JimmyD
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

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

PostPosted: Mon 11 Feb 2019, 16:10
by Lizard-13
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 :)

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

PostPosted: Mon 11 Feb 2019, 16:17
by JimmyD
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.

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

PostPosted: Mon 11 Feb 2019, 16:45
by Lizard-13
Using global variables is ok, just want to make sure that you know what you're doing ;)

I know it not working, as I am waiting 1 second and the 'money' is not going up.

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:
Code: Select allDo = GlobalVariableString(number) to the text of money (text object)

:)

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

PostPosted: Mon 11 Feb 2019, 17:08
by JimmyD
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)