GMS 2.3+ [SOLVED] Scroller won't work to change items in a shop

NTiger7

Member
IDE v. 2.3.0.529 and runtime v 2.3.0.401

So I'm trying make a shop for my game where the player presses the "w" key in the trigger area and then the shop opens up. It can display 3 items at a time and if there are more than 3 items then there is a scroller that will change the items that are displayed. The problem is that for my scroller, to calculate the percentage of the scroller (as in 0% at top and then 100% percent at the bottom), it needs to know the y coordinate of the scroller, and it can't do that when it isn't spawned in until the player interacts with it. I'm trying to make a code where if the scroller is at a certain percentage, then the items available for the player to purchase change. Here is the code for the scroller (the slider part of the scroller):

This is the slider code located in the step event:
GML:
if (!mouse_check_button(mb_left))    grab = false;
if (!grab)    exit;

else {
    if ((mouse_y + yy < oBar.bottomLimit) && (mouse_y + yy > oBar.topLimit))    y = mouse_y + yy;
    else if (mouse_y + yy > oBar.bottomLimit)    y = oBar.bottomLimit;
    else if (mouse_y + yy < oBar.topLimit)    y = oBar.topLimit;
}

percentage = round(((oSlider.y - oBar.bottomLimit) / (oBar.topLimit - oBar.bottomLimit)) * 100);
This is the slider code in the left pressed event:
GML:
grab = true;

yy = y - mouse_y;
On left release, grab = false.

