Legacy GM Problem with consecutive elements in array

A

ag07

Guest
I have the following code:

Create:

// array called 'point', to store numbers.

point[0] = 3;
point[1] = 2;
point[2] = 1;
point[3] = -1;
point[4] = 6;
point[5] = 7;
point[6] = -1;
point[7] = -1;

clicked = 0;


Global mouse Left released:

// track if left mouse clicked

if (clicked = 0) {
clicked = 1;
}

alarm[0] = 15;

Alarm[0]:

// -1 indicates empty slot in array

// if only one empty array slot next, skip to next empty slot, if two consecutive slots found, put mouse_x and mouse_y coordinates in these slots:

if (clicked = 1) {
for (i = 0; i < array_length_1d(point); i ++) {
if (point = -1) and (point[i + 1] != -1) {

continue;
}
else if (point = -1) and (point[i + 1] = -1)

point = mouse_x;
point[i + 1] = mouse_y;
}
}

This results in an endless column of the mouse_x coordinate, for example:

150

150

150

150

150

150

150

150

endlessly

The result I want is, for example:

point[0] = 3;
point[1] = 2;
point[2] = 1;
point[3] = -1;
point[4] = 6;
point[5] = 7;
point[6] = 150; // two empty slots in a row found, put in mouse x and y coordinates
point[7] = 320;

Sorry for the huge post, but why don't I have the above example result? What do I have wrong for that?
Thanks for the help
 
C

ConsolCWBY

Guest
In your if statements you are assigning 1 to click and -1 to point.
You should have used == ( I pronounce it IS EQUAL whereas = is just EQUALS if I read code to myself)
So, you should have:
if (clicked == 0) {
clicked = 1;
}
etc...
Fix those and see if the problem abates or abides. ;)
 

Jezla

Member
That shouldn't matter as GML is squishy in differentiating between = and == in comparison statements. I think the problem is in the for loop:

Code:
for (i = 0; i < array_length_1d(point); i ++)
{
          if (point = -1) and (point[i + 1] != -1) {
               continue;
     }
     else if (point = -1) and (point[i + 1] = -1)
   
          point = mouse_x;
          point[i + 1] = mouse_y;
}
should be:

Code:
for (i = 0; i < array_length_1d(point); i ++)
{
     if (point[i] = -1) and (point[i + 1] != -1)
          {
               continue;
          }
     else if (point[i] = -1) and (point[i + 1] = -1)
          {
               point[i] = mouse_x;
               point[i + 1] = mouse_y;
          }
}
See the difference?

EDIT: Apologies, I misread ConsolCWBY's post a little, he was on the right track. point = <value> erases the array and creates a single variable with the value you assigned. the for loop keeps re-assigning it.
 
Last edited:
C

ConsolCWBY

Guest
That shouldn't matter as GML is squishy in differentiating between = and == in comparison statements.
Squishy, or undefined?.. I can't see how it would make no difference due to every programming paradigm I've ever heard of. I mean, compilation-wise, how the hell could an assignment or a comparison be squishy on the backend?? If true, black magic MUST BE involved! :D
Anyway, I wouldn't take the chance due to compatibility between versions of GM:S. Besides, if one gets into the habit now - a world of programming opens up for you without any head scratching.

Nice catch on the curlies, btw! Those DO make a difference!
 

Jezla

Member
Black magic indeed! ;-) I wasn't implying that using = instead of == is a good practice, only that it wasn't necessarily the cause of the problem as GML doesn't distinguish between the two in comparisons; hence, squishy.
 
C

ConsolCWBY

Guest
GML doesn't distinguish between the two in comparisons; hence, squishy.
O M G
I never knew that. I didn't think anyone would be that sloppy. Thank you for enlightening me on this.
Do you know what - if any - language the code is compiled to? I assumed it was C++, but now I think I see where the slow-downs are coming from if the abstraction is that bad... o_O
 

Jezla

Member
That I don't know. I'm not a programmer myself, and I don't know any languages other than GML and NWScript (the scripting language for Neverwinter Nights). All of what I know is from the manual and the GMC. You'd have to ask someone with a more knowledge of GM:S's inner workings. Me; as long as it works, I'm happy! :D
 
C

ConsolCWBY

Guest
Well, you knew more than me Jezla! Hopefully the OP's code is working and our sidetrack isn't noticed by... YOU-KNOW-WHO!
 
A

ag07

Guest
Hi,

I put all '=' of if statements to '==' as recommended, like this:

if (clicked == 1) {
for (i = 0; i < array_length_1d(point); i ++)
{
if (point[ i ] == -1) and (point[i + 1] != -1)
{
continue;
}
else if (point[ i ] == -1) and (point[i + 1] == -1)
{
point[ i ] = mouse_x;
point[i + 1] = mouse_y;
}
}
}

I didn't realize '==' vs. '=' could make a difference, but
it works great now! so thanks guys...btw yes, GML Is compiled into C++ before running programs
 
Top