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

Can somebody please tell me why this way of altering a global variable doesn't work?

D

Darren

Guest
So the below code works, and is in the key press - any section of the object (making a rebinding system)

GML:
var n_key = keyboard_key;

if (n_key != vk_escape)
{
    global.key_down = n_key;
}
But if I want to be able to change the global key I'm changing, it doesn't work, ie create the key change object from my menu with


GML:
i =  instance_create_layer(x,y,"Instances", obj_key_controller);
i.new_ = global.key_u;
or something like that and then the below code in the object I create

GML:
var n_key = keyboard_key;

if (n_key != vk_escape)
{
    new_ = n_key;
}
Why is it not changing my global variable like it does when I reference it directly? I want it to be a variable I change so I can create one object for many global key changes, rather than a new object for each one.

Thanks all!
 

TsukaYuriko

☄️
Forum Staff
Moderator
You can't pass variables by reference - you're assigning the value of that global variable to a variable, then overwriting its value. If you want to change it, reference it directly.
 
D

Darren

Guest
You can't pass variables by reference - you're assigning the value of that global variable to a variable, then overwriting its value. If you want to change it, reference it directly.
So I will literally have to make the object for every single key I wish to change? Is there no more efficient way?
 

Nidoking

Member
Since you have the obj_key_controller, you just need to change the value in that every time you want to change the key binding. So instead of storing it in a global variable, you'd store it in obj_key_controller.new_
 
D

Darren

Guest
Since you have the obj_key_controller, you just need to change the value in that every time you want to change the key binding. So instead of storing it in a global variable, you'd store it in obj_key_controller.new_
I need the global variable because the key controller deletes itself after a key is chosen because it works by letting the player press any key on the keyboard whilst it exists, then assigning that key to the selected movement, and deleting itself.
 

Nidoking

Member
Ah, I see. Probably the best way to handle this, then, is to use a ds_map or array to store the key bindings. Let's say that you map the down key to the string "key_down". Then, when you create the obj_key_controller, you'd set its string to be "key_down". Now it knows which element of the map to set with the new key, and everything that reads from the map can use the strings to get the correct key bindings.
 
D

Darren

Guest
Ah, I see. Probably the best way to handle this, then, is to use a ds_map or array to store the key bindings. Let's say that you map the down key to the string "key_down". Then, when you create the obj_key_controller, you'd set its string to be "key_down". Now it knows which element of the map to set with the new key, and everything that reads from the map can use the strings to get the correct key bindings.
Thanks! That's a more elegant way of doing things, I just solved it by doing something very simple, always seems to be the way that once I've committed my dumb question to the internet I figure it out myself.
I simply now add a variable var_ to the menu that counts from 0 - 5, then in the controller I say if var_ = 0 key_down = new_key, if var_=1 key_up = new_key etc.
 
Top