Is there any better naming of resources?

relycan

Member
Hi,

I'm trying to name scripts somehow so I can easily assign them to objects as functions (like in other OOP languages). Is there any better option for naming them? I find it quite nonsense that in GMS 2 resources must have unique names regardless of folders, type of resource or something like that. In other languages this is done by packages and their import.

Thank you very much for any help.
 

Attachments

Last edited:

Dr_Nomz

Member
Well the way GM works is when you call a script, it's referring to ALL the scripts in the script resource tree, so whenever you call one in code, you use "scr_name" and it'll loop through all the scripts in the game until it finds the right one. (It might be better optimized than that lol)

Point is, it's necessary for them ALL to have completely unique names. Better yet, the way it works is great because you can just type one letter of a script and see all the scripts with those letters in them, making it WAY easier to find what you need. So just name them like you are right now, that seems fine.
 
Yeah, I don't know of another way. Over time, I have ended up following a similar pattern.

Script names all begin with the object or functional area they belong too, such as :
Code:
scr_gui_*
scr_gui_window_*
scr_gui_menu_*
scr_player_*
etc....however with the 2020 GML Update, with being able to define our own functions, things may improve.
 

Binsk

Member
Prefix / Suffix is the best you can do for the moment. You can always use the "User Events" in objects to have custom local functions for the object. Then just call event_perform(ev_other, ev_user0) (for example) to execute it. I actually have this topic which better allows treating them like actual functions.

Worth noting that scripts are treated as local when executed. That is to say, they have access to all the local variables and whatnot of the calling object. With this is mind you can often rewrite your scripts to be more general rather than having a specific one for each individual object.
 

TheouAegis

Member
Prefix / Suffix is the best you can do for the moment. You can always use the "User Events" in objects to have custom local functions for the object. Then just call event_perform(ev_other, ev_user0) (for example) to execute it. I actually have this topic which better allows treating them like actual functions.
I did that for my Tetris clone I've been working on. As long as you don't need more than 16, it's good. You can't pass arguments or return values, but I just used a global variable for returns.

Also you don't have to even name the scripts, they are all indexed.

But yeah, I get the frustration of trying to come up with unique names.
 

efeace

Member
Namespacing is actually not any different than declaring global variables. It is possible to create a namespace-like arrangement using GM's structs. LUA uses a similar approach.

GML:
globalvar MyNamespace;

MyNamespace = {
    myNamespacedFunction = function() {
        // Do things
    }
};

// Call function
MyNamespace.myNamespacedFunction();
 
Top