GML Case argument should be a constant error

O

OriginalGrim

Guest
I don't know why this doesn't work.

create:
globalvar instop;
instop = false;

inv = -1;
w = 0;
h = 0;
maxw = 2;
maxh = 1;

step:
if instop = false {

if keyboard_check_pressed(ord("S"))
h += 1;
else if keyboard_check_pressed(ord("W"))
h -= 1;
else if keyboard_check_pressed(ord("D"))
w += 1;
else if keyboard_check_pressed(ord("A"))
w -= 1;

}

{ if (h < 0) h = maxh;
else if (h > maxh) h = 0;

if (w < 0) w = maxw;
else if (w > maxw) w = 0;

}

if keyboard_check_pressed(vk_enter)
{
switch inv[w, h] {
//invetory
case inv[0, 0]:
break;
//equipment
case inv[0, 1]:
break;
//status
case inv[1, 0]:
break;
//synopsis
case inv[1, 1]:
break;
//save/load
case inv[2, 0]:
if instop = false {
instance_create_layer(view_xport[0] + 640, view_yport[0] + 73, "SLQ", obj_SLQ)
instop = true;
}
break;
//settings
case inv[2, 1]:
break;
}
}
 

obscene

Member
Cases need to be a constant (fixed) value, ie a number. 3. Or something like obj_name or spr_name. As the value of your arrays may change, they can't be used in a switch so you'll have to write them as IF statements.
 
O

OriginalGrim

Guest
Cases need to be a constant (fixed) value, ie a number. 3. Or something like obj_name or spr_name. As the value of your arrays may change, they can't be used in a switch so you'll have to write them as IF statements.
how would I go about doing that? would it be like
if inv = [0, 0] (action)? or would I need to set the inv var in create to w & h?
 

TheouAegis

Member
Not sure what you're trying to do, but you should probably fetch the value in the array after setting w&h. What IS in inv[]?Or you can combine w&h into one single variable and use a switch on that.
 
O

OriginalGrim

Guest
Not sure what you're trying to do, but you should probably fetch the value in the array after setting w&h. What IS in inv[]?Or you can combine w&h into one single variable and use a switch on that.
I'm trying to make a selection screen in an inventory (a 2 by 3) and so I thought that by setting inv to -1 in the step event I could override it with both w and h and have the cases set to what the w and h are. I was trying to do a 2d array but I think I might have done it wrong.
 

TheouAegis

Member
A 2d array only holds values. w & h would be indexes to the array. you could assign values to the array, but it sounds like what you are doing doesn't even need an array, it would be pointless. Just combining the values of w and h would be enough.
 
O

OriginalGrim

Guest
A 2d array only holds values. w & h would be indexes to the array. you could assign values to the array, but it sounds like what you are doing doesn't even need an array, it would be pointless. Just combining the values of w and h would be enough.
inv = [w, h]
if keyboard_check_pressed(vk_enter)
{
if inv = [0, 0] {
}
else if inv = [1, 0] {
}
else if inv = [2, 0] {
if instop = false {
instance_create_layer(view_xport[0] + 640, view_yport[0] + 73, "SLQ", obj_SLQ)
instop = true;
}
}
else if inv = [0, 1] {
}
else if inv = [1, 1] {
}
else if inv = [2, 1] {
}
}
this almost worked it didn't run into an error but now it won't enact [2, 0] I wish I didn't have to ask for help but I'm new at this.
 

TheouAegis

Member
I take it you're using Game Maker Studio 2, because that code shouldn't even run in older versions.

inv[w,h] = 0

That is a 2D array. That is how you set a 2D array, specifically.

if inv[w,h] == 0

That is how you read a 2D array.

inv = [w,h]

That is a 1D array in Studio 2 (and an error message in all other versions). I'm pretty sure that sets the variable inv to a pointer to the array [w,h], so checking if inv == [2,0] probably isn't working because inv itself isn't [2,0], the array it's pointing to is; so the [1,2] in the check is a different array.

In other words, while that is a crafty way of handling things (I almost thought it would have worked, too! lol), it's just not compatible with how GMS2 handles array literals (which is what you ended up using). HOWEVER!!!! What you can do is use the array_equals() functions. Your code is still wrong, but I'm just throwing this out there. lol

Code:
if array_equals(inv,[2,0]) {
   if instop = false {
      instance_create_layer(view_xport[0] + 640, view_yport[0] + 73, "SLQ", obj_SLQ)
      instop = true;
   }
}
 
O

OriginalGrim

Guest
I take it you're using Game Maker Studio 2, because that code shouldn't even run in older versions.

inv[w,h] = 0

That is a 2D array. That is how you set a 2D array, specifically.

if inv[w,h] == 0

That is how you read a 2D array.

inv = [w,h]

That is a 1D array in Studio 2 (and an error message in all other versions). I'm pretty sure that sets the variable inv to a pointer to the array [w,h], so checking if inv == [2,0] probably isn't working because inv itself isn't [2,0], the array it's pointing to is; so the [1,2] in the check is a different array.

In other words, while that is a crafty way of handling things (I almost thought it would have worked, too! lol), it's just not compatible with how GMS2 handles array literals (which is what you ended up using). HOWEVER!!!! What you can do is use the array_equals() functions. Your code is still wrong, but I'm just throwing this out there. lol

Code:
if array_equals(inv,[2,0]) {
   if instop = false {
      instance_create_layer(view_xport[0] + 640, view_yport[0] + 73, "SLQ", obj_SLQ)
      instop = true;
   }
}
I don't entirely understand why but this fixed the problem
Code:
inv = [w, h]
if keyboard_check_pressed(vk_enter)
{
if array_equals (inv, [0, 0]) {
}
else if array_equals (inv, [1, 0]) {
}
else if array_equals (inv, [2, 0]) {
        instance_create_layer(view_xport[0] + 640, view_yport[0] + 10, "SLQ", obj_SLQ)
        instop = true;
}
else if array_equals (inv, [0, 1]) {
}
else if array_equals (inv, [1, 1]) {
}
else if array_equals (inv, [2, 1]) {
    }   
}
 

TheouAegis

Member
I don't entirely understand why but this fixed the problem
Code:
inv = [w, h]
if keyboard_check_pressed(vk_enter)
{
if array_equals (inv, [0, 0]) {
}
else if array_equals (inv, [1, 0]) {
}
else if array_equals (inv, [2, 0]) {
        instance_create_layer(view_xport[0] + 640, view_yport[0] + 10, "SLQ", obj_SLQ)
        instop = true;
}
else if array_equals (inv, [0, 1]) {
}
else if array_equals (inv, [1, 1]) {
}
else if array_equals (inv, [2, 1]) {
    } 
}
inv[1,2] is a 2D array called "inv" with two indexes (0 and 1), and three subindexes or keys (0, 1, and 2).

[1,2] is a 2D array with no name, so Game Maker Studio 2 stores that array temporarily inside its own little pocket of memory. So when you say "inv = [1,2]", you are telling GM to set the variable "inv" to that little pocket of memory.

Now, when you have something like w is 1 and h is 2 and your code says
Code:
inv = [w,h] //This sets "inv" to the hidden array [1,2]
if inv == [1,2] { //This checks if the array pointed to by "inv" is the same hidden array [1,2]
...those two arrays are not the same ones. Even if you replaced "[w,h]" with "[1,2]" in this example, the first array that you set "inv" to will never be the same array that you compare "inv" to. The values might be the same, but the arrays occupy different pockets of memory.

It's confusing, I know. If you had
Code:
A = 1;
B = 1;
...then A equals B. But if you had
Code:
A = [1,2];
B = [1,2];
...then A does not equal B. To you and most people, it looks like they are equal, but to Game Maker, they're just completely different. Now, you could check if A[0] equals B[0] and A[1] equals B[1], but you can't simply check if A equals B when A and B contain arrays.

The function array_equals() doesn't care if the pockets of memory are different, it only cares about the contents of those pockets. It will look up the array in the first argument (in this case, "inv") and loop through every entry in it comparing it to every entry in the other array in the second argument.
 
O

OriginalGrim

Guest
inv[1,2] is a 2D array called "inv" with two indexes (0 and 1), and three subindexes or keys (0, 1, and 2).

[1,2] is a 2D array with no name, so Game Maker Studio 2 stores that array temporarily inside its own little pocket of memory. So when you say "inv = [1,2]", you are telling GM to set the variable "inv" to that little pocket of memory.

Now, when you have something like w is 1 and h is 2 and your code says
Code:
inv = [w,h] //This sets "inv" to the hidden array [1,2]
if inv == [1,2] { //This checks if the array pointed to by "inv" is the same hidden array [1,2]
...those two arrays are not the same ones. Even if you replaced "[w,h]" with "[1,2]" in this example, the first array that you set "inv" to will never be the same array that you compare "inv" to. The values might be the same, but the arrays occupy different pockets of memory.

It's confusing, I know. If you had
Code:
A = 1;
B = 1;
...then A equals B. But if you had
Code:
A = [1,2];
B = [1,2];
...then A does not equal B. To you and most people, it looks like they are equal, but to Game Maker, they're just completely different. Now, you could check if A[0] equals B[0] and A[1] equals B[1], but you can't simply check if A equals B when A and B contain arrays.

The function array_equals() doesn't care if the pockets of memory are different, it only cares about the contents of those pockets. It will look up the array in the first argument (in this case, "inv") and loop through every entry in it comparing it to every entry in the other array in the second argument.
Ok Thanks this was very helpful I think I get how it works now. Thank you for your time and help :)
 
Top