GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2007-11-29 17:24:11

paul23
Member
Registered: 2007-10-17
Posts: 110

resource loaders

Well some time ago there was a "small" topic by smarty about that we shouldn't use the execute_string() function. - However the problem was that it was hard to get the resource ID when you had a resource name, that's why smarty "made" (in pseudo code) this script..
I was a bit surprised it wasn't added here, since when used correctly it's very powerfull:

/*
**  Usage:
**      load_sprites(ds_map)
**
**  Arguments:
**      ds_map      map to which all sprites area loaded.
**
**  Notes:
**      fills the map with name,index values 
**      (with as key the name used in gm's editor!)
**
**  Returns:
**      nothing
**
**  GMLscripts.com
*/
var no,i,ds_map;
ds_map = argument0
no = sprite_create_from_screen(0,0,1,1,false,false,false,false,0,0);
sprite_delete(no);
for (i=0;i<no;i+=1) {
    if (sprite_exists(i)) {
        ds_map_add(ds_map,sprite_get_name(i),i);
    }
}
/*
**  Usage:
**      load_sounds(ds_map,ex_sound)
**
**  Arguments:
**      ds_map      map to which all sprites area loaded.
**      ex_sound    filename from an external sound.
**
**  Notes:
**      fills the map with name,index values 
**      (with as key the name used in gm's editor!)
**      The loaded sound is "unloaded" directly, and only
**      used to get the max index number
**
**  Returns:
**      nothing
**
**  GMLscripts.com
*/
var no,i,ds_map;
ds_map = argument0
no = sound_add(argument1,0,false);
if (no != -1)
{
    sound_delete(no);
    for (i=0;i<no;i+=1) {
        if (sound_exists(i)) {
            ds_map_add(ds_map,sprite_get_name(i),i);
        }
    }
}
/*
**  Usage:
**      load_backgrounds(ds_map)
**
**  Arguments:
**      ds_map      map to which all backgrounds area loaded.
**
**  Notes:
**      fills the map with name,index values 
**      (with as key the name used in gm's editor!)
**
**  Returns:
**      nothing
**
**  GMLscripts.com
*/
var no,i,ds_map;
ds_map = argument0
no = background_create_color(1,1,c_white,false);
background_delete(no);
for (i=0;i<no;i+=1){
    if (background_exists(i)) {
        ds_map_add(ds_map,background_get_name(i),i)
    }
}
/*
**  Usage:
**      load_pathds(ds_map)
**
**  Arguments:
**      ds_map      map to which all paths area loaded.
**
**  Notes:
**      fills the map with name,index values 
**      (with as key the name used in gm's editor!)
**
**  Returns:
**      nothing
**
**  GMLscripts.com
*/
var no,i,ds_map;
ds_map = argument0
no = path_add();
path_delete(no);
for (i=0;i<no;i+=1) {
    if (path_exists(i)) {
        ds_map_add(argument0,path_get_name(i),i)
    }
}
/*
**  Usage:
**      load_fonts(ds_map)
**
**  Arguments:
**      ds_map      map to which all fonts area loaded.
**
**  Notes:
**      fills the map with name,index values 
**      (with as key the name used in gm's editor!)
**
**  Returns:
**      nothing
**
**  GMLscripts.com
*/
var no,i,ds_map;
ds_map = argument0
no = font_add("ariel",12,false,false,32,32);
font_delete(no);
for (i=0;i<no;i+=1) {
    if (path_exists(i)) {
        ds_map_add(ds_map,path_get_name(i),i)
    }
}
/*
**  Usage:
**      load_timelines(ds_map)
**
**  Arguments:
**      ds_map      map to which all timelines area loaded.
**
**  Notes:
**      fills the map with name,index values 
**      (with as key the name used in gm's editor!)
**
**  Returns:
**      nothing
**
**  GMLscripts.com
*/
var no,i,ds_map;
ds_map = argument0
no = timeline_add();
timeline_delete(no);
for (i=0;i<no;i+=1) {
    if (timeline_exists(i)) {
        ds_map_add(ds_map,timeline_get_name(i),i)
    }
}
/*
**  Usage:
**      load_objects(ds_map)
**
**  Arguments:
**      ds_map      map to which all objects area loaded.
**
**  Notes:
**      fills the map with name,index values 
**      (with as key the name used in gm's editor!)
**
**  Returns:
**      nothing
**
**  GMLscripts.com
*/
var no,i,ds_map;
ds_map = argument0
no = object_add();
object_delete(no);
for (i=0;i<no;i+=1) {
    if (object_exists(i)) {
        ds_map_add(ds_map,object_get_name(i),i);
    }
}
/*
**  Usage:
**      load_sprites(ds_map)
**
**  Arguments:
**      ds_map      map to which all sprites area loaded.
**
**  Notes:
**      fills the map with name,index values 
**      (with as key the name used in gm's editor!)
**      For getting the max number it creates an extra room.
**      This room index will be returned (and it's name will
**      be added unless you have already a room with the same name
**
**  Returns:
**      nothing
**
**  GMLscripts.com
*/
var no,i,ds_map;
ds_map = argument0
no = room_add();
for (i=0;i<no;i+=1) {
    if (room_exists(i)) {
        ds_map_add(ds_map,room_get_name(i),i);
    }
}
if (!ds_map_exists(ds_map,room_get_name(no))) ds_map_add(ds_map,room_get_name(no),i);
return no;

