• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

global variables adding 1

Pfap

Member
[solved]



When my game starts I set a global variable to zero.
global.streak = 0
in the create event of another object I have:
global.streak += 1
then in a different object I turn the real that global.streak is holding into a string and draw it to the screen.
I get the number 120 drawn on screen when I'm expecting a 1.

Can someone point out my mistake?
I want to count the players streak of successes between rooms.
So, adding one to the global.streak each room is my goal.

Should I use an event other than the create event? I tried an alarm and got a value of 60 returned... how can I add just 1??
 
Last edited:

Tthecreator

Your Creator!
How are you creating your objects? If you just open the debugger, then how many instances are there?
is there any other code that changes this variable?
You can do a cntrl+shift+F search on it to see if it's used anywhere else.
 

Pfap

Member
How are you creating your objects? If you just open the debugger, then how many instances are there?
is there any other code that changes this variable?
You can do a cntrl+shift+F search on it to see if it's used anywhere else.

I'm using instance_create() to bring the objects into the room. Also, I'm putting the code in one object and then having other objects be its children. There isn't any other code that would change it, I'm thinking it's adding one for each frame of the game for some reason? I just used the function show_debug_message and it's showing it adding 1 individually 120 times. Maybe when using instance create it is creating the calling object 120 times?
 
Last edited:
X

XirmiX

Guest
I'm using instance_create() to bring the objects into the room. Also, I'm putting the code in one object and then having other objects be its children. There isn't any other code that would change it, I'm thinking it's adding one for each frame of the game for some reason?
How many of these objects are spawned? The children objects that is, which I'm guessing are the ones that increase the global variable's amount?
 

Pfap

Member
How many of these objects are spawned? The children objects that is, which I'm guessing are the ones that increase the global variable's amount?
It should just be once, but I just edited my previous post. If you use instance_create() does it create only a single instance?



Edit:
Part of me just wants to use
global.streak += .006 and then round the resulting .72 up to one.
 
Last edited:
So, adding one to the global.streak each room is my goal.
You are adding 1 to global.streak in the Create Event of your parent object. If you are getting the number 120 back, that code must be being called 120 times.
- Your child objects are probably calling the Parent objects Create Event.
- Are you using the code "event_inherited" in your child objects Create Event by any chance?
- OR - If your child objects do not have a Create Event, they will be using the Parent objects Create Event by default, which is the problem.

So if you have 60 child objects, even though the code to increase global.streak is in the parent object, every child object will also be executing that code and adding one to the global variable every time a child object is created.

Sounds like you should only add one to global.streak when you change rooms.

Somewhere you must have code that checks when the player has completed the room, and then calls "room_goto_next()" or similar.

Just add one to global.streak just before you change rooms.
 

Pfap

Member
You are adding 1 to global.streak in the Create Event of your parent object. If you are getting the number 120 back, that code must be being called 120 times.
- Your child objects are probably calling the Parent objects Create Event.
- Are you using the code "event_inherited" in your child objects Create Event by any chance?
- OR - If your child objects do not have a Create Event, they will be using the Parent objects Create Event by default, which is the problem.

So if you have 60 child objects, even though the code to increase global.streak is in the parent object, every child object will also be executing that code and adding one to the global variable every time a child object is created.

Sounds like you should only add one to global.streak when you change rooms.

Somewhere you must have code that checks when the player has completed the room, and then calls "room_goto_next()" or similar.

Just add one to global.streak just before you change rooms.




Thanks man, the child parent stuff is really convenient, but tricky to work with.

Thanks everybody :)
 
Top