1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

GMS 2 Change Key State By variable value

Discussion in 'Programming' started by FoufaDjo, Feb 14, 2020.

  1. FoufaDjo

    FoufaDjo Member

    Joined:
    Nov 14, 2018
    Posts:
    39
    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;
     
  2. SeraphSword

    SeraphSword Member

    Joined:
    Aug 16, 2016
    Posts:
    56
    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
    }
     
  3. FoufaDjo

    FoufaDjo Member

    Joined:
    Nov 14, 2018
    Posts:
    39
    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
     
  4. BattleRifle BR55

    BattleRifle BR55 Member

    Joined:
    Jun 24, 2016
    Posts:
    1,087
    What about it isn't working? How do the move* variables function?
     
  5. FoufaDjo

    FoufaDjo Member

    Joined:
    Nov 14, 2018
    Posts:
    39
    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;
     
  6. SeraphSword

    SeraphSword Member

    Joined:
    Aug 16, 2016
    Posts:
    56
    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.
     
  7. Nidoking

    Nidoking Member

    Joined:
    Nov 20, 2019
    Posts:
    131
    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.
     

Share This Page