1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 Movement + Item Use (Order of Events)

Discussion in 'Programming' started by Ourobolus, Mar 3, 2019.

  1. Ourobolus

    Ourobolus Member

    Joined:
    Mar 3, 2019
    Posts:
    4
    Hi everyone!

    Still relatively new to GameMaker, I've watched a bunch of tutorials and the like, but I'm having issues making it so that the player character performs their movement first, and then uses the item that is clicked on. For example, I click on a light switch, the player moves to it, and THEN the light clicks on. Right now all I can seem to do is have the light turn on when it is clicked, while the player moves towards it simultaneously.

    Currently I have a horizontal movement script set up on left-click event for the player object which pretty much consists of one line:
    Code:
    move_towards_point(target_x, y, 8);
    and a step event for the player object that stops when they reach that point:
    Code:
    if point_distance(x, y, target_x, y)<8 {
      speed = 0;
     }
    
    On the light switch I have in a step event (which...i'm sure isn't the best place for it - i've also tried it in a left-mouse-click event):

    Code:
    if mouse_check_button_pressed(mb_left) {
     if collision_point(mouse_x, mouse_y, o_light, true, false) {
      scr_light_switch();
    }}
    the scr_light_switch() script just changes the lighting, that seems to work fine.

    Any thoughts? I've looked through the documentation, including the Events entry, and I can't seem to figure out how to do this. Thanks in advance!
     
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,867
    In the switch step event, besides checking for mousebutton also check that player is close enough (using the point_distance again) and only then switch the light.
     
  3. Ourobolus

    Ourobolus Member

    Joined:
    Mar 3, 2019
    Posts:
    4
    I tried that as well before, but then it only works if the player is next to the switch. I mean, The player walks to it, but then I have to click the switch again to trigger it. I'm trying to make it do them both in one sequence.

    Code:
    if mouse_check_button_pressed(mb_left) {
     if collision_point(mouse_x, mouse_y, o_light, true, false) {
      if point_distance(x, y, o_codi.x, o_codi.y)<250 {
      scr_light_switch();
    }}}
    (codi being the player object)
     
    Last edited: Mar 3, 2019
  4. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,867
    Ok, so that means a sequence of actions to perform. There's multiple ways of different complexities to do this, but basically you need to have the game remember that the player needs to switch the light once they have reached their destination. If the light switch is the only possible sequence of actions, it can be implemented lightly. I'd probably do it like this: when you click somewhere to set your movement target, I'd also check if the click hits a switch. If so, I'd save the ID of the switch to a variable on the player. Once the player reaches the position and stops, check if there's an ID saved. If so, switch the light on that instance. Also, when you move and did not click on a switch, set that variable to noone. The ID check if then a if(switch_id != noone).
     
  5. Ourobolus

    Ourobolus Member

    Joined:
    Mar 3, 2019
    Posts:
    4
    That seems like a decent idea, thanks. Ideally this would be a function/script I'd use for pretty much any item that can be interacted with, so setting up IDs could work. Thanks!
     
  6. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,867
    If you want multiple different clickable items, but there's only one type action each item can perform, then it is best to have the items themselves worry about what exact action to take. Let's say the item instance ID you clicked is saved to target_id variable. When player reaches their destination and checks for this, it instructs the item to perform an action by setting a variable. Like:
    Code:
    if(target_id != noone){
        target_id.do_action = true;
    }
    Each clickable item has do_action variable set to false in create event. Each item checks it in step, and performs an action specific to them when true. For example the light switch:
    Code:
    if(do_action == true){
        do_action = false;
        scr_light_switch();
    }
    
     
  7. Ourobolus

    Ourobolus Member

    Joined:
    Mar 3, 2019
    Posts:
    4
    That worked! Thanks.

    Right, that makes sense. The main thing to check before calling any of those IDs is to check that the player has stopped moving, because almost all interaction (it's a point-and-click game) will be done once the player has moved to an item that has been clicked on. So I'll just need to specify the various things that need to occur depending on itemID
     

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