• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Loading External Images as Room's Background

S

ShinkoNet

Guest
Hi,

I would like to import an image from a person's PC and make it the background image for the room.

This code makes the variable "custombackground" (which initially equals "") store the complete address of the image after drawing a button that browses for it. How would I go about making this the room's background?

Code:
if (draw_button2(rw-200,24,72,"Background") && wmenu=0) {
        fn=string(get_open_filename_ext("Background Image (*.png;*.jpg;*.gif)|*.png;*.jpg;*.gif","",programpath,"Add a Background Image to the Stage."))
                if (file_exists_lib(fn)) {
                    custombackground=programpath+filename_name(fn)
        }
    }
}
Thanks.
 
M

montiedragon

Guest
To load the background, use:
Code:
back = background_add(custombackground, 0, 0);//load background
Then in the Image Loaded Event (Add Event > Asynchronous > Image Loaded)
Code:
if ds_map_find_value(async_load, "id") == back
    {
    if ds_map_find_value(async_load, "status") >= 0
       {
       room_set_background(room_to_set_background, 0, true, false, back, 0, 0, true, true, 0, 0, 1);//change the room's background
       }
    }
 
S

ShinkoNet

Guest
To load the background, use:
Code:
back = background_add(custombackground, 0, 0);//load background
Then in the Image Loaded Event (Add Event > Asynchronous > Image Loaded)
Code:
if ds_map_find_value(async_load, "id") == back
    {
    if ds_map_find_value(async_load, "status") >= 0
       {
       room_set_background(room_to_set_background, 0, true, false, back, 0, 0, true, true, 0, 0, 1);//change the room's background
       }
    }
Thanks for the quick reply.

Getting this error when activating the event code after I load it:
Code:
___________________________________________
############################################################################################
ERROR in
action number 1
of Create Event
for object obj_controller:

Data structure with index does not exist.
 at gml_Script_change_theme (line 12) - if ds_map_find_value(async_load, "id") == back
############################################################################################
--------------------------------------------------------------------------------------------
 
Last edited by a moderator:
M

montiedragon

Guest
Make sure that second part goes into the Image Loaded Event. It looks like you put it in the Create event.
 
S

ShinkoNet

Guest
Make sure that second part goes into the Image Loaded Event. It looks like you put it in the Create event.
I was activating it from a script, not an object. Changed that now, whoops.

I now have created an object called "obj_bgloader", created an instance of it when starting the program, and made the code go into it's Image Loaded event.
I have only room right now, and it's called rm_app. I changed the placeholder room name to that.

That room has a default background colour of grey.

The errors are gone, but the background is not changing when I press the button and load the image. I still have the grey background colour that was started up originally.

window_set_color() works to change it, however, so I know I can somehow change this background...
 
Last edited by a moderator:
M

montiedragon

Guest
It seems you can't change the background of a room while you're in it. You may have to use this in the Draw Begin event.
Code:
draw_background_tiled(back, 0, 0);
 
S

ShinkoNet

Guest
It seems you can't change the background of a room while you're in it. You may have to use this in the Draw Begin event.
Code:
draw_background_tiled(back, 0, 0);
I think you can change backgrounds when in a room. It works for me anyway. Just use :

Code:
background_index[0] = back
I tried both of these one by one by putting them in the draw script for the room (same place as I put those window_set_color() stuff), and the draw_background_tiled one did this:

while the other one did nothing.

Putting the draw_background_tiled into the Image Loaded Event caused nothing to happen, not even an error.

Could I load the image as a sprite and have it be drawn under all of the other sprites?
 
You also have to make sure the background is visible after you have assigned it to the index.

background_visible[0] = true

And don't do this in the draw event. you should only assign the background index when you want to change the background, so only call it Just after you have loaded the background.
 
M

montiedragon

Guest
Alright I've combined both soloutions. This should work:

Code:
back = background_add(custombackground, 0, 0);//load background
Then in the Image Loaded Event (Add Event > Asynchronous > Image Loaded)
Code:
if (ds_map_find_value(async_load, "id") == back){
    if (ds_map_find_value(async_load, "status") >= 0){
        background_index[0] = back;//change the room's background
    }
}
Edit: I just realized that this is almost exactly the same thing written in the manual.
Click here to learn a bit more about how this works. One of the reasons Gamemaker is so easy to learn is because the kind folk at Yoyogames document the functions, events, and other inner workings of Gamemaker so well.
 
Last edited by a moderator:
Top