GameMaker: Studio

GameMaker: Studio

View Stats:
g_brix Nov 12, 2016 @ 12:43pm
"Create" event running multiple times?
It seems like an instance is running its own create event every time I create another instance of the same object, causing duplicates to be created in the same location. I thought the create event only runs once, when an instance of an object is created? I don't understand what's going on.

Context if it helps:

In the room the game takes place in, the first object spawned is an invisible "room manager", which every game step checks conditions of the game, and then runs specific code whenever certain conditions are met.

This is a simple test game, with a top-down "Zelda" perspective, with a player character whose movement is controlled with the arrow keys. "Candy" objects spawn intermittently in random intervals, and the goal of the game is to collect as much candy as possible by moving the player character over each piece of candy to collect it.

Now, after collecting a certain amount of candy, the room manager spawns enemies. Specifically, a turret that shoots bullets at the player spawns after collecting five candies. Then, a second turret spawns in a different location after the player collects ten candies.

That second spawn is where it gets strange, because it seems to run the original turrets "Create" event again, while running the Create event for the new turret in a different location.

To troubleshoot this, I added a show_debug_message at the top of the script that runs in the Create event of each object.

This is what shows up in the console after the player collects 5 candies:

"-NOW TRIGGERING: first enemy spawn (because 5 candies collected)."
"***IN CREATE EVENT for oTurret (ID=100009) @LOC(50,50)***"

And this is what shows up fater the player collects 10:

"-NOW TRIGGERING: second enemy spawn (because 10 candies collected)."
"***IN CREATE EVENT for oTurret (ID=100009) @LOC(50,50)***"
"***IN CREATE EVENT for oTurret (ID=100020) @LOC(400,50)***"

Note the ID's, which is the "id" property of the instance running the show_debug_message() function; the object, oTurret, whose ID is 100009, seems to be re-running the code for its Create event a second time. What...???

I've ensured the problem's not with the room manager object. The script that runs inside the "step" event of the room manager has this code that triggers the spawns:

if ((global.CANDIES_COLLECTED>=5) && (five_candies_collected==false) )
{
show_debug_message("-NOW TRIGGERING: first enemy spawn (because 5 candies collected).");
five_candies_collected = true;
instance_create(50, 50, oTurret);
}
else if ((global.CANDIES_COLLECTED>=10) && (ten_candies_collected==false) )
{
show_debug_message("-NOW TRIGGERING: second enemy spawn (because 10 candies collected).");
ten_candies_collected = true;
instance_create(400, 50, oTurret);
}

So it's not re-running the code in the first "if" block because of the flag preventing it. I've also tried commenting out the "instance_create(400, 50, oTurret)" line to confirm that.

Thanks for reading all of that, any ideas as to what might be causing this are greatly appreciated!
Last edited by g_brix; Nov 12, 2016 @ 12:44pm
< >
Showing 1-4 of 4 comments
woodsguide Nov 12, 2016 @ 12:49pm 
if candies >=5 run code

if candies >=10 run code


since the first one is true as well it would run the first code again...


in your first bit
how about adding something along the lines of

if (candies >=5 && candies <= 10) run code
g_brix Nov 12, 2016 @ 12:54pm 
Right, but the flags "five_candies_collected" and "ten_candies_collected" already prevent those sections from being re-ran.
woodsguide Nov 12, 2016 @ 1:24pm 
how about somewhere after



if ((global.CANDIES_COLLECTED>=5) && (five_candies_collected==false) )
{
show_debug_message("-NOW TRIGGERING: first enemy spawn (because 5 candies collected).");
five_candies_collected = true;
instance_create(50, 50, oTurret);
}


set five_candies_collected = false

MinorThreat Nov 12, 2016 @ 2:43pm 
Every object runs it's creation code. They look the same but they aren't. You could put something in the creation code that would tell the turret to only run that code if there is only one turret that exists.
< >
Showing 1-4 of 4 comments
Per page: 1530 50

Date Posted: Nov 12, 2016 @ 12:43pm
Posts: 4