GML [Solved!] Finding a variable of an instance outside of the current room?

pumblerak

Member
Running GMS:2 Version 2.2.3.436.

Hello everyone! I've been on GM for a number of years now, and I've encountered a problem that I can hardly even begin to solve. I have an object named obj_musicmanager. obj_musicmanager fades the currently playing music between rooms, changes music as need be, all the bells and whistles. It's been through the wringer and works fantastic. The way it works is in each room there's one obj_musicnode with the creation code theme=(numerical theme id here);, and at room start obj_musicmanager will check for obj_musicnode, and get obj_musicnode.theme. If it's the same track, the music keeps going, if it's a different one, it starts the new track. A great system with minimal Spaghetti, especially considering its former incarnation before rewriting it. I've gotten better at programming since the start of the project, so aforementioned rewrite brought along the ability to fade tracks out. This creates an unwanted seam between rooms where the same track plays, making the music fade out and back in for no reason.

The best way I can solve this is by being able to figure out the next room's obj_musicnode.theme, and making the music not fade out if it's the same. I'm sure there's other ways I could do it, e.g. using room variables (if those work the way I think they do) or by making a big list, but changing how it works already would be a huge pain in the neck (not just for me!), and I'm trying to minimize Spaghetti as much as possible.

To reiterate: I am NOT having a problem where a track restarts, it picks up where it leaves off, I'm after a solution to make it not leave off to begin with.

As always any help is appreciated! Thank you.



Edit following solution:
Thanks for the response! While it may seem excessive I'm willing to argue that it will always be better than consulting a list each time I add a new room, but that's neither here nor there. My solution is as follows:

During the visual fadeout, obj_musicmanager doesn't automatically begin fading, but instead checks to see if the next room shows on a ds_map. By default it doesn't, so the game visually fades out to the next room. Once there, THEN obj_musicmanager begins the fadeout and does ds_map_add(loadedthemes,room,obj_musicnode.theme). Any time you revisit an area it will load much more quickly. I'm glossing over a lot of the technical aspects but the core solution is there and implemented. Again, thank you.
 
Last edited:
Objects outside of the current room have not been instantiated and loaded into memory, so you won't be able to access their variables.

Also, having a game object dedicated to storing a single variable like that is a bit excessive, as it's going to come along without a whole lot of built-in variables that are useless to your purpose.

Instead I'd recommend using a ds_map managed by your obj_musicmanager to map out your tracks to rooms. For example, you could create your map and assign your tracks like so:

GML:
musicMap = ds_map_create();
ds_map_add(musicMap, roomA, themeA);
ds_map_add(musicMap, roomB, themeB);
ds_map_add(musicMap, roomC, themeC);
...
...
ds_map_add(musicMap, roomZ, themeZ);
Before you change rooms, have obj_musicmanager do something like:

GML:
var targetRoomTheme = ds_map_find_value(musicMap, targetRoom);

if (!audio_is_playing(targetRoomTheme)) {

    //fade out the current track

}
Room Start event of obj_musicmanager:

GML:
var roomTheme = ds_map_find_value(musicMap, room)

if (!audio_is_playing(roomTheme)) {

    //fade in the new track

}
 
Top