Author Topic: Arrays and storing data?  (Read 1052 times)

Some questions:
1. How do arrays work in Torquescript? How do I push to them and "pull" from them (delete a specific piece of data in it)
2. How can I store data in a .txt file and then have Torque read it line-by-line?
3. How can I have Torquescript search for data in the .txt file?
4. Do I even need a .txt file to store data that will appear again on a restart of Blockland?

First one: Arrays don't technically exist in Torquescript. They used like this: %Variablename[%Arrayindex] but they're special in that you don't actually need to use an integer for %arrayindex, you can use a fraction, negative number, even a string with spaces in it as the index, and it'll just map it to a variable name.

For example, %var[5] is the same as %var5 and %var["hello"] is the same as %varhello.

%var["hello"]... is the same as %varhello.
This specifically is the reason why they "don't technically exist"
It's not very important to know a ton about why, other than what it means for you: there's a lot of things you can't do with arrays in TorqueScript that you can in other languages, like passing them as arguments or returning them from functions

EDIT: whoops i kinda messed up my quote snipping

2. How can I store data in a .txt file and then have Torque read it line-by-line?
Code: [Select]
%file = new fileObject();
%file.openForWrite("My/File/Path.txt");

%file.writeLine("Stuff here.");

%file.close();
%file.delete(); //Not deleting the file. Deleting the object that we were using to write in the file.
openForWrite will delete all the contents of the text file and then you can write whatever you want. openForAppend will just write whatever you want at the end of the text file.
3. How can I have Torquescript search for data in the .txt file?
Code: [Select]
%file = new fileObject();
%file.openForRead("My/File/Path.txt");

while(!%file.isEOF())
{
        %line = %file.readLine();
}

%file.close();
%file.delete();
This will read the file line by line. There's no other way to read files in TorqueScript.
4. Do I even need a .txt file to store data that will appear again on a restart of Blockland?
Yes. You can do one of three things.
1. Use the above methods.
2. Use global variables and do export("$MyGlobal::Var"); (use * as a wild card) and then use exec(); to execute the file when you want to load the variables.
3. There is a default way that's method 2. Just make a global variable named $Pref::Whatever(client sided) or $Pref::Server::Whatever(server sided) and it'll automatically be stored and executed.

If you want to go with global variables then I suggest using method 2 if you're storing a lot of data.

Using export() on a large number of variables is also much faster than writing the file yourself.

One potential issue you can run into using export is that it cannot be executed if you use a variable name with a space in it (which can only happen through arrays, eg $myArray["With Space"] ). It will write to the file as $myArrayWith Space = "value"; which when executed, causes a syntax error. Also has the same effect using other symbols, eg - + * / ? % $ . etc

$myArray["With Space"]
Yeah but that's something that should be avoided in general.
Even if it worked fine, it just looks... gross

There's other problem causing characters that are more likely to be used.
For example, the old version of RTB had a feature that saved some sort of player data (don't remember what) with an exported and execed array indexed by IP address, but the periods in the IPs caused error on exec.
There's little need for IP address based saving now that we have BLIDs, but you never know when someone will need/think they need an IP based saving system