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

Complete If Statement Wont Run In Some Objects

I

ItzJamPakt

Guest
Okay, so im using this EXACT set of code in 3 different objects, however it only runs completely through in the object txt_bot_name. in the other 2 objects, (names are really irrelevant as they are never referenced in any code yet), they still run the first line of the outer-most if statement, "sprite_index=spr_text_box_selected", and also all of the code within the else statement, however none of the other if statements are checked. They work perfectly fine the object i mentioned, but these other 2 just wont work.

Here is the code:
Code:
if (place_meeting(x,y,obj_mouse)) {
    sprite_index=spr_text_box_selected
    if(keyboard_check(vk_anykey) and string_length(text) < 20) {
        text = text+string(keyboard_string);
        keyboard_string = "";
    }

    if(keyboard_check(vk_backspace) and !keyboard_check_pressed(vk_backspace) and delete_timer = 2) {
        text = string_delete(text,string_length(text),1);
        delete_timer = 0;
        keyboard_string = "";
    }

    if(keyboard_check_pressed(vk_backspace)) {
        text = string_delete(text,string_length(text),1);
        keyboard_string = "";
        delete_timer = -4;
    }
    if(keyboard_check_pressed(vk_enter)) {
        global.oauth = text
        text = ""
        keyboard_string = "";
    }
    //Handle Timer Update
    if(delete_timer != 2) {
        delete_timer ++;
    }
}
else {
    sprite_index=spr_text_box
    keyboard_string=""
}
 
I

ItzJamPakt

Guest
Also, to clairify, the only difference between the 3 objects is the global variable affected under one of the if statements, but that shouldn't matter, and im trying to create a text input box. three different ones that assign three different variables to be specific. (just thought of this, and will try it, but i could put them all under a parent object, and leave minimal code in each one. will try that and return)
 
I

ItzJamPakt

Guest
well, i can now confirm that attempting to put them under a parent completely broke everything... so going back to my original code
 
C

CptChuckles

Guest
IIRC, the keyboard_check functions eat the keyboard input. So whichever instance is running first causes the checked keyboard input to be eaten, and then there's no more keyboard input seen by any objects that run subsequently that step. All of the code is probably running and checking just fine, but the absence of keyboard input after the first object runs is causing it to look like it's not executing.

I suggest you only put keyboard_check calls in one object, like a control object, and put it in the Begin Step event, and have all your textbox objects ask for controlObject.key_whatever etc etc. instead of using keyboard_check calls themselves.

EDIT: i just realized none of the keyboard_check calls are made unless it's moused-over, so there goes my theory :c It's still worth abstracting those calls out, though, just as a preventative measure to ensure that nothing is eating keyboard input.

EDIT 2: Maybe this is the problem:
Code:
else {
   sprite_index=spr_text_box
   keyboard_string=""
}
The very first object that doesn't have focus erases keyboard_string. Maybe that erases everything that keyboard_check would have seen, too, preventing all subsequently-executed instances from seeing any keyboard input?
 
Last edited by a moderator:

TheouAegis

Member
keyboard check Carl's work at all times. The keyboard is not get cleared unless you actually specifically clear it yourself or if the current step has completely ended then, including all of the draw events. The only way to clear the keyboard directly is to use the function io_clear(). seriously, if YYG actually made the keyboard clear out every time you used keyboard_check, they would be the biggest bunch of idiots in programming history, well below the guys that made Action 52.

Anyway, as the second edit touched upon, keyboard_string is a global variable. that means you need that part of your code to be INSIDE the code that checks if the mouse is over the object. If the mouse is not over the object, we shouldn't be even bothered by the presence of that object at all, right? so if the condition to concern ourselves with the text box object has not been met, then there should be no code run at all for that text box object.

Edit: if you want the keyboard_string to be cleared every step, then put that code o_ending sniopet of code in the end step event. It's also means you wouldn't need to actually clear keyboard_string with every key press, since it will be cleared at the end of each step.

Also, unless they changed it, I am pretty sure the backspace key affects the keyboard_string, so it is essentially deleting two characters at a time if the player manages to actually pull that off. It is also entirely possible to clear the keyboard_string, then on the next step have four characters in the keyboard_string. This is just a heads up. It's unlikely you'll encounter it, though.
 
I

ItzJamPakt

Guest
Thanks for the suggestion! It turns out that it actually WAS the position in which I had placed “keyboard_string”, and I only had to move it to the end of the inside of the If statement, rather than putting in the else. It was because keyboard_string is likely a global variable that stores the same result for ALL objects, and I had it in place to stop text from saving when no input was selected. However, since the first generated object in the room, txt_bot_name, was not selected, it set the variable keyboard_string to an empty string, therefore breaking the system.
 
Top