GMS 2 Help!!

Hello! im in the making of a game where you can run around as a ghost interacting with things where it either turn it off and on or play an animation once

and ive put a code like this into object sofa


Code:
if (keyboard_check(vk_up)) and place_meeting(x,y+1,player) {
  image_speed = 1   
    } else if (image_index > image_number = 1 or image_index = image_number = 1) {       
        image_speed = 0
}

so my problem is, if i put the code into another object that needs the same interaction

it just does the sofa, which i dont understand why

any one can answer me why and maybe a solution?
ty
have a great day!
 

TsukaYuriko

☄️
Forum Staff
Moderator
Please be a little more descriptive with topic titles.

Please also elaborate on what "doing the sofa" means... what do you expect to happen, and what happens instead?
 

TsukaYuriko

☄️
Forum Staff
Moderator
I figured I'll address the absurdity of that line once the actual problem has been established, but I might as well do it now then... :)

The "= 1" part here is synonymous to "== true", which is synonymous to being entirely redundant. It is valid code, though, as awkward as it may be. To translate the line in question:

GML:
else if (image_index > image_number = 1 or image_index = image_number = 1)
Or:

GML:
else if (((image_index > image_number) == true) or ((image_index == image_number) == true))
Or:

GML:
else if ((image_index >= image_number) == true)
Or just:

GML:
else if (image_index >= image_number)
;)
 
Code:
if (keyboard_check(vk_up)) and place_meeting(x,y+1,player) {
  image_speed = 1 
    } else if (image_index > image_number = 1 or image_index = image_number = 1) {     
        image_speed = 0
}
This code is formatted to make it as hard as possible to read, I'd say. Try to make it so when you are writing code, each "thing" that is going on is easily visible from glancing at it. There's all sorts of styles you can pick from, but pick one and be consistent. My personal style would be this:
Code:
if (keyboard_check(vk_up) and place_meeting(x,y+1,player)) {
  image_speed = 1 
}
else if (image_index > image_number = 1 or image_index = image_number = 1) {     
        image_speed = 0
}
I've corrected a bracketing error in the first if statement (the second bracket after keyboard_check() needs to be moved to the end after place_meeting()).

Now, looking at the else if statement, there's a lot of wrong going on. image_index > image_number = 1. This does not do what you think it does. I'm assuming you actually wanted image_index > image_number-1. The second check is completely redundant with a small change to the first check: image_index >= image_number-1. If image_index is greater than or equal to image_number-1, the conditional will be executed. We can also remove the else from the if, as I don't imagine it's actually needed. So the finalised codeblock would be:
Code:
if (keyboard_check(vk_up) and place_meeting(x,y+1,player)) {
  image_speed = 1 
}
if (image_index >= image_number-1) {     
        image_speed = 0
}
That code should work in any object that it is placed in. If it doesn't, you may have other things interfering with the image_speed or image_index in other parts of the code.

It's important to pay good attention to exactly what you are typing, especially if you are copying from someone else's code. Computers do exactly what you tell them, typos included.

EDIT: Sniped multiple times I guess.
 

Vusur

Member
else if (image_index > image_number = 1 or image_index = image_number = 1) {

This reads as "if the image number is bigger than the image_number set to 1 OR image_index equals image_number set to 1. Because (A == B == C) is not possible in GMS (If I'm not completly wrong). So the first "=" is interpreted as "is equal" while the second one is interpreted as "setting".
What you meant probably is
else if(image_index >= image_number) { //...your code
When comparing equal, always use "==". Your compiler would throw you an error in your case and you would know what is wrong.

Edit: Wait, now I was confused (A == B == C) works of course. I had (A < B < C) in mind.
Edit2: Also possible. Why did I thought, that this doesn't work? Was there a case, where you need (A < B && B < C)?
Edit3: That bothered me. Again. (A < B < C) works, but has a different meaning compared to (A<B && B < C). Quick example (-3 < -2 < -1). You might think this is true, but it's false. It does (-3 < -2) == true == 1 and then (true/1 < -3) which is false. While (-3 < -2 && -2 < -1) == true. Same with (A == B == C) -> (A ==B) ==true/false, then (true/false == C)
So don't use the shortcut.
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
Actually both of these are interpreted as a comparison, as evident by this code outputting "1 0 0 1" (first the output of show_debug_message and then the values of a, b and c showing that none of them were changed):
GML:
a = 0;
b = 0;
c = 1;

show_debug_message(a = b = c);
show_debug_message(a);
show_debug_message(b);
show_debug_message(c);
That compiles like this:

GML:
show_debug_message(a = b = c);
show_debug_message((a = b) = c);
show_debug_message((a == b) == c);
show_debug_message((0 == 0) == 1);
show_debug_message((0 == 0) == true);
show_debug_message((true) == true);
show_debug_message(true == true);
show_debug_message(true);
show_debug_message(1);
So yeah, that line, while awkward, is perfectly valid code.


Now that we've established that this line of code replicates the Animation End event... what's the actual problem here? What's supposed to happen and what happens instead? Why is the sofa being done and what does that even mean? 😵

Do instances of any other object with this code refuse to animate? Does the sofa animate regardless of what you interact with? We'll need a bit more information before we can help you.
 
Please be a little more descriptive with topic titles.

Please also elaborate on what "doing the sofa" means... what do you expect to happen, and what happens instead?
sorry -
it animates the sofa, even tho i put the script into another object step event - i thought i could put the same script into different object and yea... apparently not haha
i have a painting that i want to fall down when the player interacts - and when i interact with it - it interacts with the sofa even tho im miles away from it D:

im getting a lot of comments on my scripting way xD
im sorry for that - i will do it the other way so it easier to read next time i need help.
im new to programming or not fully new, but still lack a lot of understanding.

but since i wrote place_meeting,x,y,player
i thought it would work
apparently not D:

the other solution i have in mind is to put every script into the player step and put place_meeting for every interact object - but i kinda wanna avoid that cause that seems like a lot if i want to make more maps etc.

hope this gave more information and better understanding of my problem.

I need somebody!

Help!

Not just anybody!

Help!

You know I need someone!

Heeeeellllpp!!




I think you mean to do "image_number - 1", not "image_number = 1".

if i do that,it will stop on the 5th frame of my animation sprite and not the first or 6th

and nice with the beatles refference hahah!

I figured I'll address the absurdity of that line once the actual problem has been established, but I might as well do it now then... :)

The "= 1" part here is synonymous to "== true", which is synonymous to being entirely redundant. It is valid code, though, as awkward as it may be. To translate the line in question:

GML:
else if (image_index > image_number = 1 or image_index = image_number = 1)
Or:

GML:
else if (((image_index > image_number) == true) or ((image_index == image_number) == true))
Or:

GML:
else if ((image_index >= image_number) == true)
Or just:

GML:
else if (image_index >= image_number)
;)
ah thanks - my code is messy yea - i need to work on that, i spent hours just trying to get the code to work and came up this hahah
still in the progress of learning and understanding! :)

