Moving Object // GetTok problem

Started by MacTavish, Nov 20, 2014, 05:45 AM

Previous topic - Next topic

MacTavish

Q. 1..   I have a question i added the Electric gate ID 303 at the mansion  gate like flockshots script but its not moving and the way is got blocked. Is there any way to open that gate


Q. 2..  I am using VBS SYSTEM i tried to port 0.3 cmds to 0.4 some cmds works but gives me GetTok error in VBS/utils.nut. it show me invalid 'string' expect 'instance' bla bla


GetTok
function utils_gettok( string, separator, n, ... )
{
local m = vargv.len() > 0 ? vargv[0] : n,
  tokenized = split(string, separator),
  text = "";

if (n > tokenized.len() || n < 1) return null;
for (; n <= m; n++)
{
text += text == "" ? tokenized[n-1] : separator + tokenized[n-1];
}
return text;
}



Grand Hunting Project
Join #SLC, #KAKAN, #Doom, #GHP @LUnet

Retired VC:MP Player/Scripter :P

Honey

Ans 1 : You need to move it using Object.MoveBy( Vector posOffset, int time ) .

For e.g.

l1 <-CreateObject( 1796, 0, Vector( -559.496, 779.4, 23.25 ), 255 );
l1.ShowForAll();
 l1.MoveBy(Vector(0,2,0), 2800 );

Ans 2 : "Bla Bla" Should contain the error lines as well.Post the error and specify the lines as well

.

Try using this version of the GetTok() and NumTok() functions. They do various checks on the passed arguments before using them so they might be safer.

function GetTok(str, sep, pos, len = null)
{
// First round is parameter validation
if (typeof str != "string" || str.len() < 1) return ""; // Not a valid string
else if (typeof sep != "string" || sep.len() < 1) return ""; // Not a valid separator
// Split the string into tokens
local tok = split(str, sep);
// Second round is result validation
if (typeof tok != "array") return str; // Maybe an internal error
else if (tok.len() == 1 && tok[0] == str) return tok[0]; // No tokens we're found
// Third round is position parameter adjustment
if (typeof pos != "integer")
{
if (typeof pos == "float" || typeof pos == "string") pos = pos.tointeger();
else return ""; // Invalid position
}
// Make sure the position is in bounds
if (pos >= tok.len()) return ""; // The position is out of bounds
else if (pos < 0) pos = 0;
// Fourth round is length parameter adjustment
if (len == null)
len = (tok.len() - pos);
else if (typeof len != "integer")
{
if (typeof len == "float" || typeof len == "string") len = len.tointeger();
else return ""; // Invalid length
}
// Make sure the length is in bounds
if ((pos+len) > tok.len()) len = (tok.len() - pos);
else if (len <= 0) return ""; // The length is out of bounds or invalid
// Retrieve the required tokens
tok = tok.slice(pos, pos+len);
// Make sure we don't go into a loop for nothing
if (tok.len() > 1)
{
// Create a temporary variable to hold the generated string
local res = tok[0];
// Remove the element that we just used
tok.remove(0);
// Implode those tokens using the specified separator
foreach (tk in tok) res += sep + tk;
// Return the result
return res;
}
// Just return the requested token
else if (tok.len() == 1) return tok[0];
// The array slicing failed somehow
else return "";
}

function NumTok(str, sep)
{
// First round is parameter validation
if (typeof str != "string" || str.len() < 1) return 0; // Not a valid string
else if (typeof sep != "string" || sep.len() < 1) return 0; // Token must be a string
// Split the string into tokens
local tok = split(str, sep);
// Second round is result validation
if (typeof tok != "array") return 0; // Maybe an internal error
else if (tok.len() == 1 && tok[0] == str) return 0; // No tokens we're found
else return tok.len(); // Return the number of found tokens
}

Be aware that they work a little different and the rest of the code might have to be adjusted to work with them:
function GetTok(str, sep, pos, len = null);
pos: Is the position where you want your string slicing to start. If you set it to 0 it will start from where the first separator is encountered. If it's set to 1 it will start from where the second separator is encountered. If it's set to 2 it will start from where the third separator is encountered. And so on...

len: Is the position where you want your string slicing to end. If you set it to 0 it return an empty string because that means to return no token. If it's set to 1 it'll end where the first separator is encountered after the start. If it's set to 2 it'll end where the second separator is encountered after the start. And so on...
Setting the len to null or simply omitting it will return everything from the start position until the last separator is encountered.

Here are some tests and the expected results:
local text = "param1 arg2 param3 val4 blah blah blah";

GetTok(text, " ", 0);
// Returns: "param1 arg2 param3 val4 blah blah blah"

GetTok(text, " ", 1);
// Returns: "arg2 param3 val4 blah blah blah"

GetTok(text, " ", 2);
// Returns: "param3 val4 blah blah blah"

GetTok(text, " ", 0, 2);
// Returns: "param1 arg2"

GetTok(text, " ", 2, 4);
// Returns: "param3 val4 blah"

GetTok(text, " ", 67);
// Returns: "" (Out of bounds)

GetTok(text, " ", -242, 4);
// Returns: "param1 arg2 param3 val4" (Got converted to 0)

GetTok(text, " ", 2, -23);
// Returns: "" (Out of bounds)

GetTok(text, "#", 2, -23);
// Returns: "param1 arg2 param3 val4 blah blah blah" (Separator not found)

GetTok(text, " ", "3", 3.2454);
// Returns: "val4 blah blah" (Automatically converted to integer)

GetTok(text, 261, "3", 3.2454);
// Returns: "" (Not a valid separator)

GetTok(null, " ", "3", 3.2454);
// Returns: "" (Not a valid string)
.

MacTavish

#3
THANKS

@Honey: The gate still not moving

Grand Hunting Project
Join #SLC, #KAKAN, #Doom, #GHP @LUnet

Retired VC:MP Player/Scripter :P