• 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] Spawning four instances at once

Q

qbot21

Guest
Hello!!!

I came across a problem that when I fire a gun, four instances of a bullet are created at once (each on top of other). I really don't have idea what causing this problem :/

I have a parent o_player obj, which have three childs. Each child inherits whole step event from the parent. At start of the level you can choose which and how many child obj's you want to have in room.

Parent step:
Code:
// Character avater
icon = char_class[class, 1];
spr = char_class[class, 2];
name = char_class[class, 3];
class_n = char_class[class, 4];

// Character base stats
hp_max = char_class[class, 5];
hp = hp_max;
spd = char_class[class, 6];
acc = char_class[class, 7];
crit = acc * .1;

// State on start of level
if state != player.spawn{
    unit_select();
}
// Skill cooldown
id.skill_1_cd = clamp(id.skill_1_cd, 0, cd);
id.skill_2_cd = clamp(id.skill_2_cd, 0, cd);

if id.skill_1_cd > 0{
    id.skill_1_cd -= 1;
}
if id.skill_1_cd == 0{
    id.skill_1_ready = true;
}

switch (state) {
    case player.spawn:
        spr = s_char_2;
    break;
    case player.movement:
        if id.active{
            if mouse_check_button_pressed(mb_right){
                var _xx = mouse_x;
                var _yy = mouse_y;   
                if mp_grid_path(global.grid, my_path,x, y, _xx, _yy, true){
                    path_start(my_path, spd, path_action_stop, false);
                }
            }
            if id.skill_1_ready{
                if keyboard_check_pressed(ord("Q")){
                    path_end();                                   
                    state = player.skill_1;
                }
            }
        }
    break;
    case player.skill_1: // use skill 1
        char_skills(id.skill_1); //shooting, argument taken from child obj!!!
        state = player.movement;
    break;
    case player.skill_2:       
    break;
    case player.downed:
    break;
    case player.dead:
    break;
}

show_debug_message(instance_number(o_bullet));
shooting script:
Code:
skill = argument0;

switch (skill){
    case 0:
        var inst = instance_create_layer(x,y,"Instances",o_bullet);   
        with(inst){
            motion_add(point_direction(x,y,mouse_x,mouse_y), 3);
        }
        id.skill_1_ready = false; // disable skill use
        cd = 60; // cooldown value
        id.skill_1_cd = cd; // set skill cooldown
    break;
    case 1:
        show_message("Skill 2 used !!!");
        cd = 90;
    break;
}
Any help or ideas are appreciated. Thank you!
 
Q

qbot21

Guest
Tried that in every possible way, but in script everything seams to be ok. When I debug that case for instance number it always return 1 but in step event it gives me 4.
 

CloseRange

Member
did you already check to make sure there was only 1 player? perhaps there are multiple stacked on top of each other, each creating their own bullet
 
Q

qbot21

Guest
did you already check to make sure there was only 1 player? perhaps there are multiple stacked on top of each other, each creating their own bullet
I didn't checked that case, Won't hurt to give it a try :)
I have room, where you select level and characters and put them in four slot array. After that you click play and by room_instance_add it create objects that were set in slots in previously selected room and finally you go to that room.
 
Q

qbot21

Guest
Ok, I checked. You were right @CloseRange. There are four instances of player object. I'll try to work it out by my self.
Thanx for now :)

[EDIT]

It turned out that the problem was in the for loop. The code responsible for creating characters was in loop. As the for loop has four entries, characters in every slot was created four times. I would never have thought of that.

Thanx again @CloseRange for accurate sugestion!!!

Oh and if anyone wants to see what was the case, here's character select code:

Code:
if keyboard_check_pressed(ord("D")) or (position_meeting(mouse_x, mouse_y, next) and mouse_check_button_pressed(mb_left)) && class <= 2{
    class ++;
    if class > 2{
        class = 0;
    }
}
else if keyboard_check_pressed(ord("A")) or (position_meeting(mouse_x, mouse_y, prev) and mouse_check_button_pressed(mb_left)) && class >= 0{
    class --;
    if class < 0{
        class = 2;
    }
}

for( var i = 0; i <= 3; i ++){
    if slot[i] == -1 and char_class[class, 8] = false{
        if keyboard_check_pressed(vk_enter){
            slot[i] = char_class[class, 0];
            slot_icon[i] = char_class[class, 1];
            slot_class[i] = class;
            char_class[class, 8] = true;
            slot_clear[i] = instance_create_depth(128, 20 + (sprite_get_height(s_frame) + 10) * (i +1), -100, o_x);
            break;
        }
    }
    else if slot[i] != -1{
        if position_meeting(mouse_x, mouse_y, slot_clear[i]){
            show_debug_message(i)
            if mouse_check_button_pressed(mb_left){
                slot[i] = -1;
                slot_icon[i] = -1;
                char_class[slot_class[i], 8] = false;
                instance_destroy(slot_clear[i]);
            }
        }
    }
   
}
// This chunk was in for loop
if position_meeting(mouse_x, mouse_y, o_start_level){
    with o_start_level image_index = 1;
    if mouse_check_button_pressed(mb_left){
        global.stage = r_test_map;
        room_instance_add(global.stage, 96, 128, slot[0]);
        room_instance_add(global.stage, 96, 192, slot[1]);
        room_instance_add(global.stage, 96, 256, slot[2]);
        room_instance_add(global.stage, 96, 320, slot[3]);
        room_instance_add(global.stage, 0, 0, o_grid);      
        room_goto(global.stage);
    }
}
else with o_start_level image_index = 0;
Cheers!!!
 
Last edited by a moderator:
Top