Jump to content


Photo

How to choose which variable is changed


  • Please log in to reply
6 replies to this topic

#1 Kululu17

Kululu17

    GMC Member

  • GMC Member
  • 71 posts
  • Version:GM:Studio

Posted 28 March 2016 - 03:43 PM

This is probably an easy one, but how can you reference a specific variable that is to be changed within the code?

 

I am trying to create a generic button that I can use in various place in the GUI - I want it to increase or decrease certain parameters. The increasing or decreasing is easy, you just assign a + or - number using the with command when you create the button. But how can I reference WHICH variable gets changed. Here is a little code to explain it better - note that this code does not work, and I know exactly why it does not work, but I don't know the correct code:

 

Create code:

Control = -1;//What does it control
High_Low = "High";//increase or decrease control?
Increment = .1;//How much does it change
Upper_Limit = 1;
Lower_Limit = 0;
Highlighted = false;
image_speed = 0;

Step event (just the basics, no checking for upper/lower limits, etc.)

if position_meeting(mouse_x,mouse_y,id) {
    Highlighted = true;
    //Code to adjust
    if mouse_check_button_pressed(mb_left) == true && Control != -1 {
        if High_Low == "High" {
            Control += Increment;
            }
    
        }
    }
    else {Highlighted = false}

The intention is that this object be created, and the desired parameters be passed to it using the with command, e.g.

Button_1 = instance_create(100,100, OBJ_BTN);
with Button_1 {
    Control = Sound_Volume;
    High_Low = "High";
    }

For example this one would use the button to increase the variable "Sound_Volume". But it won't work. All it does is pass the current value of the variable "Sound_Volume" to the variable "Control", and then "Control" is increased with the button. So what code is needed to get it to actually push the change to the variable "Sound_Volume"?

 

Clear as mud?

 

Thanks!


  • 0

#2 RekNepZ

RekNepZ

    GMC Historian

  • GMC Member
  • 718 posts
  • Version:GM:Studio

Posted 28 March 2016 - 04:31 PM

Wouldn't that just be "sound_volume = control;" ? Not sure if I quite understand the question.


  • 0

AczgxAZ.png

^kept in my sig for historical purposes^


#3 Kululu17

Kululu17

    GMC Member

  • GMC Member
  • 71 posts
  • Version:GM:Studio

Posted 28 March 2016 - 05:04 PM

Hi - thanks for the response.

 

That would be a correct solution for my example, however if you don't know in advance what variable it is that you want to change, you can't use that method. It might be Sound_Volume, or Music_Volume, or Game_Difficulty, or whatever. As a work around, I could just make a new button object every time I wanted to control a different variable, but then I would have dozens of buttons each with 98% the same code. I can make a single generic button, and use the with statement to change all the other parameters I need, except which specific variable it is that I want that instance of the button to change; and unfortunately that is the most important part...


  • 0

#4 RekNepZ

RekNepZ

    GMC Historian

  • GMC Member
  • 718 posts
  • Version:GM:Studio

Posted 28 March 2016 - 08:22 PM

Hmm. The best I can come up with would be to assign each variable setting its own id and have that called when creating the button.
Eg:
When the button's created

Button_1 = instance_create(100,100, OBJ_BTN);
with Button_1 {
    Var_Type = 1
    }

And then in the click event of the button (or whatever you use)

switch (Var_Type)
{
  case 1 Sound_volume = "high"; break;
  case 2 blahblah = blahblag;   break;
//etc
}

This should work, but I'll ask other people to take a look (I've sort of been wondering about this too).
 
 
Edit: Alternatively, it was suggested to me that you could use maps, which could be a little easier to handle.


Edited by RekNepZ, 28 March 2016 - 09:12 PM.

  • 1

AczgxAZ.png

^kept in my sig for historical purposes^


#5 Kululu17

Kululu17

    GMC Member

  • GMC Member
  • 71 posts
  • Version:GM:Studio

Posted 28 March 2016 - 11:29 PM

Hey, thanks a lot. That would help in this case, so that I don't need 20 buttons.

 

I have had this issue for other parts of the program as well. Is there a more generic way that you can, sort of "push" a variable into an object. The situation this would come in handy is when an object uses a (not-predefined) variable many times. For example once you have defined a variable that is the "key" for an object it would be used in the step event (where you would push changes to the variable), the draw event(where you would pull the value from the variable), etc. I know I am not explaining it well, but it seems I run into the situation a lot where I have a very good idea of WHAT I want to do with a variable, I just want some flexibility in defining WHICH VARIABLE I am doing the stuff to. Very scientific, I know :-)

 

Anyway, thanks for the help.


  • 0

#6 flyingsaucerinvasion

flyingsaucerinvasion

    GMC Member

  • GMC Member
  • 720 posts
  • Version:GM:Studio

Posted 28 March 2016 - 11:49 PM

The only way I know of doing this in GML is to use arrays or ds structures.

 

Example:

 

controls[VOLUME] = 1.0;

controls[GAMMA] = 0.5;

controls[MOUSE_SENSITIVITY] = 0.2;

 

VOLUME, GAMMA, MOUSE_SENSITIVITY are macros with values like 0, 1, and 2.

 

So then when you are creating a new button, you can asign which variable it "controls" like this:

 

Control = VOLUME;

 

and then in the step event:

 

controls[Control] += Increment;


Edited by flyingsaucerinvasion, 29 March 2016 - 12:56 AM.

  • 1

#7 Kululu17

Kululu17

    GMC Member

  • GMC Member
  • 71 posts
  • Version:GM:Studio

Posted 29 March 2016 - 12:53 AM

Thanks, I'll have to try that out!


  • 0