Getting Started:
If you're reading this, you're probably pretty new to torquescript. But that's fine! I'm going to teach you how to use programming as a tool, and apply it to anything you might see happening in Blockland. The first thing that you must understand that every part of the game-- from the minigame system, to the nigh-invisible smoke emitter coming out of that sniper rifle. Right now, I'm going to introduce you to how you (the client) talk to the server.
Client-Server Interactions
When you're playing a multiplayer game, your copy of Blockland-- and everything you do with it, is constantly communicating with the server you are on. The server can invoke functions in the client, and vice-versa. When coding, you can either make a script that is serverside, or clientside. In this tutorial, we're going to be making a serverside script.
Server Commands, and Creating Something
Server commands are snippets of code, that players can activate through slash commands. If you have played the game, you've probably already seen them. Some default examples would be /light and /Self Delete. The code that runs when these commands are executed is serverside. One of the major ways people go about creating add-ons for blockland is by using these. Now, you're probably wondering-- How do I create my own?
Well, you create a new function by typing in function.
function
Then, you specify that it's a servercomand (abbreviated to servercmd).
function servercmd
Now, you have to define what arguments you want the command to take in. This is like figuring out what information you need from your environment before you start doing something. As an example, /Self Delete kills your player. So in order for that command to work, you'd need to take in the information of what player to kill. In that scenario, it'd be whatever player that activated the command. You specify what you want by saying the name of the variable that represents that bit of information. For example, if you wanted to refer to the client that's activating the function [there is a distinct difference between client and player. think of client as a soul. every time you die, you get a new body but your soul stays the same. if you want to modify a person physically, refer to their player], you would use the variable %client. Note that there are two different types of variables. There are local, and global ones. Local ones are represented with a %, while globals are $. You never need to refer to a global variable in the arguments of a servercommand, since it is always present, and never goes away. Local variables (%) get erased after a function ends, and their value can differ from servercmd to servercmd.
Lets say we wanted to code a command where it makes the player grow in size, and then alert the server about it. We'd need to take in information about the client (soul) executing this. We don't need to request %player since it is attached to the client, and we can refer to it from %client. I'll explain further later.
Lets say we'll call this servercommand /growMe. This is what players will type in when they want to trigger the command. We type in the local variables we want to refer to in parentheses, like so:
function servercmdGrowMe(%client)
Now, all we need to do is write the code that gets activated whenever someone triggers this command. This code is surrounded in curly brackets.
function servercmdGrowMe(%client)
{
You put your code here.
}
Okay, now lets start writing the code. Like previously said, we want to make it so it grows the player, and also alerts the server about it. The way to grow player's size is by using setScale on the player's body. So in order to effect the player, we have to go through the client, and refer to the playerObject attached to him. So, in order to access a variable that's saved to something, you do %something.savedvariable. So, in this case, the player is data that's nested inside of %client. So you'd use %client.player .
function servercmdGrowMe(%client)
{
%client.player.setPlayerScale
}
But yet, we are not finished! We have to specify what to set the player's scale as. For players, the engine-set limit is where all the axises are scaled to 5. So, since it's formatted x y z, it would become 5 5 5. I want to remind you that setPlayerScale is a function itself, and it takes in data just like the one we are making. The data it wants is what to scale the player to, which as we've found is 5 5 5. The way we input this data into the function is by using parenthesis again. Also, in those parenthesis we have to have quotation marks around 5 5 5 as it is a string, multiple words, and not a single word. Adding on to this, we have to end the command with a semicolon. It's a syntax thing in torque, and other guides would go more in depth on it. So, it would end up looking like this:
function servercmdGrowMe(%client)
{
%client.player.setPlayerScale("5 5 5");
}
At this stage, the command would work perfectly! Yet, like we said we also want to alert everyone else ever about this growth. So, as you've probably seen before, we'll use the talk function. This makes it seem that the console is talking, like this:
Console: Hello!
The variables that this function takes in is simply what the message is going to say. But so now, lets make OUR MINIONS DANCE it talk.
function servercmdGrowMe(%client)
{
%client.player.setPlayerScale("5 5 5");
talk("A player has made himself grow!");
}
Of course, this in no way is dynamic. I mean, no one would ever know who the player is! Is it a mystery? Forever? Because of you?!?
Absolutely not. I won't let this injustice stand. We must speak the name of the perpetrator, and speak it loud we must.
In order to do this, we need to access the name of the client. Lucky for us, it's neatly stored in a variable-- stored in the client. In order to access it, we simply do %client.name. But, this is a bit trickier. We want to access it, inside of the string being sent to the talk function, so it can be displayed serverwide. We have to end the quotation marks in the string, signaling the engine to recognize that civilian-speak has ended. In this between-space, we can refer to variables all day long. So, let's do this:
function servercmdGrowMe(%client)
{
%client.player.setPlayerScale("5 5 5");
talk("The player" %client.name "made himself grow!");
}
Yet, this still wouldn't work. The spaces in the in-between area won't be recognized, and it'll display this:
Console: The playerPiexesmade himself grow!
We have to use these special abbreviations, SPC and @. As you probably guessed, SPC adds a space to things. The @ symbol doesn't add a space, and just superglues the variable and the word before it into one thing. So, we'd end up doing this:
function servercmdGrowMe(%client)
{
%client.player.setPlayerScale("5 5 5");
talk("The player" SPC %client.name SPC "made himself grow!");
}
Now, this function may work as described. But there is still one more thing! This servercmd can be executed by someone who hasn't spawned, making the script break since %client.player won't refer to anything in particular. So, we have to add what is called an if-statement. If-statements contain snippets of code, but they only run the code if the values that they describe are true. Since we want to check if the client has a body, we'd do if(isObject(%client.player)). The code inside this if-statement would be stored in brackets, much like the code for the servercommand is. This is the result:
function servercmdGrowMe(%client)
{
if(isObject(%client.player))
{
%client.player.setPlayerScale("5 5 5");
talk("The player" SPC %client.name SPC "made himself grow!");
}
}
I hope this tutorial helped any of you aspiring learners! Trust me, we're all still learning new things.
I'll try to update this a few more times. Sorry if I forgeted anything up, and please leave criticism below.