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

GameMaker [SOLVED] GMS2: Instance Will Not Create

YoSniper

Member
I am programming a game where a player collects coins. Collecting ten coins without a miss is meant to incur a "streak" bonus. This will be represented by an object that displays text next to the score HUD that rises and fades away.

Score is kept by a singular persistent object called World. Up until this point, the score has been kept very well. My problems only arise with the StreakBonus object, which for some reason cannot be created.

Here is the relevant code for the Collectible object (coin).
Code:
//Interaction with player or boundary
    if place_meeting(x, y, Player) {
        if Player.powerup == "MULTIPLIER" {
            World.current_score += 2;
        } else {
            World.current_score += 1;
        }
        World.current_streak += 1;
        //--- FOR SOME REASON, THE FOLLOWING BLOCK DOES NOT GET EXECUTED ---
        if World.current_streak mod 10 == 0 {
            //Add a bonus score for streak
            instance_create_layer(room_width - 160, 40, 0, StreakBonus);
        }
        //--- END ERROR
        if World.high_score < World.current_score {
            World.high_score = World.current_score;
        }
        instance_destroy();
    }
And here is the ScoreEffect create event. It includes a debug prompt confirming that the instance has been created, and this never shows up.
Code:
/// @description Set value, alpha

bonus = World.current_streak / 10;
World.current_score += bonus;
alpha = 1;
depth = -15;

//DEBUG
show_message("Streak bonus created")
Can someone tell me why the StreakBonus object is not being created? I have confirmed that the conditions for the code block are met.

[EDIT] Found the issue. Was trying to create to a layer rather than using instance_create_depth.
 
Last edited:

chamaeleon

Member
GML:
instance_create_layer(room_width - 160, 40, 0, StreakBonus);
I don't like the idea of using a hard-coded number for the layer id. Either use the name of the layer as a string, or the result of calling a function that returns a layer id, or the layer variable of an instance, etc.

Continuing, I'd recommend using show_debug_message() or stepping through the debugger, if you haven't.
GML:
show_debug_message("Current streak = " + string(World.current_streak) + " => " + string(World.current_streak mod 10));
if World.current_streak mod 10 == 0 {
    show_debug_message("Should create an instance");
    //Add a bonus score for streak
    var inst = instance_create_layer(room_width - 160, 40, 0, StreakBonus);
    show_debug_message("New instance id is " + string(inst.id));
} else {
    show_debug_message("Condition is not true: " + string(World.current_streak mod 10 == 0));
}
 
Top