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

Darren

Member
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

Q&A Spawn Camper
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.
 

Darren

Member
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_
 

Darren

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_
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.
 

Darren

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