Jump to content


Photo

debugging an array


  • Please log in to reply
3 replies to this topic

#1 _255638

_255638

    GMC Member

  • New Member
  • 3 posts

Posted 27 February 2012 - 01:03 AM

I cannot get around this and it's driving me crazy!

var i;
for (i=0;i<9;i+=1)
{if (controller.inv[i,1]<1)
{controller.inv[i,0]=-1;
}
}

that's the code i'm stuck on. controller.inv is an array that i'm using as an inventory. But whenever i start the game it says variable not recognized or array out of bounds. I declared the variable at game start and the error-checker says nothing while i'm actually writing the code. what am i doing wrong?

inv[i,0] identifies what item is being held in that slot and inv[i,1] tells how many are being held. This is supposed to make it so that if there is no amount of the item being held then the item being held is equal to nothing (aka -1).
  • 0

#2 111Studio

111Studio

    GMC Member

  • GMC Member
  • 258 posts
  • Version:GM:Studio

Posted 27 February 2012 - 01:37 AM

You have to initialize every array slot, not just the first one. So...

var i;
for (i=0; i<9; i+=1){
inv[i,1] = 0;
inv[i,0] = -1;
}

So then whenever you do a check, each array slot that you actually use will have a value already. The above code should go in the create event of the controller.
  • 0

#3 _255638

_255638

    GMC Member

  • New Member
  • 3 posts

Posted 27 February 2012 - 03:30 AM

You have to initialize every array slot, not just the first one. So...

var i;
for (i=0; i<9; i+=1){
inv[i,1] = 0;
inv[i,0] = -1;
}

So then whenever you do a check, each array slot that you actually use will have a value already. The above code should go in the create event of the controller.


no it's IF controller.inv[i,1]<1 THEN controller.inv[i,0]=-1. It needs to check if inv[i,0]=0 not make inv[i,0]=0.
  • 0

#4 FoxInABox

FoxInABox

    GMC Member

  • GMC Member
  • 5393 posts
  • Version:GM:Studio

Posted 27 February 2012 - 03:59 AM

but if the variable position have not been assigned a value yet and is still undefined, then you are not allowed to compare it to anything..

so you should set all the slots to their empty value before using them, hopefully the controller does so in its create event?

another thing .. depending on the order things is performed in and what events this is, could be that that code triggers before the variables have been defined .. can be checked by putting a show_message in both places and see which ones triggers first .. if the wrong one, then re-place the 2 objects into the room starting with the controller
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users