Legacy GM How to refer to specific room in GML code (in Player object)?

A

Agletsio

Guest
Hi,

I'm trying to figure out how to refer to a specific room in Player code sheet.

I'm currently exploring the parallax background technique and each room will need different variations of background speeds (some of them none).

At the moment I have this code in my Player object but what happens now is that this code is obviously applied to every room. How can I indicate that if Player is in this room, this will be the background speeds.

if direction = 0 and speed > 0 {
background_hspeed[2] = 1;
}

else

if direction = 180 and speed > 0 {
background_hspeed[2] = -1;
}

else

background_hspeed[2] = 0;


________________________________________________________________________________________



And then I also have a problem referring to more than one background in code (but let me know if this is a question for a new thread) as it only calls the first background I mention. So with the code below, why does only background[2] move?



if direction = 0 and speed > 0 {
background_hspeed[2] = 1;
backgroudn_hspeed[3] = 3;
}
 

jo-thijs

Member
To check if you're in a certain room:
Code:
if room == replace_this_with_the_name_of_your_room_without_quotes {
    // do stuff if you're in that room
}
What's wrong with this code:
Code:
if direction = 0 and speed > 0 {
background_hspeed[2] = 1;
backgroudn_hspeed[3] = 3;
}
is that you misspelled the third line.
 
A

Agletsio

Guest
To check if you're in a certain room:
Code:
if room == replace_this_with_the_name_of_your_room_without_quotes {
    // do stuff if you're in that room
}
What's wrong with this code:
Code:
if direction = 0 and speed > 0 {
background_hspeed[2] = 1;
backgroudn_hspeed[3] = 3;
}
is that you misspelled the third line.
Awesome, thanks jo-thijs! Works perfectly. Just so that I understand the reasoning behind this, what does "==" mean? Why not use only "="?

Quite new to Game Maker (and coding in general) so haven't caught up with all the function symbols yet.

As for second bit, the spelling mistake was only in this post, sorry! Originally it was spelled correct in code sheet. But still wouldn't move background [3].
 

jo-thijs

Member
Well, I need more information then in order to tell you why background_hspeed[3] remains 0.
Can you give more code you're using?
Are you sure the if condition is true?
Are you sure some other code doesn't reset background 3?
Some other relevant information?
 
A

Agletsio

Guest
Well, I need more information then in order to tell you why background_hspeed[3] remains 0.
Can you give more code you're using?
Are you sure the if condition is true?
Are you sure some other code doesn't reset background 3?
Some other relevant information?
Ok so this is full code in Step Event in my Player object. What I've realised just now is that when I added the "background [1]" reference to first paragraph ("direction = 0" etc.) then it seems to work. But when I add references to "background[1] to other paragraphs, then it started not moving anymore, no matter in which direction I'm moving.

Also, what does "==" mean?



//General Controls

script_execute(noplay_script)

//Player Hide

if (place_meeting(x, y, obj_door)) and keyboard_check(ord('C')) {
visible = false;
speed = 0;
}

else {visible = true;}


//Background

if room == room3 {
if direction = 0 and speed > 0 {
background_hspeed[2] = 1;
background_hspeed[3] = 9;
}

else

if direction = 180 and speed > 0 {
background_hspeed[2] = -1;
background_hspeed[3] = -9;
}

else

background_hspeed[2] = 0;
background_hspeed[3] = 0;
}
 
T

TimothyAllen

Guest
Awesome, thanks jo-thijs! Works perfectly.
As for second bit, the spelling mistake was only in this post, sorry! Originally it was spelled correct in code sheet. But still wouldn't move background [3].
Well, I need more information then in order to tell you why background_hspeed[3] remains 0.
Can you give more code you're using?
Are you sure the if condition is true?
Are you sure some other code doesn't reset background 3?
Some other relevant information?
Maybe im the confused one... but I think he said its working. He just stated that it wasnt working before even with correct spelling.
 
