hidden rule?

G

gibberingmouther

Guest
i understand most of this scroll bar code but i don't understand how the variable barOffsetY is used (use control f to find it - you don't need to read the whole code). it's used within the statement: dragY=clamp(mouse_y-barOffsetY,listY+buttonsH,buttonY[1]-b);

the reason i'm confused is that it isn't defined before being used here (it's also used again later in the code). normally this would give a compiler error but this code works. what am i missing? is there a hidden rule or something that applies here?


Create event:
Code:
inventoryList[0]="Sword<ID:0>";
inventoryList[1]="Pen<ID:1>";
inventoryList[2]="Bag<ID:2>";
inventoryList[3]="Apple<ID:3>";
inventoryList[4]="Mace<ID:4>";
inventoryList[5]="Armor<ID:5>";
inventoryList[6]="Buckler<ID:6>";
inventoryList[7]="Arrow<ID:7>";

inventoryTotal=8;

itemsShow=5;//no of items to show
itemCurrent=0;//current first item to show

listX=120;
listY=120;
listW=140;
listH=200;

itemsOffset=5;

barW=30;

buttonsW=barW;
buttonsH=24;

buttonX[0]=listX+listW;
buttonY[0]=listY;

buttonX[1]=listX+listW;
buttonY[1]=listY+listH-buttonsH;

barH=listH-buttonsH*2;

barSelected=false;

Draw event:
Code:
draw_set_font(fontMain);

var cl;
cl[0]=c_black;
cl[1]=c_red;

//draw list
draw_rectangle(listX,listY,listX+listW,listY+listH,true);

//draw list content

for(var A=0;A<itemsShow;A++){
     //this is going to show the inventory list
    var itemH=listH/itemsShow;
   
    var mouseWithin=point_in_rectangle(mouse_x,mouse_y,listX,listY+A*itemH,buttonX[0]-1,listY+(A+1)*itemH-2);

    draw_set_valign(fa_center);
    //draw list item red if it's hovered
    draw_set_colour(cl[mouseWithin]);     
    draw_text(listX+itemsOffset,listY+A*(listH/itemsShow)+itemH/2,inventoryList[A+itemCurrent]);
   
    //if clicked, show a message
    if mouse_check_button_pressed(mb_left) && mouseWithin{
        show_message("You clicked "+inventoryList[A+itemCurrent]);
    }
}
draw_set_colour(c_black);

//draw bar
draw_rectangle(listX+listW,listY,listX+listW+barW,listY+listH,true);

//draw top button

var mouseWithin=point_in_rectangle(mouse_x,mouse_y,buttonX[0],buttonY[0],buttonX[0]+buttonsW,buttonY[0]+buttonsH);

draw_set_colour(cl[mouseWithin]);
draw_rectangle(buttonX[0],buttonY[0],buttonX[0]+buttonsW,buttonY[0]+buttonsH,true);
draw_sprite(imageScroll,1,buttonX[0]+buttonsW/2,buttonY[0]+buttonsH/2);


if mouse_check_button_pressed(mb_left) && mouseWithin{

    itemCurrent=clamp(itemCurrent-1,0,inventoryTotal-itemsShow);
}

//draw bottom button

var mouseWithin=point_in_rectangle(mouse_x,mouse_y,buttonX[1],buttonY[1],buttonX[1]+buttonsW,buttonY[1]+buttonsH);


draw_set_colour(cl[mouseWithin]);
draw_rectangle(buttonX[1],buttonY[1],buttonX[1]+buttonsW,buttonY[1]+buttonsH,true);
draw_sprite(imageScroll,0,buttonX[1]+buttonsW/2,buttonY[1]+buttonsH/2);



if mouse_check_button_pressed(mb_left) && mouseWithin{

    itemCurrent=clamp(itemCurrent+1,0,inventoryTotal-itemsShow);
}



//get some coords
var dragY,dragR,itemsMax,h,p,b;
itemsMax=max(itemsShow,inventoryTotal);
h=barH/itemsMax;//Item's height
p=h*(itemsMax-itemsShow);//Scroll's
b=h*itemsShow;//Bar's size

if barSelected{
    dragY=clamp(mouse_y-barOffsetY,listY+buttonsH,buttonY[1]-b);
   
    itemCurrent=round((dragY-(listY+buttonsH))/h);
}
else{
    dragY=listY+buttonsH+(itemCurrent/max(1,itemsMax-itemsShow))*p;
}


//check if the bar was clicked
var mouseWithin=point_in_rectangle(mouse_x,mouse_y,buttonX[0],dragY,buttonX[0]+barW,dragY+b+1)
var mouseWithin2=point_in_rectangle(mouse_x,mouse_y,listX,listY,listX+listW,listY+listH)

if mouse_check_button_pressed(mb_left) && mouseWithin{
    barSelected=true;
    barOffsetY=mouse_y-dragY;
}
else if mouse_check_button_released(mb_left){
    barSelected=false;
}
else if mouseWithin2{
    //manipulate and clamp the current first item
    itemCurrent=clamp(itemCurrent+mouse_wheel_down()-mouse_wheel_up(),0,inventoryTotal-itemsShow);
}

//Draw scrollbar

draw_set_colour(cl[barSelected]);//set colour according to barSelected
draw_rectangle(buttonX[0],dragY,buttonX[0]+barW,dragY+b,true);
draw_set_colour(c_black);

draw_sprite(imageScroll2,barSelected,buttonX[0]+barW/2,dragY+b/2);
 
V

Variss

Guest
looks like it technically is being defined before it's used, in the section here:

Code:
if mouse_check_button_pressed(mb_left) && mouseWithin{
   barSelected=true;
    barOffsetY=mouse_y-dragY;
}
The code won't actually enter into that section you quoted until barSelected is set to True, which is first done in this line.
 
G

gibberingmouther

Guest
yay! i get it now. i guess i didn't see that because the definition came AFTER the first use of the variable. it comes after in the code, but before if you actually use the scroll bar.
 
Top