Windows Toggle Variable in script issues

J

Jdown79

Guest
Hey guys,
I'm trying to make a toggle variable, so that when you click once, it turns the variable on, and then if you click again, its off, so on, so forth.
However the script I have made has stopped function of everything, and I'm not sure if I'm on the right path to getting this working.
The variable I'm trying to toggle is global.tieroneequipped, from 0 to 1
Thanks in advance

Code:
if global.tieronepurchased = 1
    {
    if distance_to_point(mouse_x,mouse_y) <=0
        {
        if mouse_check_button(mb_left)
            {
            global.tieroneequipped = 1
            image_index = gunequipped;
            }
        }
    }
   
if global.tieroneequipped = 1
    {
    if distance_to_point(mouse_x,mouse_y) <=0
        {
        if mouse_check_button(mb_left)
            {
            global.tieroneequipped = 0;
            image_index = gun;
            }
        }
    }
 

Roderick

Member
If the first block is true, it sets your variable to true, making the second block automatically true, which means that the second block will ALWAYS run if either block is true.

You need to restructure your code; moving the second block above the first should probably work.
 
A

Aura

Guest
Using distance_to_point() is a bad idea. Use position_meeting() instead.

Code:
if (mouse_check_button_pressed(mb_left) && position_meeting(mouse_x, mouse_y, id)) {
   variable = !variable;
}
Note how I'm using the not operator which would set the variable to true (1) if it is false (0) and to false if it is true.
 
J

Jdown79

Guest
Okay so I changed a few things, I flipped the code, and removed distance_to_point()
Code:
if global.tieronepurchased = 1

    {
    image_index = gun;
    }
   
if global.tieroneequipped = 1
    {
    if position_meeting(mouse_x,mouse_y,self)
        {
        if mouse_check_button_pressed(mb_left)
            {
            global.tieroneequipped = 0;
            image_index = gun;
            }
        }
    }



if global.tieronepurchased = 1
    {
    if position_meeting(mouse_x,mouse_y,self)
        {
        if mouse_check_button_pressed(mb_left)
            {
            global.tieroneequipped = 1
            image_index = gunequipped;
            }
        }
    }
This doesn't toggle though, when I click, the change happens, but it instantly reverts back.

@Aura, sorry, I'm not understanding how that works, nor how to implement it. I am a novice, after all :p
Any chance you could explain it a bit more?
 

NightFrost

Member
The basic code structure for making a flip-flop variable is
Code:
if(variable == 1){
    variable = 0;
} else {
    variable = 1;
}
 

Yal

🐧 *penguin noises*
GMC Elder
Or even simpler if you use the xor operator...
Code:
variable ^= 1;
 
J

Jdown79

Guest
Okay, I finally got that one working.
Code:
if global.tieronepurchased = 1
{
if global.tieroneequipped = 0


    {
    image_index = gun;
    }
}


if global.tieronepurchased = 1
    {
    if position_meeting(mouse_x,mouse_y,self)
        {
        if mouse_check_button_pressed(mb_left)
            {
            if(global.tieroneequipped == 1)
            {
            global.tieroneequipped = 0;
            image_index = gun;
            } 
            else 
            {
            global.tieroneequipped = 1;
            image_index = gunequipped;
            }
         
            }
        }
    }
Thanks alot for the help.
@Yal, can you give me a reference link for the XOR operator? I'd like to read into it
 

Yal

🐧 *penguin noises*
GMC Elder
I assumed that there was some reason for the more complex code though, so I didn't bother addressing the overcomplexity.
You only need to type the variable name once with the XOR method, and if you use descriptive variable names (instead of single-letter ones, which is a bad idea for anything other than a temporary script variable) that's a boon.
 
J

Jdown79

Guest
@Yal At the moment I'm really just trying to see how complex of a game I can make. That being said, I'm trying to make sure I know what I've written, so I'm using descriptive variables just to get me by at the moment.
Is it actually a bad thing? What will it effect in the long run?
 
A

Aura

Guest
If you want to learn how the ^ operator works, here's a pretty good article by Mike Dailly:

http://www.yoyogames.com/blog/46

Either way, the main motive of my post was to make you aware of the fact that you need not call the same function multiple times. For instance, you'd want to use mouse_check_button_pressed() and position_meeting() only once. You should also learn the usage of the && operator and how to structure if statements properly.

Code:
if (mouse_check_button_pressed(mb_left) && position_meeting(mouse_x, mouse_y, id)) {
   if (variable == 1) {
      variable = 0;
      image_index = gun;
   }
   else {
      variable = 1;
      image_index = gunequipped;
   }
}
That would make the code look neater and easier to trace. Also, are you sure that it is image_index you want to use? I guess you want sprite_index instead; unless gun and gunequipped are variables that hold image indexes.
 

Yal

🐧 *penguin noises*
GMC Elder
Is it actually a bad thing? What will it effect in the long run?
If you use the same variable names all over the place [in the same object], you risk having later code interfere with old code's variable names, breaking code that previously worked. Bugs like that are really hard to track down. That's why I try to keep variable names descriptive. Plus, it's easier to understand what code you wrote ages ago does if you made it readable... you forget how code works much faster than you think.

Optimization-based, though... no real change. Variable names get changed to abstract tokens on compilation, anyway (or to be precise they get changed to memory address references, and all those has the same length, a system Word). IMO, you shouldn't worry about optimization if you're a new programmer. Don't look for the best way to do things, any way that works is enough... many of the best ways are situational or require more upkeep/work than the 'worse' ways. Don't optimize your code unless you start noticing a need for optimization (e.g. lag or excessive load times).
 

NightFrost

Member
Descriptive variable names are a must in larger projects. If you write a nice little ds grid accessor with several enum pointers in the middle of your code in the style of: var a = global.b[# c.d, e.f]... after several days you'll have no idea what that grid contains and what the enums pointing into it mean. Descriptive variables would make the operation obvious: var Price = global.Items[# Item_Pointer.Dagger, Item_Data.Price].
 
  • Like
Reactions: Yal
J

Jdown79

Guest
Thanks all for your information, I'll take it all onboard.
@Aura , I used image_index because the way I was profiling my sprites was like this:
There were five "perk" trees of the character, so I made five sprites for each class.
Each sprite had eight or so images inside them, each a different weapon-perk specific.
sprite_index was used to define the perk, image_index to define the weapon equipped.
That was the most logical way I could think of doing it, if you can think of a better way, please do inform me, I'm always eager to learn, self-teaching has only gotten me so far.
Thanks again all.
 
Top