Author Topic: Silly Torque, that's not an array!  (Read 2464 times)

So using export();, I get this

Code: [Select]
$Map::Chunks::Chunk0_0 = "34";
Of course, attempting to execute this throws a syntax error. What is an easy way to pass this
Code: [Select]
$Map::Chunks::Chunk0_0 = "34";to this
Code: [Select]
$Map::Chunks::Chunk[0,0] = "34";?

And unrelated note, attempting to execute an empty script crashes your Blockland. :o

Why is that a syntax error?

%foo[1, 2, 3] is the same as %foo1_2_3.

Also, executing an empty script should only give you a syntax error message (as a valid script is at least one top level statement).

No syntax error and no crash.


It made me crash and executing that script file threw a syntax error (the one with the arrays), which is why I posted here.

It made me crash and executing that script file threw a syntax error (the one with the arrays), which is why I posted here.
Out of curiosity, post the file here.

Code: [Select]
$Map::BLID::Sector-1_-1 = 27690;
$Map::BLID::Sector-1_0 = 27690;
$Map::BLID::Sector-1_1 = 27690;
$Map::BLID::Sector-1_2 = 27690;
$Map::BLID::Sector-2_-1 = 27690;
$Map::BLID::Sector-2_0 = 27690;
$Map::BLID::Sector-2_1 = 27690;
$Map::BLID::Sector-2_2 = 27690;
$Map::BLID::Sector0_-1 = 27690;
$Map::BLID::Sector0_0 = 27690;
$Map::BLID::Sector0_1 = 27690;
$Map::BLID::Sector0_2 = 27690;
$Map::BLID::Sector1_-1 = 27690;
$Map::BLID::Sector1_0 = 27690;
$Map::BLID::Sector1_1 = 27690;
$Map::BLID::Sector1_2 = 27690;
$Map::BLID::Sector2_-1 = 27690;
$Map::BLID::Sector2_0 = 27690;
$Map::BLID::Sector2_1 = 27690;
$Map::BLID::Sector2_2 = 27690;
$Map::Color::Sector-1_-1 = "32";
$Map::Color::Sector-1_0 = "33";
$Map::Color::Sector-1_1 = "32";
$Map::Color::Sector-1_2 = "33";
$Map::Color::Sector-2_-1 = "33";
$Map::Color::Sector-2_0 = "32";
$Map::Color::Sector-2_1 = "33";
$Map::Color::Sector-2_2 = "32";
$Map::Color::Sector0_-1 = "33";

$Map::Color::Sector0_1 = "33";
$Map::Color::Sector0_2 = "32";
$Map::Color::Sector1_-1 = "32";
$Map::Color::Sector1_0 = "33";
$Map::Color::Sector1_1 = "32";
$Map::Color::Sector1_2 = "33";
$Map::Color::Sector2_-1 = "33";
$Map::Color::Sector2_0 = "32";
$Map::Color::Sector2_1 = "33";
$Map::Color::Sector2_2 = "32";
$Map::Height::Sector-1_-1 = -10.8333;
$Map::Height::Sector-1_0 = 0.833333;
$Map::Height::Sector-1_1 = 12.5;
$Map::Height::Sector-1_2 = 24.1667;
$Map::Height::Sector-2_-1 = -10.8333;
$Map::Height::Sector-2_0 = 0.833333;
$Map::Height::Sector-2_1 = 12.5;
$Map::Height::Sector-2_2 = 24.1667;
$Map::Height::Sector0_-1 = -10.8333;
$Map::Height::Sector0_0 = 0.833333;
$Map::Height::Sector0_1 = 12.5;
$Map::Height::Sector0_2 = 24.1667;
$Map::Height::Sector1_-1 = -10.8333;
$Map::Height::Sector1_0 = 0.833333;
$Map::Height::Sector1_1 = 12.5;
$Map::Height::Sector1_2 = 24.1667;
$Map::Height::Sector2_-1 = -10.8333;
$Map::Height::Sector2_0 = 0.833333;
$Map::Height::Sector2_1 = 12.5;
$Map::Height::Sector2_2 = 24.1667;
$Map::X::Limit::Down = "-2";
$Map::X::Limit::Up = "2";
$Map::Z::Limit::Down = "-1";
$Map::Z::Limit::Up = "2";

This gives a syntax error at:

$Map::BLID::Sector##-##1_-1 = 27690;

when executing.

Yeah negatives aren't valid in variable names because it thinks you're trying to subtract.

You'll have to come up with some other way of saving

Well, 0,0 has to be the center of the map, not a corner. I'm just going to use two different arrays. One for positives and one that should be negative.

This function will export a file, and then read through that file using a FileObject, and when it finds something which would cause a syntax error, it encapsulates the rest of the variable in an array.

This is not a good thing to do if you are releasing the add-on, as its much slower than export, but it works fine if you just want to lazily save some variables.
Also worth noting that this allows you to save arrays and stuff when you don't know either the length or the index of each element, as it uses export.

Code: [Select]
function Safe_Export(%vars, %dir) {
export(%vars, "Config/Client/SafeExport_Temp.cs");

%readFile = new fileObject();
%readFile.openForRead("Config/Client/SafeExport_Temp.cs");
%writeFile = new fileObject();
%writeFile.openForWrite(%dir);

%legalChars = "abcdefghijklmnopqrstuvwxyz_"; //Only these chars can be the first letter of a var name
%extra = "0123456789"; //These chars can be anywhere in a var name

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

%var = getSubStr(%line, 1, (%pos = strPos(%line, " = "))-1);
%value = getSubStr(%line, %pos + 3, strLen(%line) - strLen(%var) - 5);

if(striPos(%legalChars, %firstChar = getSubStr(%var, 0, 1)) == -1) {
error("ERROR: Safe_Export: Var starts with illegal char!" SPC %firstChar);
continue;
}

for(%i=1; %i < strLen(%var); %i++) {
%char = getSubStr(%var, %i, 1);
if(striPos(%legalChars @ %extra, %char) == -1) {
%var = getSubStr(%var, 0, %i) @ "[\"" @ expandEscape(getSubStr(%var, %i, strLen(%var) - %i)) @ "\"]";
break;
}
}

if(getSubStr(%value, 0, 1) !$= "\"" || getSubStr(%value, strLen(%value)-1, 1) !$= "\"")
%value = "\"" @ %value @ "\"";

%writeFile.writeLine("$" @ %var SPC "=" SPC %value @ ";");
}

%readFile.close();
%readFile.delete();

%writeFile.close();
%writeFile.delete();
}


Edit: Also, you'll probably want to check that openForWrite(%dir); returns true, and if it doesn't, close and delete the file objects. I've already spoonfed enough though.
Edit 2: Oh, it's also a lot smarter, faster, and more efficient to simply start the array after the first character in the variable's name. This function is old, idk what I was thinking.
« Last Edit: October 06, 2015, 02:21:45 PM by boodals 2 »


$foo[":"] = 1;

Yeah, I didn't take colons into account when i first made the function, and then when I started using it I found that it was putting them into the array (which i think worked, but I wanted it to look nice). I'll just take it out of the %extra variable so that it works again.

Ok, fixed it.

you could just strReplace the "-" sign with something like "n" when you create the array
this way you can save variables just as fast as normal

you could just strReplace the "-" sign with something like "n" when you create the array
That's gross

That's gross
how so
it would work and you wouldn't need to run the exported variables through a slow torque script function all at once

Or I could make two arrays?