Author Topic: Spoonfeed me coding advice  (Read 1989 times)

I've been wanting to learn how to make scripts and other stuff for Blockland for a while, but I don't know how to use Torquescript with the game.

I tried asking SeventhSandwich for help in making a baby's first script where an admin could teleport other players into specific coordinates, but whatever he suggested didn't actually work.

So, tips & tricks would be appreciated I guess?

Code: [Select]
$allPlayers

serverCmdTeleport(%client,%player,%x,%y,%z) {
if(%client.isAdmin | $allPlayers) {

%targetClient = findClientByName(%player);
%targetClient.player.position = setWord(%targetClient.player.position,0,%x);
%targetClient.player.position = setWord(%targetClient.player.position,1,%y);
%targetClient.player.position = setWord(%targetClient.player.position,2,%z);
}
}

Here's the code Sev guided me to create.
« Last Edit: August 11, 2016, 01:52:19 AM by Rednakin »

You didn't actually set $allPlayers to anything, Torque doesn't really have pointer interactions or any of that fancy stuff. $allPlayers = True; and $allPlayers = 1; are the same thing.
| should be ||  if you want an OR statement.
%client.isAdmin will return 0 or 1/false or true.
So if you do $allPlayers = 1; and the %client isn't an admin, then if(0 OR 1)/if(false OR true) is what the game will see and the if statement will pass.

Bolded the errors, rest is useful information.

You didn't actually set $allPlayers to anything, Torque doesn't really have pointer interactions or any of that fancy stuff. $allPlayers = True; and $allPlayers = 1; are the same thing.
| should be ||  if you want an OR statement.
%client.isAdmin will return 0 or 1/false or true.
So if you do $allPlayers = 1; and the %client isn't an admin, then if(0 OR 1)/if(false OR true) is what the game will see and the if statement will pass.

Bolded the errors, rest is useful information.
Sev said that it ($allPlayers) would do as a substitute for having a server pref for who can use the command.

I changed the code to
Code: [Select]
serverCmdTeleport(%client,%player,%x,%y,%z) {
if(%client.isAdmin) {

%targetClient = findClientByName(%player);
%targetClient.player.position = setWord(%targetClient.player.position,0,%x);
%targetClient.player.position = setWord(%targetClient.player.position,1,%y);
%targetClient.player.position = setWord(%targetClient.player.position,2,%z);
}
}

But it does nothing in-game.
« Last Edit: August 10, 2016, 02:02:10 PM by Rednakin »

You didn't actually set $allPlayers to anything, Torque doesn't really have pointer interactions or any of that fancy stuff. $allPlayers = True; and $allPlayers = 1; are the same thing.
| should be ||  if you want an OR statement.
%client.isAdmin will return 0 or 1/false or true.
So if you do $allPlayers = 1; and the %client isn't an admin, then if(0 OR 1)/if(false OR true) is what the game will see and the if statement will pass.

Bolded the errors, rest is useful information.
Technically, having just one '|' would work since it'd perform a binary or operation, which would be 0 or 1.
Of course it's better to use || in this situation, but it'd still work.

Also, don't forget to prefix the command definition with function.
Code: [Select]
function serverCmdteleport(%client, %player, %x, %y, %z) {
...
}
Additionally, it'd be better to set the position using .position = %x SPC %y SPC %z;, as I'm not sure if setWord is a default function.

Slowly, but surely this is getting somewhere.

EDIT: It works.

I'd now like to make a command for slapping around players. How would I go about doing that?

Code: [Select]
function serverCmdSlap(%client,%player,%force) {
if(%client.isAdmin) {

%targetClient = findClientByName(%player);
%targetClient.player.addVelocity(%force);
}
}

something like this?
« Last Edit: August 11, 2016, 01:49:58 AM by Rednakin »

I'd also like to learn how server preferences work.

Slowly, but surely this is getting somewhere.

EDIT: It works.

I'd now like to make a command for slapping around players. How would I go about doing that?

Code: [Select]
function serverCmdSlap(%client,%player,%force) {
if(%client.isAdmin) {

%targetClient = findClientByName(%player);
%targetClient.player.addVelocity(%force);
}
}

something like this?
You're on the right track. Velocity is determined by an X, Y, and Z. You're only putting X.
Also, you should check if %targetClient's player exists before doing anything with it. Use isObject(%targetPlayer) for this. If their player doesn't exist, then it'll usually cause errors in the console.
I'd also like to learn how server preferences work.
What do you mean? Global variables? RTB prefs? Glass prefs? Prefs you make using your own commands?

What do you mean? Global variables? RTB prefs? Glass prefs? Prefs you make using your own commands?
Oh.. umm.. I thought there was a server pref system in Blockland, but RTB prefs I guess.

Code: [Select]
function serverCmdSlap(%client,%player,%force) {
if(%client.isAdmin && isObject(%targetClient.player)) {

%targetClient = findClientByName(%player);
%targetClient.player.addVelocity(%force);
}
}

