Legacy GM Change variable value by clicking on instances

wilmer

Member
Greetings to everyone in the community:
I need help in this, is that I have an object that works as a touch button, which I separate in the room in 3 instances, whose function is that by clicking on each one, my player moves only to the "Y" position of each instance; my problem is that I have a variable called "lane" for my player, and I want to know how to change the value of the variable between (1,2,3)? (
that is to say, lane = 1) for what? , so to tell my player which lane he is in, since my game is runner type and my player moves in 3 different lanes. Graphically it looks like this:

Inside the room, I make "creation code" and I put this variable to each instance of the object of the button to identify them:
Code:
button_id=1
button_id=2
button_id=3
In the player's create event (obj_player), write this as my player starts in lane # 2:
Code:
lane = 2
I make this request to use the following code, which tells both the generated object and the player in which lane they are, to understand me a little more, I do not know if it is advisable to use the same variable for two different objects (player and object generated ):
name: Object_box // generated object
step event:
Code:
var obstacle = instance_place(x, y, obj_player);
if (obstacle != noone && obstacle.lane  == obj_player.lane) {
pslife -= 10 //being in the player's position, the player is subtracted -10.
}
 
Last edited:

wilmer

Member
if TheouAegis, I also need help in that, but I am very interested in how I do to change the value of the variable lane of the player by clicking on the instances that are buttons.
 

TheouAegis

Member
If there is one player, just do

if abs(button_id-obj_player.lane) == 1 obj_player.lane = button_id;

You can remove the absolute value check if you want the player to jump immediately 2 lanes.
 

wilmer

Member
Theo Aegis, I do not understand the hairline in the middle
(button id-obj_player.lane). I must put this code in the player I imagine.
 

TheouAegis

Member
The hairline is a minus symbol. If the the player's lane and the button's id are 1 value apart (e.g., the button's id is 2 and the player is in lane 3), then move the player to the button's lane. So if the player was in lane 1 and you clicked on button 3, since the player's lane is 2 lanes away from the button's, the player wouldn't move there. Like I said in the second part, if you just remove that conditional, the player would be able to move to whatever button you push regardless of how far away the button is.
 
Top