Two arguments in a function?

Author Topic: Two arguments in a function?  (Read 3633 times)

What i'm attempting to do is call a function to return some variables, (to load saves and teleport players to them) which are:
Code: [Select]
$tele[1] = "152.596 101.372 1.01 0 0 -1.00001 0.102687"; //teleport location for apartment save
$tele[2] = "-53.6 -68.24 21.5 0 0 1 1.6"; // teleport location for arch of constantine save

$map[0] = "Apartment.bls"; // apartment save file
$map[1] = "Arch of Constantine.bls"; //arch save file

the random map and tp function
Code: [Select]
function random(%map, %randomTp) {
    %random = getRandom(1,2); // random number between 1 and 2

    // random map
    switch (%map) {
    case 1:
        return ($map0);
    case 2:
        return ($map1);
    default:

    }
    //set teleport to be same as map
    %randomTp = %random;
    switch (%randomTp) {
    case 1:
        return ($tele[0]);
    case 2:
        return ($tele[1]);
    default:

    }
}

the loading bricks function

Code: [Select]
function loadBrix() {
    schedule(0, 0, serverDirectSaveFileLoad, "saves/" @ random(%map), 3, "", 2, 1);
    findclientbyname("Arix").player.setTransform(random(%randomTp));
}
(forgive my formatting)
is it possible to use %map and %randomtp in that function that way?

You're calling the random function with undefined variables, no.

You haven't really explained what you're doing, or what its for, very well.

So you have a global array of maps, and transformations for each of the maps, and you're trying to get both of these variables out of a single function?
You can concatenate the two variables and return that, then unconcatenate it to get the variables out.
return $map[0] NL $tele[0];
And then %mapData = random(); %map = getRecord(%mapData, 0); %transform = getRecord(%mapData, 1);

Also, you can seriously optimize this by using $map[%random] as opposed to checking each number, of course this wouldn't check if it exists.

And return doesn't need brackets, its not a function.

So you have a global array of maps, and transformations for each of the maps, and you're trying to get both of these variables out of a single function? - This is correct
You can concatenate the two variables and return that, then unconcatenate it to get the variables out.
return $map[0] NL $tele[0];
And then %mapData = random(); %map = getRecord(%mapData, 0); %transform = getRecord(%mapData, 1);

Also, you can seriously optimize this by using $map[%random] as opposed to checking each number, of course this wouldn't check if it exists.

And return doesn't need brackets, its not a function.
Thank you so much, I knew I could've done it smaller but I didn't know you could do it that way.

Edit: How do I de-catenate these? I know that NL puts the second string onto a new line. Would I need to use getField like this?
Code: [Select]
echo(getField(NL %secondstring));another edit: i am an idiot, getRecord( sourceString , index ); (from the torquescript wiki) does this
« Last Edit: October 22, 2014, 09:24:30 AM by alex dude »

Lol, I had it in my post

%map = getRecord(%mapData, 0); %transform = getRecord(%mapData, 1);


One note for anyone who might come apon this, make sure you understand what is stored in the variables. Using NL (new line) is fairly good because its hard to get a new line character into a string via user input (unless you purposefully plan for that. But if you used SPC (spaces) it could create unexpected bugs depending on what is in the variables. For example a transformation as seen here "30.2 45.022 89.57 0 0 1 0.2432" has many words in it already, so using getWord would be a bad choice. If you know how many words are in each variable, you can plan for it and use getWords, but only if the amount of words never changes.
« Last Edit: October 22, 2014, 10:31:44 AM by boodals 2 »