J

Jaqueta

Guest
== is for comparisons
Such as IF statements, like this:
Code:
if a==10 then {game_end()} //Recommended, since it's how some languages do (I think Java does that)
However, = can be used without problems for comparisons too.
Code:
if a=10 then {game_end()} //Totally OK
And = is for assignments, like this:
Code:
a=10 //Recommended
However, using == for assignments, will result in an syntax error
Code:
a==10 //Don't do that, it will not compile the game
 
Last edited by a moderator:

TheouAegis

Member
Is it set up so that background[2] is visibly above or below 3?

[[[[[[[[[[[[[2]]]]]]]]]]]]]]]
[[[[[[[[[[[[[3]]]]]]]]]]]]]]]
Are you sure background[3] didn't just go flying off the screen? Is background[3] an actual background or a bunch of tiles? As jo said, are you sure you're even working with the correct backgrounds - are you sure it's background[3] that's not moving and not background[2]?

Are background[2] and background[3] foregrounds? Because moving them right when the player is moving right is odd for a background parallax.
 
A

Agletsio

Guest
== is for comparisons
Such as IF statements, like this:
Code:
if a==10 then {game_end()} //Recommended, since it's how some languages do (I think Java does that)
However, = can be used without problems for assignments too.
Code:
if a=10 then {game_end()} //Totally OK
And = is for assignments, like this:
Code:
a=10 //Recommended
However, using == for assignments, will result in an syntax error
Code:
a==10 //Don't do that, it will not compile the game

Hi Jaqueta,

Thanks for the thorough explanation! Helps a lot when I understand what I'm actually doing!
 
A

Agletsio

Guest
Is it set up so that background[2] is visibly above or below 3?

[[[[[[[[[[[[[2]]]]]]]]]]]]]]]
[[[[[[[[[[[[[3]]]]]]]]]]]]]]]
Are you sure background[3] didn't just go flying off the screen? Is background[3] an actual background or a bunch of tiles? As jo said, are you sure you're even working with the correct backgrounds - are you sure it's background[3] that's not moving and not background[2]?

Are background[2] and background[3] foregrounds? Because moving them right when the player is moving right is odd for a background parallax.

HI TheouAegis,

Not sure what you mean with "Is it set up so that background[2] is visibly above or below 3?". In the room editor it is set with background 2 appearing behind background 3.

And then also not sure what you mean with "Are you sure background[3] didn't just go flying off the screen?". Yeah, can still see it there, it's just not moving at all. And set the background to tile horizontally (view port set to follow player) so can't see how it would fly off.

Yes, I'm sure. At the moment background[2] is moving but background three[3] is static with the other static backgrounds. Set it to high speed (9 and -9) to easily see if it's moving but no luck.

Yes, background 3 is actual background and not tile set.

And then another thing I need to ask is: should this code work in theory?

Help this info gives you a better idea...
 

jo-thijs

Member
The code you've given should not move background 2 if background 3 doesn't move.
The reason background 3 doesn't move should be searched for somewhere else.

Can you give us more relevant information
or temporarilly upload the gmz (file > export project) file of your project somewhere (mediafire, onedrive, dropbox, box, google drive, ...) and share the link?
 
A

Agletsio

Guest
Hi there,

Sorry for getting back to late, have been insanely busy with work! I still haven't managed to figure out how to correctly implement this code but fortunately I've found a work-around that gives me the result I'm looking for.

So instead of adding code to Player object, I created another object (no sprite) with code that moves backgrounds when viewport moves. Object will need to be placed in room.

Looks something like this (code sheet in Step Event):

background_x[2] = view_xview[0] * -5
background_x[3] = view_xview[0] * -8

I followed the reasoning shown in this tutorial:
https://www.youtube.com/watch?v=_MqcjiDOpYQ
Also, just want to thank everybody for all their help and time! Game Maker has one of the most helpful and patient communities I've encountered!
 
Top