As you can see most are 90% the same: only the "sounds" script is slightly different: it has to get an external sound to succesfully add a new sound, else it won't work. And the "room" script is different: you can't delete a room- and the if statement is to prevent your map from messing up if you created a room with name __newroomX already).

Last edited by paul23 (2007-12-14 04:02:12)

Offline

#2 2007-12-06 10:44:25

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

Re: resource loaders

These look really powerful. Sorry I haven't replied sooner, I've been on vacation. Now lots of work and catching up to do with script submissions. I'll definitely be adding these to GMLscripts.com in a future update ... which may be a while. Thanks!


Abusing forum power since 1986.

Offline

#3 2007-12-09 13:47:45

Grand-High Gamer
Member
Registered: 2007-10-09
Posts: 18

Re: resource loaders

Nice dude. Thanks for submitting them.


sigimage1.png

Offline

#4 2008-01-01 03:18:13

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

Re: resource loaders

Wouldn't this work better for rooms?

/*
**  Usage:
**      map_rooms(ds_map)
**
**  Arguments:
**      ds_map      map to which all rooms are loaded
**
**  Notes:
**      fills the map with {key=name, val=index} pairs
**
**  Returns:
**      nothing
**
**  GMLscripts.com
*/
{
    var ds_map,i;
    ds_map = argument0;
    i = room_first;
    while (room_exists(i)) {
        ds_map_add(ds_map,room_get_name(i),i);
        i = room_next(i);
    }
}

Abusing forum power since 1986.

Offline

#5 2008-01-01 07:40:01

paul23
Member
Registered: 2007-10-17
Posts: 110

Re: resource loaders

Well I'm not entirely sure it works (though it should), but I guess you tested it already?..

A small update wfor that then:
while (room_exists(i)) you could change by while (i!=-1)

Offline

#6 2008-01-01 16:54:12

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

Re: resource loaders

Seems to work, although I haven't tested it extensively.


Abusing forum power since 1986.

Offline

#8 2008-01-07 15:26:49

Schreib
Member
Registered: 2007-10-12
Posts: 11

Re: resource loaders

These are excellent and very powerful scripts. Thanks for writing them, paul. I will certainly use them in the future.

The only suggestion I have is to add a filter to which some resource names will not be loaded. For example, if an argument filter "noload" is given, the scripts will not load those resources. For example:

/*
**  Usage:
**      load_sprites(ds_map)
**
**  Arguments:
**      ds_map      map to which all sprites area loaded.
**
**  Notes:
**      fills the map with name,index values
**      (with as key the name used in gm's editor!)
**
**  Returns:
**      nothing
**
**  GMLscripts.com
*/
var no,i,ds_map,filter;
ds_map = argument0;
filter = argument1;
no = sprite_create_from_screen(0,0,1,1,false,false,false,false,0,0);
sprite_delete(no);
for (i=0;i<no;i+=1) {
    if (sprite_exists(i) && string_count(filter,sprite_get_name(i)) == 0) {
        ds_map_add(ds_map,sprite_get_name(i),i);
    }
}

Edit: Oops, xot you better fix the ampersands problem wink

Last edited by Schreib (2008-01-07 17:01:06)

Offline

#9 2008-01-07 20:49:32

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

Re: resource loaders

Oh, wow, that's weird. I can't believe that hasn't popped up before. I'll get right on it, boss!


Abusing forum power since 1986.

Offline

#10 2008-04-08 01:49:10

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

Re: resource loaders

There is a problem with the alternate map_rooms() script I suggested: it doesn't work if rooms have been dynamically added at run-time.

room_add() Adds a new room. It returns the index of the room. Note that the room will not be part of the room order. So the new room does not have a previous or a next room. If you want to move to an added room you must provide the index of the room.

I have replaced the version on GMLscripts.com with paul23's original.

/*
**  Usage:
**      map_rooms(ds_map)
**
**  Arguments:
**      ds_map      map to which all rooms are loaded.
**
**  Notes:
**      Fills the map with {key=name, val=index} pairs.
**      To find the maximum number of rooms, it creates an extra
**      room. The name of this room will be added to the map
**      unless a room with the same name already exists.
**
**  Returns:
**      the index of the created room
**
**  GMLscripts.com
*/
{
    var no,i,ds_map;
    ds_map = argument0
    no = room_add();
    for (i=0;i<no;i+=1) {
        if (room_exists(i)) {
            ds_map_add(ds_map,room_get_name(i),i);
        }
    }
    if (!ds_map_exists(ds_map,room_get_name(no))) ds_map_add(ds_map,room_get_name(no),i);
    return no;
}

Abusing forum power since 1986.

Offline

#11 2008-05-13 17:05:04

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

Re: resource loaders

On the other hand, dynamically added rooms don't have a "name" to begin with, so there would be little point in trying to map them. I guess room_first() and room_next() offer an acceptable alternative.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB