Blockland Forums > Modification Help
Beginner scripting tutorial
mp7964:
MP6767's scripting tutorial
If you've clicked on this topic, chances are you want to know how to make your own modifications. This will teach you the basics. First off, we'll look at a script add-on's folder structure.
First off, let me say that this is for a server script, not a client one.
For description.txt, you need these things
--- Quote ---Title: Test
Author: [Yourname]
A brief description. Try to keep it to one line.
--- End quote ---
Now for namecheck.txt, which doesn't REALLY need to be included as I'm sure the game makes it if it doesn't exist anyways, you need this
--- Quote ---script_scriptName
--- End quote ---
nothing more, nothing less.
Server.cs is a little different. It contains the script for your modification.
--- Code: ---//Codegoesinthis
--- End code ---
You can also put the main script in a different .cs file and keep server.cs to
--- Code: ---exec("./yourscript.cs");
--- End code ---
Part 2 - The beef
First off, lets look at functions. They are probably the most common thing you'll find, as they are used for everything.
--- Code: ---function test(%arg1, %arg2)
{
echo("This is a test");
}
--- End code ---
This script prints a line into the console saying "This is a test". Now lets look at the arguments, %arg1 and %arg2.
Essentially, arguments are something you can put after the function when calling it from the console, so if I typed "test(herp derp);" in the console, it would print the line, but that's because we haven't done anything with the arguments yet. Lets try this.
--- Code: ---function test(%arg1, %arg2)
{
echo("Argument one is" @ %arg1 @ ", while argument 2 is" @ %arg2 ".");
}
--- End code ---
So now, if I put test(herp derp"); in the console, it would say Argument 1 is herp, while argument 2 is derp. Cool, huh? Arguments are essentially variables, which we will get into in the next section.
Lets try a servercmd. You know servercmds like /kick and /wand, right? Well you can make your own like this...
NOTE - The servercmd's first argument must always be %client. The game thinks the first argument is %client, anyway so if you put %derp for the first argument, it would work the same as %client. Just keep it as %client, though.
--- Code: ---function serverCmdtest(%client, %arg1, %arg2)
{
echo("Argument 1 is" @ %arg1 @ ", while argument 2 is" @ %arg2 @ ".");
}
--- End code ---
Also, if you haven't figured it out yet, the @ symbol sticks pieces of text together. so "Herp" @ "derp" would print as "Herp derp".
It does essentially the same thing as test(herp derp);, but through a slash command. Very cool.
Variables
You've already technically seen variables, because arguments are basically local variables.
First off, the two types of variables
%variable - Local variable. Can only be defined within a function. Only exists within the function. It usually can't be used anywhere but inside of the function.
$variable - Global variable. It can be defined both in and out of a function, and used within one. Example
--- Code: ---$variable = "foo";
function foobar()
{
%var2 = "bar";
echo("derp" @ $variable @ %var2 " :D");
}
--- End code ---
That saucy little number prints "derp foobar :D" into the console.
Now lets take a looksy-doo at defining variables.
Defining a global variable is as easy as pie.
--- Code: ---$Global::Variable = "Global :D";
$Global::Variable::Number = 7;
--- End code ---
Text must be contained in quotation marks, while numbers can just be out there.
Global variables are about as easy, but require a function.
--- Code: ---function local()
{
%localVariable = "Local :|";
%localVariableNumber = 8;
}
--- End code ---
They work together quite well. For example, look at this simple math function, which will add $Global::Variable::number to %localVariableNumber...
--- Code: ---function varMath()
{
%localVariableNumber = 8;
%ans = %localVariableNumber + $global::variable::number;
echo("Ans =" @ %ans @ ".");
}
--- End code ---
Result - 15 printed into the console.
Lets do a function serverCmd with variables. But this time, lets define them with the command.
function serverCmdAdd(%client, %num1, %num2)
{
%answer = %num1 + %num2;
echo("Answer is" @ %answer ".");
}
Lets break this down.
First, the client types /add [number] [number]
Next, the function sets a local variable, called answer, to %num1 plus %num2.
Then, it prints in the client's console "Answer is [answer]."
Simple stuff, so far. Next, we'll be taking a look at things that are a bit more complicated.
The magical if and else statements
Okay, so far we've looked at basics, functions and variables, but what if we want to check if a variable is the same as another one? Here's where the if statement comes in.
--- Code: ---function serverCmdAdd(%client, %num1, %num2)
{
if(%num1 > 200)
{
echo("I'm not smart enough to add a number bigger than 200.");
}
else
{
%ans = %num1 + %num2;
echo("I KNOW THAT, IT'S" @ %ans "!");
}
}
--- End code ---
Lets just dive right into this one :)
Okay, the client types /add and then their numbers
then, the function checks if the first number is bigger than 200,
if it is, then it tells you that it's not smart enough.
if not, or ELSE, it sets the answer to a variable, and prints it into the console. Same as last time :D
Now, lets look at a more complicated function.
--- Code: ---function serverCmdDivide(%client, %num1, %num2)
{
if(%num1 == 0)
{
echo("Division by 0 is impossible, man.");
}
else
{
if(%num2 == 0)
{
echo("Division by 0 is impossible, man.");
}
else
{
%ans = %num1 / %num2;
echo("Hey broham. Answer is" @ %ans @ ".");
}
}
}
--- End code ---
Woah, now lets take this one slow.
First, it sees if the first number is 0, if it is, it won't do the function.
Then, if not it goes merrily along and sees if the second number is 0.
If it is, it won't do the function.
If both those are not 0, then it will set the answer to a variable and echos it.
Same as last time.
More user friendly stuff.
Up until now, we've been using echo to print text into the console. The problem with that is, it makes it look like the function doesn't work because the player doesn't immediately see the number. Lets do some messaging ;)
--- Code: ---function serverCmdHelloWorld(%client)
{
messageClient(%client, '', "Hello, world!");
announce("Hello, world!");
}
--- End code ---
For the record, announce(); is the same as messageAll, but I just like Announce, so i'll use it for now.
Anyway, this tells the client, in chat, "Hello, world!", then announces it to the server.
Lets do another, hmm?
--- Code: ---function serverCmdFurpDurp(%client, %var1)
{
if(%var1 > 64)
{
messageClient(%client, '', "Var1 is bigger than 64, i'm gonna go and do something else.");
%variable = 1234;
}
else
{
messageClient(%client, '', "Var1 isn't bigger than 64, i'm gonna sit here and listen like a good script");
listen();
}
}
function listen(0;
{
echo("I'm listening.");
}
--- End code ---
This one checks if %var1 is bigger than 64, if it is, it won't listen and will set a random var to 1234.
If not, it tells the client it's going to listen, then calls the function listen.
The function listen echos "I'm listening." into the console.
To be continued
Crown2:
Cool. I will probobly use it.
Eeposs:
Hot guide!
Anything on schedules? For example:
--- Code: ---function Stick(%a)
{
cancel($stick);$sticks=%a;stick();
}
function stick()
{
cancel($stick);if($sticks<1){commandtoserver('messagesent',"no moar stikz D;");return;}else commandtoserver('messagesent',"take 1 stick down now theres,"
SPC $sticks--);$stick=schedule(2800,0,stick);
}
--- End code ---
mp7964:
I'm going to bed, I'll probably add that on the morrow.
jes00:
There are a few syntax error but other then that it's a great tutorial for beginners.