GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2009-09-10 16:43:18

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

Variable Question [Solved]

I've written a script that will take a csv file and parse it's pieces into an array.  It works great, that's not what I need help with.

I want to make the script more modular so I can use it to create many different arrays.  The problem is, when I call the script I want to be able to name the array variable.

For example, I would call the script like this:

Expandparse_text("file.csv",arrayName); //file name, array variable to store stuff in

...and then in the script it would know to store the parsed strings into arrayName[x,y] or whatever.

Is this possible?

EDIT

So, did a bit more digging myself and discovered "execute_string".  A very nice function.  Helped me get this working.

Last edited by Pixelated_Pope (2009-09-10 17:12:54)

Offline

#2 2009-09-10 20:06:23

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

Re: Variable Question [Solved]

I would have used variable_* functions for this, rather than execute_string. In fact, I have.

The following functions exist that allow you to check whether certain variables exist and with which you can set variables and get their values. In all these functions the variable name is passed as a string!

http://www.gmlscripts.com/gm8beta2/help … _misc.html

Obviously, execute_string works, but it is a little dangerous if there is any chance that the data used by it is supplied by the user, either directly or indirectly. The arbitrary nature of the function makes you vulnerable to injection style attacks which can allow anything from cheating to identity theft. It's possible to try and untaint the input, but it's safer to avoid execute_string and any other eval() type functions completely.

http://gmc.yoyogames.com/index.php?showtopic=322492


Abusing forum power since 1986.

Offline

#3 2009-09-12 08:23:39

Flatlander
Member
Registered: 2009-09-03
Posts: 9

Re: Variable Question [Solved]

Interesting.

I have some old code I am about to rewrite and extend that calls my script 'executeUserEvent(n)'
How would you code it to avoid using execute_string() and without a long switch/case?
It's a shame there is no ev_user[n] like argument[n]. But I'm hoping for a general solution I can use elsewhere. e.g. choosePlayerSprite("FALL"+LEFT/"CLIMB"+DOWN/"CROUCH"+LEFT+CARRYING) or something. To get strings like 'sprPlayerCrouchLCarrying'.
I have a feeling I'm going about this the wrong way. Or is usage of execute_string() in cases like these 'mostly harmless' anyway? (If slower.)

Thanks in advance,
Chris

Last edited by Flatlander (2009-09-12 08:50:22)

Offline

#4 2009-09-12 09:01:42

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

Re: Variable Question [Solved]

But I'm hoping for a general solution I can use elsewhere. e.g. choosePlayerSprite("FALL"+LEFT/"CLIMB"+DOWN/"CROUCH"+LEFT+CARRYING) or something. To get strings like 'sprPlayerCrouchLCarrying'.
I have a feeling I'm going about this the wrong way.

Well, in that case, I recommend these resource management scripts by paul23.

http://www.gmlscripts.com/script/resource_management

Expand/*
**  Usage:
**      map_sprites(ds_map)
**
**  Arguments:
**      ds_map      map to which all sprites are loaded
**
**  Returns:
**      nothing, but fills the map with {key=name, val=index} pairs
**
**  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);
        }
    }
}

At the beginning of the game, call this function and it will populate the supplied map data structure with name=>id pairs.

Then to get a sprite by name:

Expandbase = "sprPlayer";
action = "Crouch";
facing = "L";
carry = "Carrying";

name = base+action+facing+carry;
sprite = ds_map_find_value(mapSprite, name);

That's fast and secure.


Abusing forum power since 1986.

Offline

#5 2009-09-12 11:41:45

Flatlander
Member
Registered: 2009-09-03
Posts: 9

Re: Variable Question [Solved]

There's something about maps I find confusing. Lack of use I suppose.
I'm sure that'll be just the job once I've got my head around it!

Thanks smile

edit:
Aaah!. Clever

Last edited by Flatlander (2009-09-12 12:02:41)

Offline

Board footer

Powered by FluxBB