YoYoGames / GameMaker-Bugs

Public tracking for GameMaker bugs
24 stars 8 forks source link

asset_get_all(asset_type) - A way of retrieving all assets in an array by type #3039

Closed tabularelf closed 4 months ago

tabularelf commented 1 year ago

Is your feature request related to a problem?

Normally this wouldn't make sense to add in, as assets are just numbers in the current runtime, and this would only matter for the new runtime, since they will be asset refs instead of asset ids by that point. However as we speak, this is happening right now in the current beta for the current runtime. And to be honest, having such a change without at least having some way of fetching all assets of a given type, is a very poor move.

Libraries like Scribble by JujuAdams for instance, indexes and adds all of the games fonts to be used with Scribble. I myself, have Sonus, which indexes and categories all sounds into two OOP-like constructors based on if they're streamed or in memory. (Sonus supports in adding in external files as audio). And libraries like YUI by shdwcat, and GMLive by YellowAfterLife, both rely on searching up the entire function dictionary to be added for the users benefit.

Just these libraries alone for instance, will break. And there isn't strictly an easier way of fixing these, as these are meant to be plug and play. I also have an entire game that has a whole modding implementation for coding as well. And adding in a huge list of functions (estimating anywhere from 200-400+ at this given time, but could easily be more overtime) manually isn't strictly my cup of tea. Let alone, it also destroys my unsafe_get function (that though, also does include built in function). Both of which absolutely do require a database for quick accessing fetching function names.

This is something I can't easily do, and would require a good chunk of my time indexing each function. It's arguably a lot easier adding in a blacklist than it is adding in a whitelist. Especially for someone who wants to add in a bunch of library functions, and then just remove the ones I don't want. i.e.

ModAPI.AssignFunctionByName("scribble"); // This only adds functions that start with "scribble". So scribble and scribble_* functions
ModAPI.RemoveFunction("scribble_msdf_thickness_offset", "scribble_flush_everything", "scribble_external_sound_add", "scribble_external_sound_exists", "scribble_external_sound_remove");

Describe the solution you'd like

The idea I have in mind is that we can do asset_get_all(asset_type), where asset_type will be one of the many asset keywords. Such as:

(asset_script should refer to functions like function foo() {} imo, or introduce asset_function). This also covers assets that were added dynamically on the fly. The return value will be an array of all of the assets, specified by the asset type. The benefit of this approach is that for a developer and for modding purposes, it makes it a lot easier to reference and pull things directly later on for console usage, a custom programming language or having an overall lookup of every asset needed. It also does allow us library makers to at least update and adapt the new "asset ids are now refs" change, without much of a hitch overall. Giving more advanced users, and library makers if I didn't make it clear enough, a way of referencing assets properly.

/// Script: game_init
global.spriteMap = {};
var _sprites = asset_get_all(asset_sprite);
for(var _i = 0, _len = array_length(_sprites); _i < _len; ++_i) {
    var _sprite = _sprites[_i];
    var _name = sprite_get_name(_sprite);
    global.spriteMap[$ _name] = _sprite;
}

Describe alternatives you've considered

The change for this does make sense for asset ids to become asset refs, but as it stands, the only other alternatives are either:

Additional context

The main big reminder is that, asset ids are refs, is currently in beta right now. Meaning that if this isn't added in by the time this hits monthly, this can and will break for a lot of people. As it'll affect libraries that do add assets straight away for the developers convenience. This would require putting in more work for not just the library developer that does need this, but for the users who do want to use said library too.

AtlaStar commented 1 year ago

Part of the tags argument which I feel should be seriously considered;

A tool can be made right now that updates tags as the tags field is an optional argument in the .yy file. So this can be automated as a fix for most of the cases you presented. Take for instance the following example

image

The above is merely a function that exists in a script wrapper, and is not by itself it's own script asset for context. What this shows that a parser over the .yy file could be used to find and annotate script functions that are contained within script files dynamically, or even modify the .yy if necessary, which would make for a smoother transition to more proper tag annotating for things like functions, and better tag usability, in the future. Not only that but tags on functions allows processing metadata to ensure things like a constructor is being passed as a callback by only accepting functions with that annotation, which alleviates using some of the current hacky mechanisms that are used to fulfill those data contracts. It also means that it could ignore 'anonymous' styles of functions which the engine provides an anonymous name for.

As I have also mentioned in discord, tags act similar to a super set of an asset_get_all function already if used as such, or the exact same, the only issue is that their usability currently sucks because groups with tags don't propagate into their contents, or you have to use some sort of tool to manage things for you. This obviously shouldn't be a requirement, but it bridges a gap between now and whenever tags get improved.

YellowAfterlife commented 1 year ago

This would be very welcome - in GMLive I have to index all assets and also all globally exposed functions (when there are multiple in one script), and I very much do not want to resort to shipping the user a pre-build script that collects the resources from a YYP / GML files.

DAndrewBox commented 1 year ago

I literally requested the same on https://github.com/YoYoGames/GameMaker-Bugs/issues/3236 (your description is better tho) but got closed lol

tabularelf commented 4 months ago

We've got asset_get_ids, which fulfil the above request.

YYDan commented 4 months ago

Yes, this should have been closed as a duplicate a long time ago ;)

See https://github.com/YoYoGames/GameMaker-Bugs/issues/2964 for the info on that function above, which was released in 2023.11.