Change/update two object's variables simultaneously?

flyinian

Member
I believe I have narrowed one of my issues down to the below line of code.

I am making a scroll system and got help to get the scroll wheel to work(it broke the click on scroll bar but, ill deal with that later). The items in the scroll menu wont scroll when I use the mouse wheel(but the slider will scroll). The items in the scroll menu will scroll if I click and drag.

I believe I need to add obj_scrollbar.percentage in the below line of code for it to scroll the items that are in the scroll menu.

Is there a way to write the below line of code that will change and update both the obj_slider.percentage and obj_sliderbar.percentage simultaneously? The percentage variables do the same things but, used in two different objects if that makes any difference.

y = ystart + ((global.areaHeight/100 * obj_slider.percentage) - global.areaHeight);


I don't think you'll need any other code but, if you do let me know.

Thanks.
 

TsukaYuriko

☄️
Forum Staff
Moderator
You can't change two variables simultaneously, but you can store the result of a calculation in a temporary storage variable and then set two other variables to the value of the temporary storage variable sequentially.
 

flyinian

Member
You can't change two variables simultaneously, but you can store the result of a calculation in a temporary storage variable and then set two other variables to the value of the temporary storage variable sequentially.
Im not sure on how to do that. Im still learning code. I understand what you said but, implementing it into code is the difficulty of it.

Would i use var to temporarily store the "percentage" variables?

Am I able to store two object references in a single var?

Im not at my pc yet so, i havent done any pre testing before making this.
Ex: var abc = obj_slider.percentage, obj_slidebar.percentage;

Thats how i see it.

Thanks
 

TsukaYuriko

☄️
Forum Staff
Moderator
Im not sure on how to do that. Im still learning code. I understand what you said but, implementing it into code is the difficulty of it.
No problem - helping with that is what the GMC is here for. :)

Am I able to store two object references in a single var?
You can't store more than one value in a variable. (I think you meant "variable reference" according to your code - and no, you can not store even a single variable reference, as variables in GML can not be passed by reference, only by value.)

Ex: var abc = obj_slider.percentage, obj_slidebar.percentage;
What you seem to be trying to do is storing two variable references in one variable (and then presumably setting them to the result of a calculation).

Instead, store the result of the calculation (single value) in the temporary variable first. After that, set the two variables to the stored result (first set the first variable to the result, then set the second one to the result, on two separate lines).

Would i use var to temporarily store the "percentage" variables?
It's the other way - use var to temporarily store the result of the calculation and then set the variables to said variable's value. Like this:
Code:
var result = 1 + 1;

instance1.variable = result;
instance2.variable = result;

On a slightly related note: If these two values will always be the same no matter what (unsure if that's the case - that's how I interpreted the opening post), wouldn't it be easier to store it only once and then use that single variable from both locations in which it needs to be used? This way, you won't have to store and update them twice.
 

flyinian

Member
I've spent a few hours today trying to get the code to work so, i'm burned out for today. I've read your last reply, although I believe I understand it, it doesn't help with my current issue.

I'm giving the events and code for the scroll system. If you're interested or if someone else may be interested in helping more.

Current Issues that i'm aware of.
The items wont scroll when the mouse wheel is used.
Also, the click on scroll bar doesn't work right.

The current function that im trying to have.
  • Scroll with mouse wheel.(broken)
  • Click on scroll bar and go to that location.(broken)
  • Secluded scroll menus so, that I can have more than one on screen and the player may only scroll one menu at a time instead all of them.(implemented but, not adequately tested)
  • Click and drag the slider.(implemented)


