1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 [Solved]Storing keyboard_string in two variables with different length

Discussion in 'Programming' started by Nuclear Detergent, Aug 21, 2019.

  1. Nuclear Detergent

    Nuclear Detergent Member

    Joined:
    Aug 21, 2019
    Posts:
    17
    I'm having trouble wrapping my brain around strings, and especially how string_delete works.

    I'm trying to store characters from keyboard_string in two different variables with different lenghts. One of the variables is used to store only 1 character (output), which refreshes everytime something new is put into keyboard_string, while I want (variable - not yet made) to just store the whole keyboard_string. So far I've tried several approaches, but I think I don't quite grasp how string_delete works. Here's the code:

    Code:
    input = keyboard_string;
    output = string_delete(input, 1, 1);
    
    if string_length(output) > 1
          {
          output = string_delete(output, 1, 1);
          }
    This is just my latest approach, which seems to work for the first two times an input is made, but then the string (output) just adds numbers to itself, instead of adding and subtracting, going beyond 2 breaking the code.

    Any ideas would be much appreciated?
     
    Last edited: Aug 21, 2019
  2. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    417
    keyboard_string stores only 1 character which is the last key you've pressed. You don't need to create a new variable and delete the last character. Just reflesh keyboard_string every end of the Step.
    Code:
    output += keyboard_string;
    keyboard_string = "";
    
     
  3. Nuclear Detergent

    Nuclear Detergent Member

    Joined:
    Aug 21, 2019
    Posts:
    17
    I've updated to OP, to better reflect what I want to do.

    I want (output) to only store one character, which is updated with a new character, everytime something new is put into keyboard_string (but only the most recent character).
     
  4. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    417
    keyboard_string stores 1 character, you don't need to delete characters from output. Just do this:
    Code:
    output = keyboard_string;
    
     
  5. Nuclear Detergent

    Nuclear Detergent Member

    Joined:
    Aug 21, 2019
    Posts:
    17
    I think you have to elaborate for me to understand.

    If I draw keyboard_string in the GUI it grows by one character each time I input something through the keyboard.
     
  6. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,273
    Keyboard_string stores up to the last 1024 characters.

    Have you tried keyboard_lastchar, which stores the single most recent character pressed as a string?
     
    Edwin and Nuclear Detergent like this.
  7. Nuclear Detergent

    Nuclear Detergent Member

    Joined:
    Aug 21, 2019
    Posts:
    17
    I thought I remembered something about 1024 characters.

    Thanks! I didn't know _lastchar was built in - it solves the problem :)
     
    IndianaBones likes this.
  8. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    417

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice