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

update an instance's y position when its in the create event?

flyinian

Member
I am currently making a scroll system with the mouse wheel and so far so good. However, I can't figure out how I can go about updating an instance's y position when it's created in the create event. I don't think I can move the instance to a step event or another since it's tied to variables in the create event. If I can get the instance's y position to update with the mouse wheel then I think I can do the rest of the scroll system.

The code is below.

button_y = 0;
scroll_amount = 20;
global.scroll_perc = 0;
bar_height = sprite_height;
active = false;


//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); //I want to update this instance's y position. I've tried, "a = instance_create_layer(x,y + button_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;


/// @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);

draw_self();

//draw_sprite(spr_slider,0,x,y+button_y); //This code works with the scroll wheel but, majority of my code is coded for the instance and not for a draw event.


Thanks.
 
T

Taddio

Guest
Simply change x,y for the coordinates you want to spawn your object. The way you wrote it will spawn the slider right on top of the calling instance.
Also (unrelated) it would look a bit better if you used with(obj_slider) {} instead of a bunch of dot operators, but that's just me ;)
 

flyinian

Member
Simply change x,y for the coordinates you want to spawn your object. The way you wrote it will spawn the slider right on top of the calling instance.
Also (unrelated) it would look a bit better if you used with(obj_slider) {} instead of a bunch of dot operators, but that's just me ;)
I'd like to be able to update the y position of the slider with the mouse wheel and I don't think I can do that since it's created in the create event so, its not being created every step/second of the game. I was asking how I can get it to update every step/second. I don't know how to change the coding for the step event.

The with statement effects every instance of the object. Even if they are meant to be doing different things, right?

In the draw event I have, " draw_sprite(spr_slider,0,x,y+button_y); ". Which works for the most part but, i changed it so that it worked with my other code. I then changed the, "a = instance_create_layer(x,y+button_y, "Instances", slider);." but, it didn't do anything(i think it's because it's in the create event which only runs the code once).

I guess the simplest way to say this is that I need to update the "button_y" variable with the slider object so I am able to scroll with the scroll wheel, mouse drag and the mouse click anywhere on the slide bar. The only one that doesn't work is the scroll wheel. It was originally in the draw event but, I didn't want it to draw it but rather create an instance of it. I don't know how to code it or alter it so that it works.
 
You would use with (a) instead of with (obj_slider) as you have already captured the instance ID of the slider in the variable a beforehand. You do NOT want to be creating multiple instances for this, that's the wrong way to think about it.
Code:
/// @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);

a.y = button_y; // Update the slider instances y variable to correspond to button_y, if you need it relative to the creator instances y, just use y+button_y instead
Here, the y variable of whatever slider was created by this instance gets updated to the button_y variable. Scroll bars are not as complicated as you seem to be trying to make it btw, its position should simply be a percentage of the scrolled height compared to the total height. You do not need to be creating multiple scroll instances for the positioning, and if you need to be able to change the button_y variable from WITHIN the slider instance, you simply store the "parent" (obviously it's not a -true- parent) instance's ID inside of the slider and use that:
Code:
a = instance_create_layer(x,y, "Instances", slider); //I want to update this instance's y position. I've tried, "a = instance_create_layer(x,y + button_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; // If you are using y + button_y to create the instance above, this line will revert that change? did you try removing this line when you did that?
a.depth = depth - 1;
a.master = id; // Now the slider has the instance ID of whatever instance created it inside a variable called master
If you want something updated in master instance from within the slider instance, you can use the dot operator or a with statement on the master variable and you have access to it's creator.
 

flyinian

Member
You would use with (a) instead of with (obj_slider) as you have already captured the instance ID of the slider in the variable a beforehand. You do NOT want to be creating multiple instances for this, that's the wrong way to think about it.
Code:
/// @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);

a.y = button_y; // Update the slider instances y variable to correspond to button_y, if you need it relative to the creator instances y, just use y+button_y instead
Here, the y variable of whatever slider was created by this instance gets updated to the button_y variable. Scroll bars are not as complicated as you seem to be trying to make it btw, its position should simply be a percentage of the scrolled height compared to the total height. You do not need to be creating multiple scroll instances for the positioning, and if you need to be able to change the button_y variable from WITHIN the slider instance, you simply store the "parent" (obviously it's not a -true- parent) instance's ID inside of the slider and use that:
Code:
a = instance_create_layer(x,y, "Instances", slider); //I want to update this instance's y position. I've tried, "a = instance_create_layer(x,y + button_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; // If you are using y + button_y to create the instance above, this line will revert that change? did you try removing this line when you did that?
a.depth = depth - 1;
a.master = id; // Now the slider has the instance ID of whatever instance created it inside a variable called master
If you want something updated in master instance from within the slider instance, you can use the dot operator or a with statement on the master variable and you have access to it's creator.
You would use with (a) instead of with (obj_slider) as you have already captured the instance ID of the slider in the variable a beforehand. You do NOT want to be creating multiple instances for this, that's the wrong way to think about it.
Code:
/// @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);

a.y = button_y; // Update the slider instances y variable to correspond to button_y, if you need it relative to the creator instances y, just use y+button_y instead
Here, the y variable of whatever slider was created by this instance gets updated to the button_y variable. Scroll bars are not as complicated as you seem to be trying to make it btw, its position should simply be a percentage of the scrolled height compared to the total height. You do not need to be creating multiple scroll instances for the positioning, and if you need to be able to change the button_y variable from WITHIN the slider instance, you simply store the "parent" (obviously it's not a -true- parent) instance's ID inside of the slider and use that:
Code:
a = instance_create_layer(x,y, "Instances", slider); //I want to update this instance's y position. I've tried, "a = instance_create_layer(x,y + button_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; // If you are using y + button_y to create the instance above, this line will revert that change? did you try removing this line when you did that?
a.depth = depth - 1;
a.master = id; // Now the slider has the instance ID of whatever instance created it inside a variable called master
If you want something updated in master instance from within the slider instance, you can use the dot operator or a with statement on the master variable and you have access to it's creator.

It works. Now I have to figure out how to get my items to scroll using the mouse wheel and to figure out why my slider won't go to clicked on slide bar location correctly.
 

flyinian

Member

It works. Now I have to figure out how to get my items to scroll using the mouse wheel and to figure out why my slider won't go to clicked on slide bar location correctly.

Thanks.

p.s. I think I need to find the delete functions...
 
Top