but thank you so much :D

Code:
if (keyboard_check(vk_up)) and place_meeting(x,y+1,player) {
  image_speed = 1
    } else if (image_index > image_number = 1 or image_index = image_number = 1) {    
        image_speed = 0
}
This code is formatted to make it as hard as possible to read, I'd say. Try to make it so when you are writing code, each "thing" that is going on is easily visible from glancing at it. There's all sorts of styles you can pick from, but pick one and be consistent. My personal style would be this:
Code:
if (keyboard_check(vk_up) and place_meeting(x,y+1,player)) {
  image_speed = 1
}
else if (image_index > image_number = 1 or image_index = image_number = 1) {    
        image_speed = 0
}
I've corrected a bracketing error in the first if statement (the second bracket after keyboard_check() needs to be moved to the end after place_meeting()).

Now, looking at the else if statement, there's a lot of wrong going on. image_index > image_number = 1. This does not do what you think it does. I'm assuming you actually wanted image_index > image_number-1. The second check is completely redundant with a small change to the first check: image_index >= image_number-1. If image_index is greater than or equal to image_number-1, the conditional will be executed. We can also remove the else from the if, as I don't imagine it's actually needed. So the finalised codeblock would be:
Code:
if (keyboard_check(vk_up) and place_meeting(x,y+1,player)) {
  image_speed = 1
}
if (image_index >= image_number-1) {    
        image_speed = 0
}
That code should work in any object that it is placed in. If it doesn't, you may have other things interfering with the image_speed or image_index in other parts of the code.

It's important to pay good attention to exactly what you are typing, especially if you are copying from someone else's code. Computers do exactly what you tell them, typos included.

EDIT: Sniped multiple times I guess.
oh well

that might be my problem then cause i sat the exact same code into another object i wanted to be able to have the same effect
but it apparently interfered!
and sorry for my messy code, im still trying to learn and understand! and thanks for the programming tips! will deffo make it easier to read!

i guess i just gotta set up local variables in that object - i hope that works

else if (image_index > image_number = 1 or image_index = image_number = 1) {

This reads as "if the image number is bigger than the image_number set to 1 OR image_index equals image_number set to 1. Because (A == B == C) is not possible in GMS (If I'm not completly wrong). So the first "=" is interpreted as "is equal" while the second one is interpreted as "setting".
What you meant probably is
else if(image_index >= image_number) { //...your code
When comparing equal, always use "==". Your compiler would throw you an error in your case and you would know what is wrong.

Edit: Wait, now I was confused (A == B == C) works of course. I had (A < B < C) in mind.
Edit2: Also possible. Why did I thought, that this doesn't work? Was there a case, where you need (A < B && B < C)?
Edit3: That bothered me. Again. (A < B < C) works, but has a different meaning compared to (A<B && B < C). Quick example (-3 < -2 < -1). You might think this is true, but it's false. It does (-3 < -2) == true == 1 and then (true/1 < -3) which is false. While (-3 < -2 && -2 < -1) == true. Same with (A == B == C) -> (A ==B) ==true/false, then (true/false == C)
So don't use the shortcut.
tbh
im either too new or too tired for understanding this
but thanks tho! haha -
have a great day!
 
Top