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

Count number of variable in an array

W

Winter_Snake

Guest
Create Event:
Code:
connectionPoints[0] = "TopCenter";
connectionPoints[1] = "BottomCenter";
connectionPoints[2] = "LeftSide";
connectionPoints[3] = "RightSide";
Step Event:
Code:
var cPa, cPb;
cPa = 0;
for(cPb = connectionPoints[cPa]; !(cPb = ""); cPa += 1;)
{
     //Do certain functions
}
So what I want my program to do is to cycle through however many connectionPoints there are and do certain functions based on that. The reason why I am doing it this way (or at least assume this is a better way of doing this) is because I want the users to be able to mod the game when completed so the can change/add/remove connectionPoints at free will. And if I were to make it a static number, they'd have to manually change that.

The problem with the coding is, it infinitely loops. I don't know why because I assume that once it gets to connectionPoints[4] it sets it to "" or 0. I've tried both, but it still infinitely loops itself and crashes the game. any help would be great!

EDIT: I should also mention I'm using Game Maker 8.1
 

FrostyCat

Redemption Seeker
You keep incrementing cPa without changing cPb inside the loop, the loop is infinite as long as the first entry isn't "". That cPb = connectionPoints[cPa] part runs only once at the beginning of the loop.

Also, if you reference beyond the bounds of an array, you won't get "" or 0, you'll get an "array out-of-bounds" error message. In 8.1 it is not possible to find the size of an array without keeping track of it in another variable, so you should use a list instead.

Some of these beliefs you have about basic GML theory are just pure baloney. I suggest that you re-read the Manual entries on them before making up more baseless assumptions of your own.
 
W

Winter_Snake

Guest
You keep incrementing cPa without changing cPb inside the loop, the loop is infinite as long as the first entry isn't "". That cPb = connectionPoints[cPa] part runs only once at the beginning of the loop.

Also, if you reference beyond the bounds of an array, you won't get "" or 0, you'll get an "array out-of-bounds" error message. In 8.1 it is not possible to find the size of an array without keeping track of it in another variable, so you should use a list instead.

Some of these beliefs you have about basic GML theory are just pure baloney. I suggest that you re-read the Manual entries on them before making up more baseless assumptions of your own.
To clarify, the reason I was using a FOR loop is because I wanted to check every single listed connection and draw a sprite depending on what was available. The reason I don't want to use an IF statement is because I would have to check every single variable and array for the same string. So I thought I would use a for loop and my idea was something like this:
Code:
connectionPoints[0] = "TopCenter";
connectionPoints[1] = "BottomCenter";
connectionPoints[2] = "LeftSide";
connectionPoints[3] = "RightSide";

var cPa, cPb;
cPa = 0;
for(cPb = connectionPoints[cPa]; !(cPb = ""); cPa += 1;)
{
     cPb = connectionPoints[cPa];

     if connectionPoints[cPa] = "TopCenter"
     {
          draw_sprite(spr_connectionPointTop, 0, self.x, self.y);
     }

     if connectionPoints[cPa] = "BottomCenter"
     {
          draw_sprite(spr_connectionPointTop, 0, self.x, self.y + 32);
     }
}
And I have taken a look at the list and my issue with the list is, it adds it to the bottom. There is no way to check if there is just a variable, but using the ds_list_find_index it returns the position of where it is in the list, not if its true or false.

The only solution I can think to try would be to use both ds_list_find_index to check if the part would have the connectionPoint in the list, then use the ds_list_find_value with a different variable to use as a boolean.

EDIT: Also, thank you for pointing out that the array would just send an error for being out-of-bounds. I did not know that much.

EDIT 2: A way I can see working for it is using both the FOR loop and the list.
Use the list to track how many connections there are, and using the FOR loop to cycle through it, thanks for that idea!

EDIT3:
Code:
///Connection Points 04
/*
  TopCenter
  BottomCenter
*/

draw_sprite(spr_Command_Pod, 0, self.x, self.y);
//Draw sprite for part

cPlist = ds_list_create();
ds_list_add(cPlist, "TopCenter");
ds_list_add(cPlist, "BottomCenter");
cPmax = ds_list_size(cPlist);
/*
  Create list of connections to track
*/

cParray[0] = "TopCenter";
cParray[1] = "BottomCenter";

for(cPfor = 0; cPfor <= cPmax - 1; cPfor += 1)
{
 
  if cParray[cPfor] = "TopCenter"
  {
  draw_sprite(spr_Fuel_Tank, 0, self.x, self.y - 32);
  }
  if cParray[cPfor] = "BottomCenter"
  {
  draw_sprite(spr_Fuel_Tank, 0, self.x, self.y + 32);
  }
 
}
/*
  Check connection grid and display avaiable connections
*/
And finished. Also, I have looked through the manual, several times. The ideas aren't baloney, it's just thinking differently.
 
Last edited by a moderator:

TheouAegis

Member
If you're going to use a list and an array in tandem, then you may as well just use the list and ditch the array completely, since all you're doing is just wasting memory to do with an array what you can do just as well with a list.
 
Top