• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code [SOLVED] Problem with background compatibility scripts when importing from 1.4

I'm trying to import my 1.4 project into GMS 2 but I have a small optimisation problem concerning backgrounds.

GMS 2 creates a couple of compatibiliy scripts for backgrounds, but they are not very cpu friendly.
__background_get_element is my main concern, it takes up a lot of step % because it uses a lot of string functions.
It basically scrolls through all layers to find the compatibility background layers. In my game this means it's called approx. 10 times per step because I set the x and y of every background in every step using background_x[value] and background_y[value].

A solution I came up with was renaming all background layers with consistent names like "Background_0" and "Background_1" so I can address them directly. However, I have over 100 rooms in my project, with >5 backgrounds each, so this will take a long time.

Does anyone have a better idea?
So basically any cpu friendly alternatives to background_x and background_y without changing all rooms or object code?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
I'm afraid that you'r only option is to go through and properly adapt your code to the new layer system and use it correctly without the need for the compatibility scripts.
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
I think you could rewrite that compatibility script to utilize layer_get_id("Compatibility_Foreground_" + string(__bind)) instead of that whole layer loop.

A lot of compatibility scripts seem to share the problem of not utilizing more recently introduced built-in functions as well as lacking any (even opt-in) form of caching, making them slower than necessary.
 
I think you could rewrite that compatibility script to utilize layer_get_id("Compatibility_Foreground_" + string(__bind)) instead of that whole layer loop.
That wouldn't work because there is no consistent naming of the backgrounds between rooms. The names are generated like this "Compatibility_Foreground_" + number + name of bg resource. Which means I would still need a function like string_pos()

A lot of compatibility scripts seem to share the problem of not utilizing more recently introduced built-in functions as well as lacking any (even opt-in) form of caching, making them slower than necessary.
Thanks, this gave me an idea. I came up with the following solution (I only use backgrounds, no foregrounds):

1. In every room I added a room start event where all background ids are stored in a global array, this is basically a modified version of __background_get_element()

Code:
///SAVE ALL BACKGROUNDS IN A GLOBAL ARRAY ON ROOM START

var __bgstring = "Compatibility_Background_";
var __bglen = string_length(__bgstring);
var __layerlist = layer_get_all();
var __layerlistlength = array_length_1d(__layerlist);
var __i;

var __layerid = -1;

// Scroll through list of layers and find backgrounds
for(__i = 0; __i < __layerlistlength; __i++)
{
    var __layername = layer_get_name(__layerlist[__i]);  
    if (string_pos(__bgstring, __layername) > 0)
    {
        var __slotchr = string_char_at(__layername, __bglen + 1);
        if (__slotchr == "")
            continue;
           
        var __slot = real( __slotchr );
       
        //store in array
        global.bgs[__slot]=__layerlist[__i];      
    }  
}
2. Then I changed the __background_set() script to something like this:

Code:
var __prop = argument0;
var __bind = argument1;
var __val = argument2;

switch __prop{
case e__BG.X:layer_x(global.bgs[__bind],__val);break;
case e__BG.Y:layer_y(global.bgs[__bind],__val);break;
case e__BG.HSpeed:layer_hspeed(global.bgs[__bind],__val);break;
case e__BG.VSpeed:layer_vspeed(global.bgs[__bind],__val);break;
//etc
//etc
default:break;
}
And it works! Saves a lot processing power. I will mark this topic as solved. Thanks for the help!
 
Top