room_set_background not working [SOLVED]

I have done extensive research all over Google for the whole day and it seems I'm the only one who came across this problem. Most likely because most people use the graphical interface to set the background of a room.

Anyways, I created a script called "scr_createbackground()" so I can dynamically set the background for each new room.

In the creation code of the room "rm_00" I wrote "scr_background()" to call the script.

In the script I have the following code:

var room_index = room;
var bind = 0;
var vis = 1;
var fore = 0;
var back = bg_sky;
var bg_x = 0;
var bg_y = 0;
var htiled = 1;
var vtiled = 1;
var hsp = 0.2;
var vsp = 0;
var alpha = 1;

room_set_background(room_index, bind, vis, fore, back, bg_x, bg_y, htiled, vtiled, hsp, vsp, alpha);​

I have tried all kinds of possible variations to write this line of code. I've read the manual about rooms several times. I watched many youtube videos. And I just can't figure out what I'm doing wrong. It must be something very simple and trivial that I'm constantly missing.

Does anyone know why it is not setting the background of my room?

Thank you all so much for taking the time to read this and also if you have an answer, thank you in advance!
 
R

RealsLife

Guest
I might be stupid but you say you called your script "scr_createbackground()" yet when you're calling your script you call it "scr_background()" make sure you write the same name when calling the script!
 

Llama_Code

Member
Ok that is your problem, you can't set the background of the current room with room_set_background.

You will need to set it before starting that room.
 

jo-thijs

Member
room_set_background only has effect when you reload the room, you'd have to use room_goto first.
However, you might rather want to use background_index[0..7] and the other built-in background_*[0..7] variables.
 
@RealsLife sorry that was a typo from me, I am actually calling the script "scr_createbackground()". Can't believe you've actually seen that !!

@Llama_Code So the "creation code" is NOT happening before the room is created? Because I am calling the script from inside the "creation code".
 
Last edited:
@jo-thijs @Llama_Code I understand now. And I can't simpy call "room_goto(room)" because that will turn into an infinite loop. I will try the background_index[0..7] and the other built-in background variables, as suggested.

Thank you all so much for the fast answer! I have created games only with JavaScript so far for the canvas and most of the time I didn't even use a framework or engine, I just wrote plain JavaScript in Notepad++ and built something from the ground up. I am slowly getting used to GameMaker and the restrictions of an Engine.

This forum is insanely helpful, cheers all! (I will try to mark this thread as solved.)
 
R

RealsLife

Guest
"With this function you can set a background for a room (except the current one), with all the same property options that you can find in the room editor like horizontal speed and horizontal or vertical tiling." Yea this is exact the description of the function you tried to use so he is right! Btw can you mark somthing as solved XD? Damn I should use that if it exists and I don't think game maker studio has that many restrictions because you can add your own scripts(functions) and I think you can even use DLL to add code from C++ for example. I've seen people do the most crazy things I didn't realize were possible :eek:!
 
@RealsLife I knew it was something small I had missed, I really should have seen that when reading the manual.

For anyone interested, what I did is I created a room called "rm_init" and the script "scr_createbackground()" is called from the creation code of "rm_init". And then I updated the script so that "var room_index = room_next(room);" and at the end of the script I added "room_goto(room_index);"

I am sure that is not the best way to do it but at least I got it to work for this case, yay!

@RealsLife I am very new to GML obviously. I am a big fan of writing code (instead of graphical interfaces) and the way I see GML is that if I understand how things tie together in the engine I'll be able to create amazing things much faster and for once also get beautiful art into a game :D
 
Top