I believe my issues is with the "a.y" variable when it comes to the click on scroll bar function(The slider doesn't go where I clicked on the scroll bar.) and possibly the scroll wheel.
-----
-THE SCROLL BAR-
/// @description Insert description here
button_y = 0;
scroll_amount = 20;
global.scroll_perc = 0;
bar_height = sprite_height;
active = false;
yy = 0;
percentage = 100;

//instance_create_layer(x,y, "Instances", obj_slider2);
slider = obj_slider2;
//TopLimit = y - (sprite_height/2) + (image_xscale*75);
//BottomLimit = y + (sprite_height/2) - (image_xscale*75);
TopLimit = y - (sprite_height/2) + (image_xscale);
BottomLimit = y + (sprite_height/2) - (image_xscale);
a = instance_create_layer(x,y, "Instances", slider);
a.image_xscale = image_xscale;
a.image_yscale = image_yscale;
a.BarLength = sprite_height;
a.TopLimit = TopLimit;
a.BottomLimit = BottomLimit;
//a.y = TopLimit;
a.depth = depth - 1;
//test
a.master = id;

/// @description Insert description here
global.scroll_perc = button_y/a.BarLength;
if active = false
{
if mouse_wheel_up()
{
if mouse_x < x
{
button_y -= scroll_amount;
}
}
if mouse_wheel_down()
{
if mouse_x < x
{
button_y += scroll_amount;
}
}
}
else if active = true
{
button_y = mouse_y - y;
}
button_y = clamp (button_y,0, a.BarLength);
//test
a.y = button_y; //I believe this is conflicting with the "a.y" in the "left pressed" event.
percentage = round(((y-BottomLimit)/(TopLimit-BottomLimit))*100);

if active = false
{
active = true;
}
if(mouse_y < BottomLimit) && (mouse_y > TopLimit)
{
a.y = mouse_y;
}

/// @description Insert description here
if active = true
{active = false;}
-----
-SLIDER-
/// @description Insert description here
yy = 0;
grab = false;
percentage = 100;

/// @description Insert description here
if(!mouse_check_button(mb_left))
{
grab = false;
}
if(!grab)
{
exit;
}
else
{
if((mouse_y + yy < BottomLimit) && (mouse_y + yy > TopLimit))
{
y = mouse_y + yy;
}
else if (mouse_y + yy > BottomLimit)
{
y = BottomLimit;
}
else if(mouse_y + yy < TopLimit)
{
y = TopLimit;
}
}
percentage = round(((y-BottomLimit)/(TopLimit-BottomLimit))*100);

/// @description Insert description here
grab = true;
yy = y - mouse_y;

/// @description Insert description here
grab = false;
//test
-----
-ITEMS-
/// @description This would be the items that are in the menu that scrolls. I believe this is where one of my issues is located and the original code that was in question.
y = ystart + ((global.areaHeight/100 * obj_slider2.percentage) - global.areaHeight);
-----
-CONTROLLER-
var areaHeight = 0;
var maxwidth = 5; //items in a row
var maxheight = 10; //items in a column
//var xx = 100;
//var yy = 125;
var xx = 0;//65; //spacing of items. Will add extra spacing.
var yy = 0;//65; //spacing of items. Will add extra spacing.
for (var i = 0; i < maxwidth; i++;)
{
for(var j = 0; j < maxheight; j++)
{
//instance_create_layer(xx, yy, "Instances", obj_item);

if (j == maxheight - 1)
{
yy += 0;//65;
}
else
{
yy += 0;//65;
}
}
areaHeight = yy;
yy = 10000; //allows longer scrolls. Sets the max scroll height.
xx += 65;
}
global.areaHeight = areaHeight - room_height;
----
That would be all the code that I believe is needed for the scroll system. The only thing that i didn't add would be the placements of the items. I hope this is enough and that I can get past this scroll system and not scrap it.

Thanks again.
No problem - helping with that is what the GMC is here for. :)


You can't store more than one value in a variable. (I think you meant "variable reference" according to your code - and no, you can not store even a single variable reference, as variables in GML can not be passed by reference, only by value.)


What you seem to be trying to do is storing two variable references in one variable (and then presumably setting them to the result of a calculation).

Instead, store the result of the calculation (single value) in the temporary variable first. After that, set the two variables to the stored result (first set the first variable to the result, then set the second one to the result, on two separate lines).


It's the other way - use var to temporarily store the result of the calculation and then set the variables to said variable's value. Like this:
Code:
var result = 1 + 1;

instance1.variable = result;
instance2.variable = result;

On a slightly related note: If these two values will always be the same no matter what (unsure if that's the case - that's how I interpreted the opening post), wouldn't it be easier to store it only once and then use that single variable from both locations in which it needs to be used? This way, you won't have to store and update them twice.
 
Top