• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Array index out of range

S

Shadowblitz16

Guest
can someone tell me why I get a push error that the variable "ChargeL" is out of range of my array when I release fire?

/// @description Init Player
// You can write your code in this editor

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Init Stats
//////////////////////////////////////////////////////////////////////////////////////////////////////

PMod = 1;
EMod = 1;
DMod = 1;

for (var _i=0; _i<8; _i++) { Weapon[_i] = 0; }
for (var _i=0; _i<8; _i++) { Energy[_i] = 16; }
for (var _i=0; _i<8; _i++) { Shield[_i] = 16; }

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Init Variables
//////////////////////////////////////////////////////////////////////////////////////////////////////

image_speed = 0;
image_index = 1;

State = 0;
WeaponState = 0;

//The Following Means..
//D = Direction
//T = Timer
//F = Frame
//L = Level

TiltD = 1;
TiltT = 0;

ThrustD = 1;
ThrustT = 0;
ThrustF = 0;

MuzzleD = direction;
MuzzleT = 2;
MuzzleF = 0;

ChargeT = 0;
ChargeF = 0;
ChargeL = 0;

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Init Projectile Types
//////////////////////////////////////////////////////////////////////////////////////////////////////
Buster = [
[1, 1, 1, 1, 1, 2],
[spr_buster_0, spr_buster_1, spr_buster_2, spr_buster_3, spr_buster_4, spr_buster_5],
[5, 5, 5, 5, 5, 5],
[1, 2, 3, 4, 5, 6],
[0, 0, 0, 0, 0, 8],
[0, 0, 0, 0, 0, 5],
[true, true, true, true, true, true]
];

/// @description Update Player
// You can write your code in this editor

var _inputX = keyboard_check(vk_right) - keyboard_check(vk_left);
var _inputY = keyboard_check(vk_down) - keyboard_check(vk_up);
var _inputA = keyboard_check(ord("X"))

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Update States
//////////////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////////////
// Switch States
//////////////////////////////////////////////////////////////////////////////////////////////////////

switch (State)
{
case 0: event_perform(ev_other, ev_user0) break;
}

/// @description Normal State
// You can write your code in this editor

var _inputX = keyboard_check(vk_right) - keyboard_check(vk_left);
var _inputY = keyboard_check(vk_down) - keyboard_check(vk_up);
var _inputA = keyboard_check(ord("X"))

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Update Movement
//////////////////////////////////////////////////////////////////////////////////////////////////////


if (_inputX != 0)
{
// Reset Tilt Timer
if (TiltT <= 0) { TiltT = 2; }

// Update Tilt Timer and Tilt Direction
if (TiltT > 0) { TiltT --; }
else
{
TiltD = 1 + _inputX;
}
}

if (_inputY != 0)
{
ThrustD = 1 + _inputY;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
// Update Shooting
//////////////////////////////////////////////////////////////////////////////////////////////////////

if ( _inputA && WeaponState == 0) { WeaponState = 1; }
else if (!_inputA && WeaponState == 1) { WeaponState = 2; }
else if (!_inputA && WeaponState == 2) { WeaponState = 0; }

if (WeaponState == 0)
{
if (ChargeT > 0) { ChargeT --; }
else if (ChargeT == 0)
{
//scr_spawn_projectile(Buster[ChargeL, 0], Buster[ChargeL, 1], Buster[ChargeL, 2], Buster[ChargeL, 3], Buster[ChargeL, 4]/2, Buster[ChargeL, 5]-2, Buster[ChargeL, 6])
}
}
if (WeaponState == 1)
{
if (ChargeT > 0) { ChargeT --; }
else
{
ChargeL ++;
ChargeT = (8 * ChargeL);
}
}
if (WeaponState == 2)
{
scr_spawn_projectile(Buster[0, ChargeL], Buster[1, ChargeL], Buster[2, ChargeL], Buster[3, ChargeL], Buster[4, ChargeL], Buster[5, ChargeL], Buster[6, ChargeL]) <---This is where it happens
ChargeL = 0;
ChargeT = 0;
}
 

Binsk

Member
I don't have GMS 2 on hand at the moment to test, but I believe the new array declaration you are using for Buster creates a 1D array containing 1D arrays rather than GameMaker's special 2D array.

To retrieve from a 2D array you go:
Code:
var __value = array[6, ChargeL];
However, to retrieve from a 1D array containing 1D arrays you must instead go:
Code:
var __subValue = array[6];
var __value = __subValue[ChargeL];
Hope that helps.
 
Top