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

Legacy GM Movement + Item Use (Order of Events)

O

Ourobolus

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

NightFrost

Member
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.
 
O

Ourobolus

Guest
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.
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 by a moderator:

NightFrost

Member
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).
 
O

Ourobolus

Guest
Ok, so that means a sequence of actions to perform. There's multiple ways of different complexities to do this, but basically you need have the game to 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).
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!
 

NightFrost

Member
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!
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();
}
 
O

Ourobolus

Guest
That worked! Thanks.

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. ]
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
 
Top