GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2010-11-06 01:32:07

sigonasr2
Hard Code that Works
Registered: 2010-11-06
Posts: 1
Website

open_variables

Hello everyone, this script is one that I use often in games that deal with lists. I like to keep things organized in lists with strings, or sometimes use strings to add and remove from items and decided to make this script to parse a string with separated values of the same type of delimiter. This allows for someone to generate lists of items through loops, as this script returns the number of variables that it has parsed. The script is kind of messy as I have not removed the 'debugging' mode, however I thought it might be useful to others who may wish to use it, improve upon it, or provide suggestions on it.

I am nowhere near an efficient coder, and people tend to outsmart me in these kinds of things all the time. Other than the obvious scrapping of the debugging if statements, it would also be nice if I can get feedback on how to improve future versions of scripts like these because I just love to learn about new things I haven't found. Thanks for your time all, hope you find a use for the script as much as I do.

/*
**  Usage:
**      val = open_variables(string,delimiter[,debug,commas]);
**
**
**  Arguments:
**      string      A string containing a list with a delimiter separating all
**                  values.
**      delimiter   A string containing just the delimiter in which all values
**                  are separated by.
**      debug       (Optional) Either true or false. Returns debug messages of
**                  the script process.
**      commas      (Optional) Either true or false. Returns comma positions
**                  that each value is separated from. (1 is the first
**                  character in the string.)
**
**  Returns:
**      Number of variables that were separated. (An integer)
**
**{{AND}}
**
**      Var[1],Var[2],Var[3],...,Var[X]
**              The X'th string from argument0. (A string)
**
**{{AND}}
**
**      [[Only if commas is set to true]]
**        comma[1],comma[2],comma[3],...,comma[X]
**              The X'th position in the string from which a comma was found.
**              (An integer)
**
**
**
**  Notes:
**      This script is useful when you are building lists and you need to parse
**      them for use in-game. For example, a list like "Banana,Cherry,Walnuts"
**      will can be parsed with this script to provide each individual value
**      separated by commas. The number of variables is returned so that you
**      can store it and then run loops with that number value repeating to get
**      all of your data computed and interpreted for usage in your game.
**
*/

{
    //Set any variables we will use in loops and such.
    var a,newstring,variables,pos1,cutstring,cc;
    for (cc=0;cc<31999;cc+=1) {
        comma[cc]=-1;
        Var[cc]="";
    }
    a=1;
    newstring=argument0;

    //First let us get how many variables we will create from this input
    //string.
    variables=string_count(argument1,argument0)+1;

    //If debugging enabled, we will display a message.
    if (argument2=true) {
        show_message("We will create " + string(variables) + " variables.");
    }

    repeat (variables) {
        //Get the value from the beginning to the first comma.
        pos1=string_pos(argument1,newstring);
        if (pos1=0) {
            pos1=9999999;
        }
        if (argument2=true) {
            show_message("The first comma is at position " +string(pos1));
        }
        if (argument3=true and comma[a-1]!=-1) {
            comma[a]=comma[a-1]+pos1;
            if (argument2=true) {
                show_message("Comma "+string(a)+" is at position "
                +string(comma[a])+
                " of the original string.");
            }
        } else {
            comma[a]=pos1;
            if (argument2=true) {
                show_message("Comma "+string(a)+" is at position "
                +string(comma[a])+
                " of the original string.");
            }
        }
        cutstring=string_copy(newstring,0,pos1-1);
        if (argument2=true) {
            show_message("The cut string has " + string(cutstring));
        }
        if (pos1=9999999) {
            newstring=string_copy(newstring,0,string_length(newstring));
        } else {
            newstring=string_copy(newstring,pos1+1,string_length(newstring));
        }
        if (argument2=true) {
            show_message("The next string has " + string(newstring));
        }
        Var[a]=cutstring;
        if (argument2=true) {
            show_message("Var["+string(a)+"]=" +string(cutstring));
        }
        a+=1;
    }
    return variables;
}

Offline

Board footer

Powered by FluxBB