Help on dynamic weapon inventory array?

M

mcsqueasy

Guest
Hello! Super green and barely started tinkering around with gms2. I'm following Tom Francis' tutorial series Making a Game With No Experience (done in gms 1.4). Part 9 has him designing an array that builds itself as the player picks up weapons. However, after following the video multiple times and ensuring my code matches his, I get this error on trying to pick up a weapon:

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object oSingleShotGun:

Variable oSingleShotGun.SelectedWeaponIndex(100010, -2147483648) not set before reading it.
at gml_Object_oSingleShotGun_Step_0 (line 7) - if Owner.Weapon[SelectedWeaponIndex] = self.id {
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_oSingleShotGun_Step_0 (line 7)

I gather that it's needing the SelectedWeaponIndex to be defined, but I have it set to 0 in the Create event for the player object. I'm not sure where else to look or what I need to add to make it happy. I've tried adding SelectedWeaponIndex = 0 for the Create events on the weapon objects, but then that breaks the code I have to switch weapons with rmb.

Any help would be amazing. Thanks! :)
 

arirish

Member
So, variables are local to instances unless explicitly created otherwise (for example by making them global - global.SelectedWeaponIndex). When you define the variable in the create event of the player object, that variable is only local to, and thus accessible to, the player object. oSingleShotGun does not have that variable to draw on. I would try making it global.

Also, you don't need to use 'self'. 'id' on its own is fine.
 
M

mcsqueasy

Guest
That worked perfectly! Not sure if it was a change between gm 2 and gm 1.4, but setting it to global got it functioning correctly. Thanks :)
 
Top