Would this work as a check for if the target player exists? If it is I'm adding it to the teleport thing.

How would I go about randomizing the slap direction?
« Last Edit: August 11, 2016, 09:04:47 AM by Rednakin »

I'd also like to learn how server preferences work.
Server preferences are global variables which are saved in a file on your computer.

In the default game, these would be written like.  $pref::server::FallingDamage.
The function that calculates falling damage checks this preference every time it goes to calculate how much falling damage you should take, to see if it should apply any damage at all.  So if $pref::server::fallingdamage is 1, you take damage.  If it's 0, then you don't take damage.

When a server quits, or, I think when you start a non-dedicated server it does this to, the server does something like:
export("$Pref::Server*","config/server/prefs.cs");
At that point it grabs every global variable that starts with "$Pref::Server" and saves it to a file.
Then, when you start another server, it reads that file to get all of the preferences that were saved last time.

Server preferences are global variables which are saved in a file on your computer.

In the default game, these would be written like.  $pref::server::FallingDamage.
The function that calculates falling damage checks this preference every time it goes to calculate how much falling damage you should take, to see if it should apply any damage at all.  So if $pref::server::fallingdamage is 1, you take damage.  If it's 0, then you don't take damage.

When a server quits, or, I think when you start a non-dedicated server it does this to, the server does something like:
export("$Pref::Server*","config/server/prefs.cs");
At that point it grabs every global variable that starts with "$Pref::Server" and saves it to a file.
Then, when you start another server, it reads that file to get all of the preferences that were saved last time.
Hey thanks.

How would I state a preference for who is able to use the teleport command? I think I might need to add another if statement to my code, change what's currently in the if statement to a function and have both outcomes (needs admin -> success & doesn't need admin -> success) execute it.

Would this work as a check for if the target player exists? If it is I'm adding it to the teleport thing.

No, you have to add the check whether the player exists after searching for it.

Like this:

function serverCmdSlap(%client, %player, %force)
{
   if(%client.isAdmin)
   {
      %targetClient = findClientByName(%player);

      if(isObject(%targetClient.player))
      {
         %targetClient.player.addVelocity(%force);
      }
   }
}


Or this:

function serverCmdSlap(%client, %player, %force)
{
   if(%client.isAdmin)
   {
      %targetClient = findClientByName(%player);

      if(!isObject(%targetClient.player))
         return;
         
      %targetClient.player.addVelocity(%force);
   }
}


Hey thanks.

How would I state a preference for who is able to use the teleport command? I think I might need to add another if statement to my code, change what's currently in the if statement to a function and have both outcomes (needs admin -> success & doesn't need admin -> success) execute it.
Well.  To make it so you can toggle whether it needs admin or not you could do something like:
Code: [Select]
if($Pref::Server::RedTPAdminNotReq $= "")
{
$Pref::Server::RedTPAdminNotReq = 1;
}
serverCmdTeleport(%client,%player,%x,%y,%z)
{
if(%client.isAdmin || $Pref::Server::RedTPAdminNotReq)
{
[rest of code]
}
}

Or you could do
Code: [Select]
if($Pref::Server::RedTPAdminReq $= "")
{
$Pref::Server::RedTPAdminReq = 0;
}
serverCmdTeleport(%client,%player,%x,%y,%z)
{
if(%client.isAdmin || !($Pref::Server::RedTPAdminNotReq))
{
[rest of code]
}
}
In both cases it will set it so you do not need admin to teleport someone.


By default an unregistered variable will return a null string.
So
if($Pref::Server::RedTPAdminReq $= "")
Is just checking if this preference has been set to anything.
Once that preference has been set to... pretty much anything, it will not reset it to the default value.

If you want to make prefs properly, do this:

if($RTB::Hooks::ServerControl)
{
   //RTB is loaded
   RTB_registerPref("Admin Only", "Your Mod", "$Pref::Server::YourMod::AdminOnly", "bool", "Script_YourMod", false, false, false, "");
}
else
{
   //RTB is not loaded
   if($Pref::Server::YourMod::AdminOnly $= "")
      $Pref::Server::YourMod::AdminOnly = false;

}


To check for the pref in your script, just check the variable: $Pref::Server::YourMod::AdminOnly

To add more prefs, duplicate the blue lines each.
Pref variables must start with $Pref::Server:: to work properly without RTB.

There are some docs for the rtb pref function in here, page 11: https://media.readthedocs.org/pdf/bldocs/latest/bldocs.pdf
However, you should ignore the examples found on page 12, as they are outdated.

With a lot of prefs the end result might be something like this: https://github.com/Zeblote/Tool_NewDuplicator/blob/master/scripts/server/prefs.cs
« Last Edit: August 11, 2016, 05:08:49 PM by Zeblote »

I think the 'spoonfeed' part was a joke guys...