Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 25 Vote(s) - 4.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Us To Improve the Wiki!
Your Computer Offline
SCAN ME!

Posts: 3,456
Threads: 32
Joined: Jul 2011
Reputation: 235
RE: Help Us To Improve the Wiki!

(08-17-2011, 02:01 AM)Apjjm Wrote: String lists (and hence int and float by reference to localVars) using a delimited string to prevent leaking vars. It might be worth using the wiki page which would contain the implementation to document the functions instead of the code file, because the documentation takes up a massive amount of space and is the main reason i'm using folding regions.

I've been in need of a string list that can be restored from the game session, but i wanted something more flexible and self-contained than what you have provided, and so i took most of your code and converted it into a class with many modifications here and there.

Since returning 0 for "this" or calling the destructor within the constructor is not practical, the string lists are shared when two or more StringList objects are instantiated with the same StringList name, and therefore are capable of erasing each others data if one is not careful. This, however, is not a problem for my use (or anyone that knows what they're doing).

The current code isn't fully tested, but it compiles fine and what has been tested works. Turning everything into a class lets me overload certain operators (which cuts down the amount of code i have to write) and contains everything within its own scope.

//{ StringList
class StringList
{
    //{ Private members
    private string __DELIMITER;
    private string __NAME;
    private string __PREFIX;
    //}

    //{ Constructors
    /*    Best if this constructor is not used, as it'll clutter the save file (or map session) unnecessarily
     *    if multiple StringLists are instantiated using this constructor. Use StringList(string &in) instead.
     */
    StringList()
    {
        // Random name for random stringlists
        string rand;
        this.genRandName(rand);
        StringList(rand);
    }

    StringList(string &in strlist_name)
    {
        // In case people don't know what they're doing!
        if (strlist_name == "")
            this.genRandName(strlist_name);

        // Prepare StringList private members
        this.__PREFIX = "_!strlist!_";
        this.__DELIMITER = "\0";
        this.__NAME = this.__PREFIX + strlist_name;

        // Debugging purposes only; accessing the same StringList shouldn't be a problem,
        // (unless people forget what they're doing!)
        if (GetLocalVarInt(this.__NAME + "_exist") == 0)
            SetLocalVarInt(this.__NAME + "_exist", 1);

        else AddDebugMessage("WARNING: StringList("+this.__NAME+"): A list with the same name already exists!", false);

        /*    No need to set any other local map variables,
         *    in case we're re-using an existing StringList,
         *    else they'll default to 0 or an empty string anyway.
         */
    }
    //}

    //{ Destructor
    /*    This is the only noticable conflict between sharing StringLists.
     *
     *    If the programmer forgets, e.g., that the StringList they're using is within the global scope
     *    and instantiates a new StringList using the same name, the destructor will be called for the local
     *    StringList and it will therefore erase the global StringList's data as well.
     *
     *    They better hope they have debug messages enabled!
     */
    ~StringList()
    {
        // "Destory" (clear) the StringList when no longer in use
        SetLocalVarInt(this.__NAME + "_exist", 0);
        this.clear();
    }
    //}

    //{ Item removal
    void clear()
    {
        SetLocalVarInt(this.__NAME + "_count", 0);
        SetLocalVarString(this.__NAME, "");
    }

    void removeItem(int idx)
    {
        this.takeAt(idx);
    }

    void removeItem(string &in str_item)
    {
        this.opSubAssign(str_item);
    }

    // Removes item at idx from the list and returns it
    string takeAt(int idx)
    {
        if (this.count() == 0)
        {
            AddDebugMessage("WARNING: StringList("+this.__NAME+").takeAt("+idx+"): List is empty; returning empty string!", false);
            return "";
        }

        string new_list = "";
        string op;
        int count = 0;

        string[] str_items;
        this.items(str_items);

        for(int i = 0; i < str_items.length(); i++)
        {
            if(i != idx)
            {
                new_list += str_items[i] + this.__DELIMITER;
                count++;
            }

            else op = str_items[i];
        }

        SetLocalVarInt(this.__NAME + "_count", count);
        SetLocalVarString(this.__NAME, new_list);

        return op;
    }

    //{ Subtract Assignment operator
    // Called when trying to do something like: StringList -= "String to remove"
    StringList@ opSubAssign(string &in str_item)
    {
        string new_list = "";
        int count = 0;

        string[] str_items;
        this.items(str_items);

        for (int i = 0; i < str_items.length(); i++)
        {
            if (str_items[i] != str_item)
            {
                new_list += str_items[i] + this.__DELIMITER;
                count++;
            }
        }

        SetLocalVarInt(this.__NAME + "_count", count);
        SetLocalVarString(this.__NAME, new_list);

        return this;
    }
    //}
    //}

    //{ StringList size
    int count() const
    {
        return GetLocalVarInt(this.__NAME + "_count");
    }

    int length() const
    {
        return this.count();
    }

    int size() const
    {
        return this.count();
    }
    //}

    //{ Item search/retrieval
    int indexOf(string &in str_item)
    {
        int index = -1;

        string[] str_items;
        this.items(str_items);

        for (int i = 0; i < str_items.length(); i++)
        {
            if (str_item == str_items[i])
                return i;
        }

        return index;
    }

    string itemAt(int idx)
    {
        return this.opIndex(idx);
    }

    // private keyword seems to force a void return type;
    // relying on &out instead.
    private void items(string[] &out output) const
    {
        string str = GetLocalVarString(this.__NAME);
        int outputIndex = 0;

        if (!StringContains(str, this.__DELIMITER))
            return;

        uint8 dlm = this.__DELIMITER[0];
        int last = 0;

        for (int i = 0; i < str.length(); i++)
        {
            if (str[i] == dlm)
            {
                int len = i - last;

                if (len >= 0)
                {
                    output.resize(outputIndex+1);
                    output[outputIndex] = StringSub(str,last,len);
                    outputIndex++;
                }

                last = i+1;
            }
        }
    }
    //}

    //{ Add Assignment operator
    // Called when trying to do something like: StringList += "String to be appended"
    StringList@ opAddAssign(string &in str_item)
    {
        // What's the point of adding an empty string?
        if (str_item == "")
        {
            AddDebugMessage("WARNING: StringList("+this.__NAME+")+=: Empty string found; rejecting item!", false);
            return this;
        }

        AddLocalVarInt(this.__NAME + "_count", 1);
        AddLocalVarString(this.__NAME, str_item + this.__DELIMITER);
        return this;
    }

    // Called when trying to do something like: StringList += {"String to be appended", "String to be appended"}
    StringList@ opAddAssign(string[] &in str_items)
    {
        // What's the point of adding an empty list?
        if (str_items.length() == 0)
        {
            AddDebugMessage("WARNING: StringList("+this.__NAME+")+=: Empty string array found; rejecting array!", false);
            return this;
        }

        for (int i = 0; i < str_items.length(); i++)
        {
            if (str_items[i] != "")
            {
                AddLocalVarInt(this.__NAME + "_count", 1);
                AddLocalVarString(this.__NAME, str_items[i] + this.__DELIMITER);
            }

            else AddDebugMessage("WARNING: StringList("+this.__NAME+")+=: Empty string found; rejecting item!", false);
        }

        return this;
    }

    // Called when trying to do something like: StringList += StringList
    StringList@ opAddAssign(const StringList &in str_list)
    {
        // What's the point of adding an empty list?
        if (str_list.count() == 0)
        {
            AddDebugMessage("WARNING: StringList("+this.__NAME+")+=: Empty string list found; rejecting list!", false);
            return this;
        }

        for (int i = 0; i < str_list.count(); i++)
        {
            if (str_list[i] != "")
            {
                AddLocalVarInt(this.__NAME + "_count", 1);
                AddLocalVarString(this.__NAME, str_list[i] + this.__DELIMITER);
            }

            else AddDebugMessage("WARNING: StringList("+this.__NAME+")+=: Empty string found; rejecting item!", false);
        }

        return this;
    }
    //}

    //{ Item/List replacement
    void replace(string &in str_item, string &in with)
    {
        string[] str_items;
        this.items(str_items);

        for (int i = 0; i < str_items.length(); i++)
        {
            if (str_item == str_items[i])
            {
                str_items[i] = with;
                break;
            }
        }
    }

    //{ Assignment operator
    // Called when trying to do something like: StringList = "String to completely replace the list"
    StringList@ opAssign(string &in str_item)
    {
        if (str_item != "")
        {
            SetLocalVarInt(this.__NAME + "_count", 0);
            SetLocalVarString(this.__NAME, "");
            return this.opAddAssign(str_item);
        }

        else
        {
            AddDebugMessage("WARNING: StringList("+this.__NAME+")=: Empty string found; rejecting item!", false);
            return this;
        }
    }

    // Called when trying to do something like: StringList = StringList
    StringList@ opAssign(const StringList &in str_list)
    {
        if (str_list.count() != 0)
        {
            SetLocalVarInt(this.__NAME + "_count", 0);
            SetLocalVarString(this.__NAME, "");
            return this.opAddAssign(str_list);
        }

        else
        {
            AddDebugMessage("WARNING: StringList("+this.__NAME+")=: Empty string list found; rejecting list!", false);
            return this;
        }
    }

    // Called when trying to do something like: StringList[i] = "string to insert at index i"
    void set_opIndex(int idx, string &in str_item)
    {
        if (idx >= 0 && idx < this.count())
            this.replace(this.itemAt(idx), str_item);

        else if (idx == this.count())
            this.opAddAssign(str_item);

        else AddDebugMessage("WARNING: StringList("+this.__NAME+")["+idx+"]=: Index is out of supported range; rejecting assignment!", false);
    }
    //}
    //}

    //{ Equals operator
    // Called when trying to do something like: StringList == StringList
    bool opEquals(const StringList &in compare)
    {
        return (this.__NAME == compare.name());
    }

    // Called when trying to do something like: StringList == "String"
    bool opEquals(string &in compare)
    {
        int idx = this.indexOf(compare);
        return (idx != -1);
    }
    //}

    //{ Index operator
    // Called when trying to do something like: StringList[i]
    string opIndex(int idx) const
    {
        if (this.count() == 0)
        {
            AddDebugMessage("WARNING: StringList("+this.__NAME+")["+idx+"]: List is empty; returning empty string!", false);
            return "";
        }

        string item = "";

        if (idx >= 0 && idx < this.count())
        {
            string[] names;
            this.items(names);

            // if(idx < names.length())
            item = names[idx];
        }

        return item;
    }
    //}

    //{ Shift Left operator
    // Called when trying to do something like: StringList << "String to be appended"
    StringList@ opShl(string &in str_item)
    {
        return this.opAddAssign(str_item);
    }

    // Called when trying to do something like: StringList << {"String to be appended", "String to be appended"}
    StringList@ opShl(string[] &in str_items)
    {
        return this.opAddAssign(str_items);
    }

    // Called when trying to do something like: StringList << StringList
    StringList@ opShl(const StringList &in str_list)
    {
        return this.opAddAssign(str_list);
    }
    //}

    //{ Debugging
    // Print all items to screen and hpl.log
    // raw: should we also print the local map variable string?
    void print(bool raw)
    {
        string[] str_items;
        this.items(str_items);

        if (raw)
        {
            AddDebugMessage(this.rawValue(), false);
            Print("Printing raw value from "+this.__NAME+": " + this.rawValue() + "\n");
        }

        Print("\n\nPrinting items from "+this.__NAME+":\n");
        for (int i = 0; i < str_items.length(); i++)
        {
            AddDebugMessage(i+": "+str_items[i], false);
            Print(i+": "+str_items[i] + "\n");
        }
        Print("\n");
    }
    //}

    //{ StringList property retrieval
    string name() const
    {
        return this.__NAME;
    }

    string rawValue() const
    {
        return GetLocalVarString(this.__NAME);
    }
    //}

    //{ Misc
    // private keyword seems to force a void return type;
    // relying on &out instead.
    private void genRandName(string &out rand)
    {
        string salt = "abcdefghijklmnopqrstuvwxyz1234567890";
        rand = salt[RandInt(0, salt.length() - 1)]
             + salt[RandInt(0, salt.length() - 1)]
             + salt[RandInt(0, salt.length() - 1)]
             + salt[RandInt(0, salt.length() - 1)]
             + salt[RandInt(0, salt.length() - 1)]
             + salt[RandInt(0, salt.length() - 1)]
             + salt[RandInt(0, salt.length() - 1)]
             + salt[RandInt(0, salt.length() - 1)];
        // return rand;
    }
    //}
}
//}

Example code:

PHP Code: (Select All)
StringList string_list("TEST_LIST");

// Add values to the list
string_list += "Test string 1";
string_list += "Test string 2";
string[] string_array = {"Test string 3""Test string 4"};
string_list += string_array;
string_list << "Test string 5"
            
<< ""// empty strings are rejected and so not stored in the list

// Print items and value to screen and hpl.log
string_list.print(true);

// Check if "Test string 1" is found in the list
if (string_list == "Test string 1")
    Print(
"\n\nContains Test string 1\n\n");

// Remove "Test string 1" from the list
string_list -= "Test string 1";

// Print items and value to screen and hpl.log
string_list.print(true); 

Tutorials: From Noob to Pro
(This post was last modified: 08-02-2012, 10:14 PM by Your Computer.)
07-28-2012, 05:40 PM
Website Find


Messages In This Thread
Help Us To Improve the Wiki! - by Tanshaydar - 06-20-2011, 04:05 PM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-13-2011, 01:51 PM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 07-13-2011, 04:49 PM
RE: Help Us To Improve the Wiki! - by Kyle - 07-13-2011, 02:23 PM
RE: Help Us To Improve the Wiki! - by xtron - 07-13-2011, 04:25 PM
RE: Help Us To Improve the Wiki! - by xtron - 07-13-2011, 05:02 PM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-13-2011, 05:07 PM
RE: Help Us To Improve the Wiki! - by Kyle - 07-13-2011, 05:15 PM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-13-2011, 05:24 PM
RE: Help Us To Improve the Wiki! - by xtron - 07-13-2011, 09:37 PM
RE: Help Us To Improve the Wiki! - by Kyle - 07-13-2011, 09:40 PM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-13-2011, 10:50 PM
RE: Help Us To Improve the Wiki! - by xtron - 07-13-2011, 11:16 PM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-14-2011, 12:12 AM
RE: Help Us To Improve the Wiki! - by Kyle - 07-14-2011, 12:50 AM
RE: Help Us To Improve the Wiki! - by xtron - 07-14-2011, 01:11 AM
RE: Help Us To Improve the Wiki! - by yasar11732 - 07-14-2011, 01:54 AM
RE: Help Us To Improve the Wiki! - by Kyle - 07-14-2011, 02:06 AM
RE: Help Us To Improve the Wiki! - by xtron - 07-14-2011, 02:05 AM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 07-14-2011, 02:53 AM
RE: Help Us To Improve the Wiki! - by xtron - 07-14-2011, 03:00 AM
RE: Help Us To Improve the Wiki! - by Kyle - 07-14-2011, 03:26 AM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-14-2011, 08:57 AM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 07-14-2011, 10:13 AM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-14-2011, 10:51 AM
RE: Help Us To Improve the Wiki! - by jens - 07-14-2011, 10:53 AM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-14-2011, 11:18 AM
RE: Help Us To Improve the Wiki! - by jens - 07-14-2011, 11:36 AM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-14-2011, 12:07 PM
RE: Help Us To Improve the Wiki! - by xtron - 07-14-2011, 12:09 PM
RE: Help Us To Improve the Wiki! - by xtron - 07-14-2011, 01:34 PM
RE: Help Us To Improve the Wiki! - by Kyle - 07-14-2011, 03:03 PM
RE: Help Us To Improve the Wiki! - by xtron - 07-14-2011, 03:47 PM
RE: Help Us To Improve the Wiki! - by Kyle - 07-14-2011, 06:09 PM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-14-2011, 11:51 PM
RE: Help Us To Improve the Wiki! - by xtron - 07-14-2011, 11:54 PM
RE: Help Us To Improve the Wiki! - by Kyle - 07-14-2011, 11:55 PM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 07-15-2011, 12:08 AM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-15-2011, 01:14 AM
RE: Help Us To Improve the Wiki! - by Kyle - 07-15-2011, 01:31 AM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-15-2011, 01:36 AM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 07-15-2011, 01:40 AM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-15-2011, 10:31 AM
RE: Help Us To Improve the Wiki! - by xtron - 07-15-2011, 10:57 AM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-15-2011, 11:11 AM
RE: Help Us To Improve the Wiki! - by Rel - 07-18-2011, 05:01 PM
RE: Help Us To Improve the Wiki! - by Kyle - 07-18-2011, 05:05 PM
RE: Help Us To Improve the Wiki! - by Rel - 07-18-2011, 05:18 PM
RE: Help Us To Improve the Wiki! - by Kyle - 07-18-2011, 05:38 PM
RE: Help Us To Improve the Wiki! - by Rel - 07-18-2011, 05:57 PM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-18-2011, 06:19 PM
RE: Help Us To Improve the Wiki! - by Rel - 07-18-2011, 06:24 PM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-18-2011, 06:33 PM
RE: Help Us To Improve the Wiki! - by Rel - 07-18-2011, 07:43 PM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 07-21-2011, 05:48 AM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 07-21-2011, 06:01 AM
RE: Help Us To Improve the Wiki! - by plutomaniac - 07-21-2011, 08:23 PM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 07-22-2011, 03:04 AM
RE: Help Us To Improve the Wiki! - by Rel - 07-22-2011, 04:15 AM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 07-22-2011, 04:34 AM
RE: Help Us To Improve the Wiki! - by Apjjm - 08-16-2011, 08:12 PM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 08-16-2011, 08:23 PM
RE: Help Us To Improve the Wiki! - by Apjjm - 08-17-2011, 02:01 AM
RE: Help Us To Improve the Wiki! - by Your Computer - 07-28-2012, 05:40 PM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 08-17-2011, 08:49 AM
RE: Help Us To Improve the Wiki! - by plutomaniac - 08-17-2011, 09:36 AM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 08-17-2011, 08:06 PM
RE: Help Us To Improve the Wiki! - by plutomaniac - 08-17-2011, 09:56 PM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 08-17-2011, 10:06 PM
RE: Help Us To Improve the Wiki! - by plutomaniac - 08-17-2011, 10:08 PM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 08-18-2011, 09:07 AM
RE: Help Us To Improve the Wiki! - by jens - 08-18-2011, 09:56 AM
RE: Help Us To Improve the Wiki! - by Elven - 08-21-2011, 10:23 PM
RE: Help Us To Improve the Wiki! - by JetlinerX - 08-22-2011, 04:54 AM
RE: Help Us To Improve the Wiki! - by Elven - 08-22-2011, 10:01 AM
RE: Help Us To Improve the Wiki! - by Apjjm - 09-05-2011, 06:13 PM
RE: Help Us To Improve the Wiki! - by Elven - 10-19-2011, 08:13 PM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 10-19-2011, 09:13 PM
RE: Help Us To Improve the Wiki! - by Elven - 10-19-2011, 10:17 PM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 10-19-2011, 10:52 PM
RE: Help Us To Improve the Wiki! - by Elven - 10-19-2011, 10:59 PM
RE: Help Us To Improve the Wiki! - by Nevicar - 03-11-2012, 04:50 AM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 03-11-2012, 05:34 AM
RE: Help Us To Improve the Wiki! - by Nevicar - 03-11-2012, 05:54 AM
RE: Help Us To Improve the Wiki! - by ClayPigeon - 03-15-2012, 12:17 PM
RE: Help Us To Improve the Wiki! - by Stepper321 - 03-16-2012, 05:37 PM
RE: Help Us To Improve the Wiki! - by Tanshaydar - 03-16-2012, 06:18 PM
RE: Help Us To Improve the Wiki! - by Stepper321 - 03-16-2012, 07:54 PM
RE: Help Us To Improve the Wiki! - by jessehmusic - 04-06-2012, 04:22 PM
RE: Help Us To Improve the Wiki! - by Homicide13 - 05-07-2012, 01:26 AM
RE: Help Us To Improve the Wiki! - by Apjjm - 05-07-2012, 02:35 AM
RE: Help Us To Improve the Wiki! - by Homicide13 - 05-07-2012, 03:01 AM
RE: Help Us To Improve the Wiki! - by Traggey - 05-19-2012, 02:13 AM
RE: Help Us To Improve the Wiki! - by Apjjm - 06-12-2012, 03:34 AM
RE: Help Us To Improve the Wiki! - by jens - 06-12-2012, 06:57 AM
RE: Help Us To Improve the Wiki! - by Finzy - 08-04-2012, 03:33 PM
RE: Help Us To Improve the Wiki! - by The chaser - 10-26-2012, 04:31 PM
RE: Help Us To Improve the Wiki! - by Racingcreed - 12-08-2012, 08:21 PM



Users browsing this thread: 14 Guest(s)