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

My instance variables won't work

8

82499

Guest
In my step event for my obj_player, I have written a code that moves it in 4 directions based on the arrow keys. I am currently using
if (keyboard_check(vk_right)) {
obj_player.x += spd;
sprite_index = spr_player_right;
image_speed = img_spd;

I am trying to remove
keyboard_check(vk_right) to be replaced with the local variable
var right_key = keyboard_check(vk_right)

The software just doesn't like the = sign in my variable. I have tried putting this as an instance variable but it still doesn't work. I am running Gamemaker 8.1 Lite. Does this change anything?

P.S. in the title, I was meant to say local variable.
 
Last edited by a moderator:

tagwolf

Member
I cover key variables and alternate key vars in this tutorial: https://forum.yoyogames.com/index.p...rol-diagonal-collision-etc.65684/#post-394291

Code:
// Controls
control_left = ord("A");
control_right = ord("D");
control_jump = vk_space;
// Alternate Controls
control_left_alt = vk_left;
control_right_alt = vk_right;
control_jump_alt = vk_up;
Code:
/// @description Control Player

// Get input
move_input_total = 0;
if keyboard_check(control_left) || keyboard_check(control_left_alt) { move_input_total -= 1; }
if keyboard_check(control_right) || keyboard_check(control_right_alt) { move_input_total += 1; }
 

TsukaYuriko

☄️
Forum Staff
Moderator
What's the actual code you're using when running into this problem? What's the exact error message? The opening post includes two fragments of code we'd have to puzzle together, and potential differences between our implementation and yours are not something we can account for.

If you're literally replacing one with the other, then you're mixing an assignment into an if statement... put the assignment on a separate line, then use the variable - and only the variable - in the if statement. Otherwise, the assignment will probably end up being interpreted as a second comparison, which will crash because the variable being compared has no value yet.

Edit: See FrostyCat's reply below. I completely missed that.
 
Last edited:

FrostyCat

Redemption Seeker
Of course it matters that you use a version as old as 8.1. Allowing = on the same line as the var is new to GMS 1.3. Before then, they had to be on separate lines.
Code:
var right_key;
right_key = keyboard_check(vk_right);
If you are going to use a version that old, you can't just take the latest code, throw it into your work and expect things to go smoothly. Too many things that contemporary GML takes for granted are actually not artifacts from the 8.1 timeframe.
 
8

82499

Guest
Of course it matters that you use a version as old as 8.1. Allowing = on the same line as the var is new to GMS 1.3. Before then, they had to be on separate lines.
Code:
var right_key;
right_key = keyboard_check(vk_right);
If you are going to use a version that old, you can't just take the latest code, throw it into your work and expect things to go smoothly. Too many things that contemporary GML takes for granted are actually not artifacts from the 8.1 timeframe.
Sorry, I just watched a tutorial from HeartBeast and basically copied the commands over. I didn't realise that my version was this old. Also, if this going to work if I put this in a script and put the script to be executed at the start of another script so that the variables work for the 2nd script.
 

TsukaYuriko

☄️
Forum Staff
Moderator
The first thing you should do when using a learning resource is to check whether it's for your version of GameMaker. (To put it into perspective, yours is eight years old! :p) Not doing so is a recipe for disaster.

This implementation is not going to work as-is in a script, as variables declared with var are local to the current event or script and do not carry over to other events or scripts. You would have to store these in a centralized manner, for example in a controller object, which also updates them every step. You can then access this data, for example through an appropriately named script such as get_key, which retrieves the correct control variable depending on the key indicated in a passed argument, within the script you need them in.
 
8

82499

Guest
So is it better to declare the variables in another script as local and then execute the script so that it sorta acts like a local variable or should I put the create event so that it becomes an instance variable.
 

TsukaYuriko

☄️
Forum Staff
Moderator
So is it better to declare the variables in another script as local and then execute the script so that it sorta acts like a local variable or should I put the create event so that it becomes an instance variable.
The former won't work, the latter works and is merely a bad practice. I wouldn't recommend either of the two. I would recommend using GMS 2, though.
 
8

82499

Guest
The version I'm using is a school version and the only installer I can find is the 8.1 Lite.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Unsure how you can find that when it's no longer available for download from the official site... your school should be providing you with access to the software and the educational license associated with it.

Anyway, are you still facing problems or can this be considered solved?
 
8

82499

Guest
///scr_move_state
scr_get_input();
if (dash_key) {
state = scr_dash_state;
alarm[0] = room_speed/8;
}
//Get the axis
xaxis = (right_key - left_key);
yaxis = (down_key - up_key);
// get hspd and vspd
dir = point_direction(0, 0, xaxis, yaxis);
hspd = lengthdir_x(len, dir);
vspd = lengthdir_y(len, dir);
// Get the length
if (xaxis == 0 and yaxis == 0) {
len = 0;
} else {
len = spd;
}
// Move
obj_player.x +=hspd;
obj_player.y +=vspd;
//Control the sprite
image_speed = .2;
if (len == 0) {
image_index = 0;
}
//Vertical movement
if (vspd > 0) {
sprite_index = spr_player_down;
}
if (vspd < 0) {
sprite_index = spr_player_up;
}
// Horizontal movement
if (hspd > 0) {
sprite_index = spr_player_right;
}
if (hspd < 0) {
sprite_index = spr_player_left;
}



this is my code to move

this is my scr_get_input

///scr_get_input
right_key = keyboard_check(vk_right);
left_key = keyboard_check(vk_left);
up_key = keyboard_check(vk_up);
down_key = keyboard_check(vk_down);
//Dash key
dash_key = keyboard_check_pressed(ord('C'));

whenever i spam the left arrow, it does something weird and then it moves to the right. Also, whenever i let go of any arrow key, it always naturally faces back to the right.
 
Top