Legacy GM Not starting a loop at 0 (solved)

M

markchapman

Guest
I'm having an issue with what I believe to be variable scope however it could be the way I'm assigning and calling the variable, Here is the error i get from gamemaker.
GML:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of  Step Event0
for object oCombatButton:

local variable result(100002, -2147483648) not set before reading it.
at gml_Script_diceRoll (line 10) - return result;
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_diceRoll (line 10)
called from - gml_Object_oCombatButton_StepNormalEvent_1 (line 22) -         case 0:showHitmarker(floor(room_width),floor(room_height),diceRoll(1,irandom(10)),2,c_blue,10);break;
here is the script it is referring to.
GML:
////diceRoll(diceSides,rollAmount)
var result;
randomize()
for(var i=1;i<argument0;i++){
   var result = irandom(argument1)
    if result <= 0{
        result+=1;
    }
}
return result;
This is where I'm calling the script in the step event of the object.
GML:
if keyYes{
    switch(buttonState){
        case 0:showHitmarker(floor(room_width),floor(room_height),diceRoll(1,irandom(10)),2,c_blue,10);break;
        case 1:showHitmarker(floor(room_width),floor(room_height),diceRoll(1,irandom(10)),2,c_white,10);break;
        case 2:showHitmarker(floor(room_width),floor(room_height),diceRoll(1,irandom(10)),2,c_red,10);break;
        case 3:showHitmarker(floor(room_width),floor(room_height),diceRoll(1,irandom(10)),2,c_purple,10);break;
    }
}
I'm not sure what is going on and if you need more info I Can give it to you, I thought that the scope of the script vars was for the script so why is it giving me an error about result? I need the diceRoll script to return A number so I didn't assign a variable to it, I'm not sure what is going on I hope it's something stupid simple, thanks in advanced.
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Since your loop is i=1;i<argument0;i++, running diceRoll(1, _) runs the loop 0 times and the result variable remains unset.
 

FrostyCat

Redemption Seeker
You should memorize basic book lines on counting in loops if you don't want a repeat of this.
If you don't start learning how to count properly from 0, you'll keep screwing up like this.
  • With n entries starting from 0, the last index is n-1.
  • With n entries starting from 1, the last index is n.
Hence the standard forms to iterate n times using for:
Code:
// Zero-indexed (GML arrays, lists, grids)
for (var i = 0; i < n; i++)
for (var i = n-1; i >= 0; i--)

// One-indexed (GML strings)
for (var i = 1; i <= n; i++)
for (var i = n; i > 0; i--)
The last time I ever made the mistake you did was a decade ago. The only reason I didn't make it again since then is because I've memorized basic counting patterns like this.
 
M

markchapman

Guest
Thanks guys I didn't know there would be problems starting from anything but zero, fixed my problem though.
 

Roleybob

Member
You can start the for loop setting i to any value you like (for example often for loops start with the highest value and count down instead of starting at 0 and counting up), but in your code you should have been setting to 0
 
Top