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

GameMaker [SOLVED] Array Trouble

D

Drago the Shinigami

Guest
Hello,

I'm currently working on a script to determine turn order for a turn-based battle system. Unfortunately, I keep getting the same error message, and I can't see how it's happening.

Here's the code for the script:
Code:
global.BattleOrder = 0
var templist = array_create(instance_number(Battler_Template))
//create list
var inst = 0
for(var i = 0; i < instance_number(Battler_Template); i++)
{
    var tempid = instance_find(Battler_Template,i)
    array_set(templist, i, tempid)
}
//sort list
for(var j = array_length_1d(templist); j > 0 ;j--)
{
    show_debug_message("j = " + string(j))
    for(var k = 0; k < j; k++)
    {
        var foundhome = false
        var speed1a = array_get(templist, j).AGL
        var speed1b = array_get(templist, j).Action_Speed
        var speed2a = array_get(templist, k).AGL
        var speed2b = array_get(templist, k).Action_Speed
        if(speed1a + speed1b <= speed2a + speed2b && !foundhome)
        {
            var temp = array_get(templist, j)
            array_set(templist, j, array_get(templist, k))
            array_set(templist, k, temp)
            foundhome = true
        }
    }
}
//finalize list
global.BattleOrder = array_create(array_length_1d(templist))
for(var i = 0; i < array_length_1d(templist); i++)
{
    array_set(global.BattleOrder, i, array_get(templist, i))
}
And here's the error message:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Other Event: User Defined 0
for object Fight:
array_get :: Index [0,19] out of range [1,19]
at gml_Script_DetermineBattleOrder (line 19) - var speed1a = array_get(templist, j).AGL
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_DetermineBattleOrder (line 19)
called from - gml_Object_Fight_Other_10 (line 7) - DetermineBattleOrder()
called from - gml_Object_Fight_Mouse_1 (line 3) - event_user(0)
If anyone could help me, that would be appreciated.
 

TheouAegis

Member
You need to set j to array_length_1d(templist)-1

Arrays start at 0, they go up to the size of the array minus one.
 
D

Drago the Shinigami

Guest
Thanks for the speedy help! I can't believe I missed that...
 
Top