• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

The Return statement doesn't work in the script

S

sisi_ke3ka_khokho

Guest
Hi,
In a nutshell, I want to use the scr_zx() I created besides this:
if (keyboard_check_pressed(ord('Z')) or keyboard_check_pressed(ord('X')))
so I created the scr_zx() function where i've put:
return (keyboard_check_pressed(ord('Z')) or keyboard_check_pressed(ord('X')))
then I replaced the long line with the simple function in my code, but it weirdly doesn't work, and when I change it back, it works again.
Thank you for your help.
 
Are you calling the script like this?
Code:
if (scr_zx()) //stuff
It doesn't matter what's returning if you aren't checking that.
 
K

kevins_office

Guest
then I replaced the long line with the simple function in my code, but it weirdly doesn't work, and when I change it back, it works again.
To follow up on what nacho_chicken said, he is correct the function needs to be in an if() statement.
I would also like to add the script could use a more descriptive name for when your project gets large.
It will be easier to remember a year from now what zx() does if it was named something like zxPressed().

Code:
/// @func scr_zxPressed
return (keyboard_check_pressed(ord("Z")) || keyboard_check_pressed(ord("X")));
Code:
// Step Event
if (scr_zxPressed()) {
   //
   // Do Stuff
   //
}
 
Last edited by a moderator:
To take it a step further regarding the naming of the script, what do the z and x keys represent in terms of gameplay. What if later in development you want to use different keys.

If these keys are for shooting, call the script "scr_shoot_pressed()". Then you can change the script code to other keys while the function name will still make sense from where it was called from.
 
S

sisi_ke3ka_khokho

Guest
Thank you @nacho_chicken @kevins_office for your response:
Yes, I obviously did the if statement before mentioning the script :
if scr_zx() {/*do the thing*/}
Still, it doesn't work; i'm really confused.
@IndianaBones :
Yes, you are right, I'll change that, but right now I'm stuck in the script for not being functional. Thanks.
 
I've got GMS 2, so I had to change the single quotes to double quotes in the ord() functions, but that shouldnt make a difference.

scr_zx()
Code:
var _result = keyboard_check_pressed(ord("Z")) or keyboard_check_pressed(ord("X"))
show_debug_message("Result : " + string(_result))
return ( _result )
Step Event
Code:
if ( scr_zx() )
{
    show_debug_message("ZX HIT")   
}
The above code works for me.

If nothing is pressed, Result is 0.

If Z or X is pressed, Result is 1, and the message "ZX HIT" gets printed.

Can you re-confirm the scr_zx() script you have, as well as the code calling it? For example, how do you know it doesn't work. Try putting some debug messages in the "do stuff" part after you run scr_zx().
 
S

sisi_ke3ka_khokho

Guest
Okay, scr_z function contains this:

Code:
var result=keyboard_check(ord('Z'))
show_debug_message(result)
return result
In the obj_player's Step Event, there is this line of code:

Code:
if scr_z image_blend=c_blue
Knowing that the image_blend variable was never mentioned in any line of the code, the player turns blue at the very beginning!
Sorry, I just don't get it. I moved on and used this besides:
I made a script called scr_keys_set, and it contains this:

Code:
var key_z // ... and all the keys I regularly use in the game...
key_z=keyboard_check_pressed(ord('Z'))
/*
...
And all the other keys
Then, whenever there is a code, I write scr_keys_set() at the top of the code, then start referring to it.
Although, I'd like to solve the return problem, because it still doesn't work, sorry.
 
There should be a pair of parentheses () after scr_z in your example code above.

Not sure if typo or your actual code, because you correctly used parentheses for scr_keys_set()
 
Top