GMS 2 Have a first time spawn problem[Solved]

Discussion in 'Programming' started by Artwark, Aug 28, 2018.

  1. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    Whenever I test my game for the first time, the objects spawn closer instead of being placed as they should.

    obj_platleft & obj_platright
    Code:
    Create event
    
    can = 1;
    
    Step event
    
    if can == 1 && global.start = 1
    {
    y+=4;
    }
    if y>1000
    {
    instance_destroy();
    }
    if !instance_exists(obj_bot)
    {
        can = 0;
    }
    
    
    obj_controller
    Code:
    Create event
    
    alarm[0]=100;
    h = choose(180,250,370,450);
    instance_create_layer(h+10,y,"layer_platform",obj_platleft);
    instance_create_layer(h+100,y,"layer_platform",obj_popup);
    instance_create_layer(h+1000,y,"layer_platform",obj_platleft);
    
    
    Alarm event
    
    alarm[0]=100;
    h = choose(180,250,370,450);
    instance_create_layer(h+10,y,"layer_platform",obj_platleft);
    instance_create_layer(h+100,y,"layer_platform",obj_popup);
    instance_create_layer(h+1000,y,"layer_platform",obj_platleft);
    
    
    and incase you're wondering what the code for global.start is

    obj_bot
    Code:
    Create event
    
    //direction = 180;
    //direction = gravity_direction;
    fall = 50;
    dead = 0;
    //gravity = x;
    global.start = 0;
    
    
    Global left pressed
    
    global.start = 1;
    hspeed+= fall;
    
    global.start is a function that once I left click, that's when the objects start appearing. How do I fix this?
     
  2. nlolotte

    nlolotte Member

    Joined:
    Aug 3, 2016
    Posts:
    465
    What are you trying to achieve? What is spawning? Sorry, I don’t quite enderstand. Perhaps one thing to check could be the x and y coordinates in the sprite editor to ensure they are spawning at the exact point you specify.
     
  3. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    Ok so here's the thing.

    The obj_platleft and obj_platright are platforms that appear from the top of the screen and move to the bottom. Now the problem is the timing of when these platforms appear as when the mouse left button is pressed, the first time, these platforms like merge or appear in groups instead of being separate and that first time part is what I want to fix.

    In other words..its appearing like this.

    https://imgur.com/a/HWmlv5g

    So how do I fix it?
     
  4. nlolotte

    nlolotte Member

    Joined:
    Aug 3, 2016
    Posts:
    465
    You need some form of a check to ensure they are not spawned more than once. It seems you're setting global.start back to one on the key press which emans both plats are going to be able to spawn.

    Try debugging with
    Code:
    show_debug_message(can)
    show_debug_message(global.start)
     
  5. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    The code you provided doesn't contain anything that would lead to your issue.
    Something else in your project must be causing trouble.

    In your screenshot, there are 3 layers of platforms: a layer on top and 2 slightly overlapping layers in the middle.
    The lowest layer of platforms consists of the top layer of platforms put on top of the other middle layer.

    I see 2 possibilities:
    1) You have some other code that messes with spawning platform layers.
    2) You have multiple controller objects that are spawning platforms at the same time over each other.

    Can you verify that there is only 1 controller object spawning platforms
    and can you provide more source code that is relevant to the issue (code of the platforms, the pop up, other spawning related code, ...)?
     
  6. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    Ok sure... the code of the objects is a bit lengthy so here's the export instead.

    https://we.tl/t-g92mOOd6L7

    Please let me know if you need anything else.

    EDIT: Incase you want to know exactly without the link

    Code:
    obj_popup
    
    step event
    
    if global.start == 1
    y+=4;
    if y>800
    {
    instance_destroy();
    }
    
    obj_score
    
    create event
    
    global.points=0;
    audio_play_sound(snd_music,true,0);
    
    Draw
    
    cx = camera_get_view_x(view_camera[0]);
    cy = camera_get_view_y(view_camera[0]);
    cw = camera_get_view_width(view_camera[0]);
    draw_set_font(fnt_goal);
    draw_text(cx+cw/2,cy+25,"Score:" +string(global.points));
    
    obj_highscore
    
    Create event
    
    highscore=highscore_value(1);
    
    Draw event
    
    cx = camera_get_view_x(view_camera[0]);
    cy = camera_get_view_y(view_camera[0]);
    cw = camera_get_view_width(view_camera[0]);
    draw_set_font(fnt_goal);
    //draw_text(cx+cw,cy+35,"Highscore:" +string(highscore));
    draw_text(200,100,"Highscore:" +string(highscore));
    
    
    
     
  7. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    Unfortunately, I only have the free version of GameMaker:Studio 2.
    This means I cannot import projects.

    In the additional code you posted, I don't see anything that would cause the issue either.

    Could you put this at the start of the create event of obj_controller and post the results?
    Code:
    show_debug_message("controllers: " + string(instance_count(obj_controller)));
    show_debug_message("left platforms: " + string(instance_count(obj_platleft)));
    show_debug_message("right platforms: " + string(instance_count(obj_platright)));
    show_debug_message("popups: " + string(instance_count(obj_popup)));
     
  8. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    I did that but the error shows that unknown function or script instance count.

    I guess I'll show every code I did so that it helps..

    Code:
    obj_bot
    
    Create event
    
    //direction = 180;
    //direction = gravity_direction;
    fall = 50;
    dead = 0;
    //gravity = x;
    global.start = 0;
    
    Step event
    
    //start
    
    // Rotation
    image_angle-=10;
    
    //Gravity
    if dead == 0
    hspeed=0;
    if mouse_check_button(mb_left)
    dead=1;
    
    if dead == 1
    {
    hspeed-=2;
    }
    if x<0
    hspeed = -hspeed;
    
    global left pressed
    
    global.start = 1;
    hspeed+= fall;
    audio_play_sound(snd_click,true,0);
    //motion_add(direction,fall);
    /*if dead == 0
    {
    hspeed-=2;
    dead = 1;
    }
    if dead == 1
    {
    motion_add(direction,fall);
    }
    
     collision with obj_platleft, obj_platright, obj_top1 and obj_top2
    
    scr_gameover();
    audio_play_sound(snd_hit,true,0)
    instance_change(obj_botdead,true);
    
     collision with obj_popup
    
    with(other)
    {
    global.points+=1;
    instance_destroy();
    }
    
    obj_botdead
    
    Create event
    
    instance_create_layer(100,100,"layer_score",obj_gameover);
    instance_destroy();
    audio_play_sound(snd_dead,true,0);
    
    step event
    
    image_angle+=90;
    
    
    Outside room
    
    instance_create_layer(100,100,"layer_score",obj_gameover);
    instance_destroy();
    audio_play_sound(snd_dead,true,0);
    
    obj_platleft & obj_platright
    
    Create event
    
    //y=vspeed;
    can = 1;
    
    Step event
    
    if can == 1 && global.start = 1
    {
    y+=4;
    }
    if y>1000
    {
    instance_destroy();
    }
    if !instance_exists(obj_bot)
    {
        can = 0;
    }
    show_debug_message(can)
    show_debug_message(global.start)
    
    obj_score
    
    Create event
    
    global.points=0;
    audio_play_sound(snd_music,true,0);
    
    Draw event
    
    cx = camera_get_view_x(view_camera[0]);
    cy = camera_get_view_y(view_camera[0]);
    cw = camera_get_view_width(view_camera[0]);
    draw_set_font(fnt_goal);
    draw_text(cx+cw/2,cy+25,"Score:" +string(global.points));
    
    obj_highscore
    
    Create event
    
    highscore=highscore_value(1);
    
    draw event
    
    cx = camera_get_view_x(view_camera[0]);
    cy = camera_get_view_y(view_camera[0]);
    cw = camera_get_view_width(view_camera[0]);
    draw_set_font(fnt_goal);
    //draw_text(cx+cw,cy+35,"Highscore:" +string(highscore));
    draw_text(200,100,"Highscore:" +string(highscore));
    
    obj_gameover
    
    Create event
    
    image_alpha = 0.5;
    x = room_width/2;
    y = room_height/2;
    
    
    Step event
    
    image_alpha =min(image_alpha+0.02+1);
    
    Key down-enter
    
    /// @description Insert description here
    // You can write your code in this editor
    game_restart();
    
    scr_highscore
    
    highscore_add("Best",global.points);
    //game_restart();
    
    
    I apologise if its very long but this is all there is to it in the code. Let me know if you need anything else.
     
  9. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    Sorry, I meant instance_number instead of instance_count.

    Looking at your code, I don't see the issue,
    so I'm really starting to think something outside of your code may be the cause.

    Do you noly have 1 room?
    Does the issue persist when you rebuild the room from scratch?
    What does this code in the create event of obj_controller say?
    Code:
    show_debug_message("controllers: " + string(instance_number(obj_controller)));
    show_debug_message("left platforms: " + string(instance_number(obj_platleft)));
    show_debug_message("right platforms: " + string(instance_number(obj_platright)));
    show_debug_message("popups: " + string(instance_number(obj_popup)));
    show_debug_message("bots: " + string(instance_number(obj_bot)));
     
  10. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    Yes I only have one room in the game and the game shows this in the output window and I haven't tried rebuilding the room from scratch.

    controllers: 1
    left platforms: 0
    right platforms: 0
    popups: 0
    bots: 1
     
  11. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    I found it.
    It's exactly what nlotte said from the beginning.
    You keep creating platforms every 100 steps in obj_controller, stacking them on top of each other before the player clicks to start playing.
    To solve this, just don't spawn platforms this early.
    Delete the alarm event of obj_controller
    and add something like this to the global left pressed event of obj_bot:
    Code:
    obj_controller.alarm[0] = 10;
     
  12. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    I did that now but now only two of them show up and they like never spawn...what do I do?
     
  13. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    Silly me, add this to the left pressed event of obj_bot instead:
    Code:
    with obj_controller {
        if alarm[0] < 0 {
            alarm[0] = 10;
        }
    }
     
  14. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    Still shows only two of them I'm afraid...
     
  15. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    I created a new project and put all the code and objects you provided in it.
    When I applied my last suggestion, I kept getting platforms beyond the 2 you're getting.

    What is currently the global left pressed mouse event of obj_controller?
     
  16. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    Uhh..that's not from obj_controller but from obj_bot instead...

    global.start = 1;
    hspeed+= fall;
    audio_play_sound(snd_click,true,0);
    with obj_controller {
    if alarm[0] < 0 {
    alarm[0] = 10;
    }
    }
     
  17. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    Sorry, I meant obj_bot indeed.

    That's the same code I have in the mouse left pressed event.

    If you add this to the alarm event of obj_controller:
    Code:
    show_debug_message("Alarm 0 fired!");
    and this to the create event of the platform objects:
    Code:
    show_debug_message("Platform created!");
    then how many times do you get each debug message?
     
  18. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    The first message keeps coming while the second message does not show up even after the objects spawned. after the platform objects went away, the alarm text kept coming in the debug mode.

    EDIT: Any news yet?
     
    Last edited: Sep 24, 2018
  19. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
  20. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,892
    If you go back to your original code, put in obj_co trler's Begin Step event:

    alarm[0] += !global.start;
     
  21. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    This doesn't make sense to me.

    The alarm 0 event of obj_controller is:
    Code:
    alarm[0]=100;
    h = choose(180,250,370,450);
    instance_create_layer(h+10,y,"layer_platform",obj_platleft);
    instance_create_layer(h+100,y,"layer_platform",obj_popup);
    instance_create_layer(h+1000,y,"layer_platform",obj_platleft);
    show_debug_message("Alarm 0 fired!");
    This is the only code in your project that prints the "Alarm 0 fired!" debug message.

    The create event of obj_platleft is:
    Code:
    //y=vspeed;
    can = 1;
    show_debug_message("Platform created!");
    In that case, every "Alarm 0 fired!" message has to be preceded by a "Platform created!" message.

    Are you sure the 3 things I mentioned are the same in your project?

    That should work too, yes.
     
  22. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    Yes they are the same.

    I applied the begin step event in obj_controller and yet it still doesn't work.

    I have the link for my project in case you want to see what the issue is.

    https://we.tl/t-q5VehKD2Hn

    EDIT: I managed to solve the issue. Basically, I had to comment the codes that were unnecessary! Thank you all for your help!
     
    Last edited: Oct 2, 2018

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice