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

GameMaker Change Key State By variable value

FoufaDjo

Member
so i want to change key state if its pressed or not by variable value something like this and idk if it work pls help me :

rkey = keyboard_check(ord("D"));
lkey = keyboard_check(ord("Q"));
dkey = keyboard_check(ord("S"));
ukey = keyboard_check(ord("Z"));

playerid.rkey = mover;
playerid.lkey = movel;
playerid.dkey = moved;
playerid.ukey = moveu;
 

SeraphSword

Member
Looks like that code would just change the rkey/lkey/etc from the keyboard_check to whatever value is in mover/movel/etc.

You'd most likely want something like:
Code:
if (rkey)
{
  //whatever your code is for moving right
}
 

FoufaDjo

Member
Looks like that code would just change the rkey/lkey/etc from the keyboard_check to whatever value is in mover/movel/etc.

You'd most likely want something like:
Code:
if (rkey)
{
  //whatever your code is for moving right
}
i want to change it state because the mover ect are sent by a buffer then read by the the server
so that the server player can move
 

FoufaDjo

Member
What about it isn't working? How do the move* variables function?
well i send client rkey ect on the buffer send it and then the server read it and apply tho the playerid.rkey = mover; is wrong i just need another way to activate the key

var mover = buffer_read(buffer, buffer_s8);
var playerid = ds_map_find_value(socket_to_instanceid,socket);
playerid.rkey = mover;
 

SeraphSword

Member
Let's say your normal move code in a single-player game would be something like:
Code:
rkey = keyboard_check(ord("D")); // when I press D, rkey is read as "true"

if (rkey) // if rkey is reading as "true"
{
  x += movespeed; // move character along x axis by given amount this step
}
In your case (bearing in mind I don't really know networking all that well in GMS), I might do something more like this:
Code:
// client
rkey = keyboard_check(ord("D")); // same as before

if (rkey) // same as before
{
  // add whatever code is sending the value of rkey to the server
} else
{
  // if needed, add the code to send that this is currently false
}

// server side
var mover = buffer_read(buffer, buffer_s8);  // I assume this is to read the state of rkey, which should be true or false, then storing it in mover
var playerid = ds_map_find_value(socket_to_instanceid, socket):
if (mover) // if the info I got through the buffer was "true"
{
  playerid.x += movespeed; // you put the move code on your server
}
I made some assumptions about what info exactly you are writing and reading to the buffer, but this seems logically like it would work. You may have other things going on in your code that make this invalid, but hopefully this helps.
 

Nidoking

Member
If what you're asking is how to simulate key presses based on variable values, that's keyboard_key_press and keyboard_key_release. But it's much easier just to read the variable values rather than worrying about keyboard states at all.
 
Top