Here is part of the code in the CSShop object that deals with the percentage line, in the draw GUI event:
GML:
if (CSshop_UI_bg_height <= CSshop_UI_height * ii) {
    instance_create_layer(CSshop_UI_bg_width, CSshop_UI_bg_height, "Instances", oBar);
    with (oSlider) {
        if (percentage == (other.CSshop_UI_height / other.CSshop_UI_height * shop_items)) {
            for (var i = 0; i < 3; i++) {
                inv_grid = other.ds_CSshop;
                
                yy = other.CSshop_UI_y + (i * other.CSshop_UI_height * other.scale);
                
                iItem = inv_grid[# 0, i + 1];
                sx = (iItem mod other.s_CSshop_items_columns) * other.cell_size;
                sy = (iItem div other.s_CSshop_items_columns) * other.cell_size;
                
                draw_sprite_part_ext(
                    other.s_CSshop_UI, 0, 0, 0, other.CSshop_UI_width, other.CSshop_UI_height, xx, yy,
                    other.scale, other.scale, c_white, 1
                );
                if (i = 1) {
                    draw_sprite_part_ext(
                        other.s_CSshop_items, 0, sx, sy, other.cell_size, other.cell_size, ((other.CSshop_UI_width * other.scale) + (78 * other.scale)),
                        ((52 * other.scale) + (i * (other.CSshop_UI_height - 26) * other.scale)), other.scale, other.scale, c_white, 1
                    );
                } else {
                    draw_sprite_part_ext(
                        other.s_CSshop_items, 0, sx, sy, other.cell_size, other.cell_size, ((other.CSshop_UI_width * other.scale) + (78 * other.scale)),
                        ((54 * other.scale) + (i * other.CSshop_UI_height * other.scale)), other.scale, other.scale, c_white, 1
                    );
                }
            }
        }
    }
    ii = 0;   
}
Is there a way I can make it so that the percentage will be set to zero until the player interacts with the slider? This is the only solution I can think of at this time. Thank you for any responses.
 

Nidoking

Member
Is there a reason you can't put "percentage = 0" in a Create event or similar? Because that's how everyone else does what you just asked.

If the problem is that you haven't created an instance of something that you need yet, this is why "instance_exists" exists.
 

NTiger7

Member
Is there a reason you can't put "percentage = 0" in a Create event or similar? Because that's how everyone else does what you just asked.

If the problem is that you haven't created an instance of something that you need yet, this is why "instance_exists" exists.
Ok thank you. I have not heard of instance_exists before but it seems really helpful. Now the program won't crash, but condition to change what is shown in the shop does not work. Here is the updated code:

GML:
if (CSshop_UI_bg_height <= CSshop_UI_height * shop_items) {
    if (!instance_exists(oBar)) instance_create_layer(CSshop_UI_bg_width, CSshop_UI_bg_height, "Instances", oBar);
    with (oSlider) {
        if (percentage >= round((other.CSshop_UI_height / other.CSshop_UI_height * other.shop_items) * 100)) && (percentage <= round((other.CSshop_UI_height * 2 / other.CSshop_UI_height * other.shop_items) * 100)){
            for (var i = 0; i < 3; i++) {
                //percentage = 0;
               
                inv_grid = other.ds_CSshop;
               
                yy = other.CSshop_UI_y + (i * other.CSshop_UI_height * other.scale);
               
                iItem = inv_grid[# 0, i + 1];
                sx = (iItem mod other.s_CSshop_items_columns) * other.cell_size;
                sy = (iItem div other.s_CSshop_items_columns) * other.cell_size;
               
                draw_sprite_part_ext(
                    other.s_CSshop_UI, 0, 0, 0, other.CSshop_UI_width, other.CSshop_UI_height, xx, yy,
                    other.scale, other.scale, c_white, 1
                );
                if (i = 0) {
                    draw_sprite_part_ext(
                        other.s_CSshop_items, 0, sx, sy, other.cell_size, other.cell_size, ((other.CSshop_UI_width * other.scale) + (78 * other.scale)),
                        ((52 * other.scale) + (i * (other.CSshop_UI_height - 26) * other.scale)), other.scale, other.scale, c_white, 1
                    );
                } else {
                    draw_sprite_part_ext(
                        other.s_CSshop_items, 0, sx, sy, other.cell_size, other.cell_size, ((other.CSshop_UI_width * other.scale) + (78 * other.scale)),
                        ((54 * other.scale) + (i * other.CSshop_UI_height * other.scale)), other.scale, other.scale, c_white, 1
                    );
                }
            }
        }
    }
    ii = 0;  
}
I used the debugging feature and it will always exit out of the if statement about the percentage. There was a mistake with what percentage equals, but I changed it to this, found in oSlider step event:
GML:
percentage = round(((oBar.topLimit - oSlider.y) / (oBar.topLimit - oBar.bottomLimit)) * 100);
Even when the percentage met the conditions, which I verified with the debugger, it still exited it out. Like, percentage was equal to 45, and in the above code, if percentage is equal or between 25 and 50, run this code, but it didn't. The debugger made it seem like it was going to enter but it didn't.

CSshop_UI_height = 150, which is set in the create event of the oCSshop.

Is there a reason why this if statement is not working?

Thank you for the help with my other problem though.
 

Nidoking

Member
percentage >= round((other.CSshop_UI_height / other.CSshop_UI_height * other.shop_items) * 100)
Take a closer look at this expression. It's the same as other.shop_items * 100. other.CSshop_UI_height / other.CSshop_UI_height is one. I don't know what it is you're trying to calculate, but your math is off.
 

NTiger7

Member
Take a closer look at this expression. It's the same as other.shop_items * 100. other.CSshop_UI_height / other.CSshop_UI_height is one. I don't know what it is you're trying to calculate, but your math is off.
Thank you! I forgot a pair of brackets. It should be:
GML:
if (percentage >= round((other.CSshop_UI_height / (other.CSshop_UI_height * other.shop_items)) * 100)) { //&& (percentage <= round((other.CSshop_UI_height * 2 / (other.CSshop_UI_height * other.shop_items)) * 100)){
I just commented out the other condition because there are only 4 shop items, but it works otherwise.

Thank you again Nidoking.
 

Nidoking

Member
You're still dividing other.CSshop_UI_height by other.CSshop_UI_height. This is exactly the same value as 100 / other.shop_items. Try it if you don't believe me.
 
Top