You are not logged in.
Pages: 1
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
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
Nice dude. Thanks for submitting them.
Offline
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
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
Seems to work, although I haven't tested it extensively.
Abusing forum power since 1986.
Offline
These scripts have been added to the site:
http://www.gmlscripts.com/script/map_backgrounds
http://www.gmlscripts.com/script/map_fonts
http://www.gmlscripts.com/script/map_objects
http://www.gmlscripts.com/script/map_paths
http://www.gmlscripts.com/script/map_rooms
http://www.gmlscripts.com/script/map_sounds
http://www.gmlscripts.com/script/map_sprites
http://www.gmlscripts.com/script/map_timelines
Thanks!
Abusing forum power since 1986.
Offline
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
Last edited by Schreib (2008-01-07 17:01:06)
Offline
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
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
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
Pages: 1