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

Can't assign a return to a variable

W

WilliamShakesbeer

Guest
I made (borrowed) a script for drawing buttons to gui and it works perfectly, except I cannot seem to assign a variable to the returned value.

Here is the script:

Code:
 /// @function draw_gui_button_sprite(sprite, index, x, y, width, height, main_view);
 /// @param {index} sprite    The sprite to draw
 /// @param {index} index    The sprites index
 /// @param {real}    x        The x location of the sprite
 /// @param {real}    y        The y location of the sprite
 /// @param {real}    width    The width of the sprite
 /// @param {real}    height    The height of the sprite
 /// @param {index}    main_view    The camera view for the sprite
 
 function draw_gui_button_sprite(sp, in, xx, yy, ww, hh, main_view){
    
     var vi = 0;
     if argument_count > 6 vi = argument[6];
     main_view = vi;
    
     var mx = mouse_x;
     var my = mouse_y;
    
     mx -= camera_get_view_x(view_camera[0]);
     my -= camera_get_view_y(view_camera[0]);
    
     mx *= (display_get_gui_width()) / (camera_get_view_width(view_camera[0]));
     my *= (display_get_gui_height()) / (camera_get_view_height(view_camera[0]));
    
     var on = mx > xx && mx < xx+ww && my > yy && my < yy+hh;
    
     draw_sprite_ext(sp, in, xx, yy, ww/sprite_get_width(sp),
     hh/sprite_get_height(sp), 0, c_white, 1);
    
     var r = 0 && show_debug_message("000000");
     if on r = 1 && show_debug_message("111111");
     if on && mouse_check_button_pressed(mb_left) r = 2 && show_debug_message("222222");
     if on && mouse_check_button(mb_left) r = 3 && show_debug_message("333333");
     if on && mouse_check_button_released(mb_left) r = 4 && show_debug_message("44444");
    
     return r;
 }
So then I put this code in the draw gui for the button object:

GML:
var a = draw_gui_button_sprite(spr_brite_button, 0, 250, 760, 64, 64, 0);

if a == 2 {
    
    instance_create_layer(mouse_x, mouse_y, "Instances", obj_brite_3bbl);
    show_debug_message("button was clicked");
    a = 0;
    
}


I know that the button itself works because I can get the debug message showing that r is equal to 2. However, I can't get var a to equal 2. It's clearly not getting the return r. This is my first attempt at a function since my GM2 was just updated. Can anyone tell me what I'm doing wrong? TIA
 

rytan451

Member
I'm going to add some parentheses and braces for ease of reading. I'm also going to convert all single equal compares with double equal compares (and will leave assignments with single equal alone).

GML:
/// @function draw_gui_button_sprite(sprite, index, x, y, width, height, main_view);
/// @param {index} sprite    The sprite to draw
/// @param {index} index    The sprites index
/// @param {real}    x        The x location of the sprite
/// @param {real}    y        The y location of the sprite
/// @param {real}    width    The width of the sprite
/// @param {real}    height    The height of the sprite
/// @param {index}    main_view    The camera view for the sprite
 
function draw_gui_button_sprite(sp, in, xx, yy, ww, hh, main_view) {
    var vi = 0;
    if (argument_count > 6) vi = argument[6];
    main_view = vi;
    
    var mx = mouse_x;
    var my = mouse_y;
    
    mx -= camera_get_view_x(view_camera[0]);
    my -= camera_get_view_y(view_camera[0]);
    
    mx *= (display_get_gui_width()) / (camera_get_view_width(view_camera[0]));
    my *= (display_get_gui_height()) / (camera_get_view_height(view_camera[0]));
    
    var on = mx > xx && mx < xx+ww && my > yy && my < yy+hh;
    
    draw_sprite_ext(sp, in, xx, yy, ww/sprite_get_width(sp),
      hh/sprite_get_height(sp), 0, c_white, 1);
    
    var r = 0;
    show_debug_message("000000");
    
    if (on) {
        r == 1 && show_debug_message("111111"); // Short circuit evaluation hack?
    }
    if (on && mouse_check_button_pressed(mb_left)) {
        r == 2 && show_debug_message("222222");
    if (on && mouse_check_button(mb_left)) {
        r == 3 && show_debug_message("333333");
    }
    if (on && mouse_check_button_released(mb_left)) {
        r == 4 && show_debug_message("44444");
    }
    
    return r;
}
You don't seem to be ever changing r from 0 to anything else. This is due to your use of && to perform two operations without braces, thus making assignment operations interpreted as equality operations, removing the assignment side-effect.

If you remove the && and the debug statements, the code should work as expected. Also, you may wish to nest your if statements, and use else if when checking your mouse buttons.
 
W

WilliamShakesbeer

Guest
Thanks but doesn't seem to help. I want to assign r= 2 if the mb_left is pressed. I think it's doing it because it will show the debug message "222222". It also goes back to "111111" if mouse is hovering. That part of the code seems to work. However, I can't get a to equal 2 when returned to instance create portion of the code. Here's the thing, if I switch it to if a == 1 instead, it will create the instance when hovering, just like it should, or if I switch it to 3 or 4 it works as well. Just not on 2.
 

FrostyCat

Redemption Seeker
Did you even read this part of the article I linked you to?
WRONG:
Code:
for (i = 0; i < 5; i += 1) {
  a = 5 && b = 7 && c = 9
}
RIGHT:
Code:
for (i = 0; i < 5; i += 1) {
  a = 5;
  b = 7;
  c = 9;
}
Lines like these are nonsense:
GML:
var r = 0 && show_debug_message("000000");
GML:
if on r = 1 && show_debug_message("111111");
This is what you should have done:
GML:
var r = 0;
show_debug_message("000000");
GML:
if (on) {
    r = 1;
    show_debug_message("111111");
}
 
W

WilliamShakesbeer

Guest
Yep, sure did. Still doesn't work. As I said, r returns for 1, 3, and 4. However, it doesn't return for 2 - (mouse_check_button_pressed(mb_left)).

Here's the code:

GML:
 /// @function draw_gui_button_sprite(sprite, index, x, y, width, height, main_view);
 /// @param {index} sprite    The sprite to draw
 /// @param {index} index    The sprites index
 /// @param {real}    x        The x location of the sprite
 /// @param {real}    y        The y location of the sprite
 /// @param {real}    width    The width of the sprite
 /// @param {real}    height    The height of the sprite
 /// @param {index}    main_view    The camera view for the sprite
 
 function draw_gui_button_sprite(sp, in, xx, yy, ww, hh, main_view){
    
     var vi = 0;
     if argument_count > 6 vi = argument[6];
     main_view = vi;
    
     var mx = mouse_x;
     var my = mouse_y;
    
     mx -= camera_get_view_x(view_camera[0]);
     my -= camera_get_view_y(view_camera[0]);
    
     mx *= (display_get_gui_width()) / (camera_get_view_width(view_camera[0]));
     my *= (display_get_gui_height()) / (camera_get_view_height(view_camera[0]));
    
     var on = mx > xx && mx < xx+ww && my > yy && my < yy+hh;
    
     draw_sprite_ext(sp, in, xx, yy, ww/sprite_get_width(sp),
     hh/sprite_get_height(sp), 0, c_white, 1);
    
     var r = 0;
        
    if (on) {
        r = 1;
    }
    if (on && mouse_check_button_pressed(mb_left)) {
        r = 2;
    }
    if  (on && mouse_check_button(mb_left)) {
        r = 3;
    }
    if (on && mouse_check_button_released(mb_left)) {
        r = 4;
    }
    
    return r;
    }
And here's the code that calls the function:

Code:
var a = draw_gui_button_sprite(spr_brite_button, 0, 148, 760, 64, 64, 0);

if a == 2 {
    
    instance_create_layer(mouse_x, mouse_y, "Instances", obj_brite_3bbl);
    
}
Like I said, in the above, if a==1 or 3 or 4 it works. Just not if a==2. What is it about a left mouse press?
 

FrostyCat

Redemption Seeker
It's because when a key is "pressed", it is also "held down". Therefore, whenever your r = 2; line gets to run, the way you wrote the conditions also makes sure you'll lose that value to r = 3; next. Yin-yang if blocks SUCK.
GML:
    var r = 0;
        
    if (on) {
        r = 1;
        if (mouse_check_button_pressed(mb_left)) {
            r = 2;
        } else if (mouse_check_button_released(mb_left)) {
            r = 4;
        } else if (mouse_check_button(mb_left)) {
            r = 3;
        }
    }
 
Top