Author Topic: Beginner scripting tutorial  (Read 3729 times)

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.



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

nothing more, nothing less.


Server.cs is a little different. It contains the script for your modification.
Code: [Select]
//CodegoesinthisYou can also put the main script in a different .cs file and keep server.cs to
Code: [Select]
exec("./yourscript.cs");


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: [Select]
function test(%arg1, %arg2)
{
echo("This is a test");
}

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: [Select]
function test(%arg1, %arg2)
{
echo("Argument one is" @ %arg1 @ ", while argument 2 is" @ %arg2 ".");
}

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: [Select]
function serverCmdtest(%client, %arg1, %arg2)
{
echo("Argument 1 is" @ %arg1 @ ", while argument 2 is" @ %arg2 @ ".");
}
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: [Select]
$variable = "foo";

function foobar()
{
%var2 = "bar";
echo("derp" @ $variable @ %var2 " :D");
}

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: [Select]
$Global::Variable = "Global :D";
$Global::Variable::Number = 7;

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: [Select]
function local()
{
%localVariable = "Local :|";
%localVariableNumber = 8;
}

They work together quite well. For example, look at this simple math function, which will add $Global::Variable::number to %localVariableNumber...

Code: [Select]
function varMath()
{
%localVariableNumber = 8;
        %ans = %localVariableNumber + $global::variable::number;
echo("Ans =" @ %ans @ ".");
}
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: [Select]
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 "!");
}
}

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: [Select]
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 @ ".");
}
}
}

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: [Select]
function serverCmdHelloWorld(%client)
{
messageClient(%client, '', "Hello, world!");
announce("Hello, world!");
}

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: [Select]
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.");
}

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
« Last Edit: October 22, 2011, 03:21:14 PM by mp7964 »

Cool. I will probobly use it.

Hot guide!

Anything on schedules? For example:

Code: [Select]
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);
}

I'm going to bed, I'll probably add that on the morrow.


There are a few syntax error but other then that it's a great tutorial for beginners.

I'd wait until you understand more to write tutorials.

Some syntax errors, some messy formatting, also using echo in a server-side script does not print it in the client's console like you say.

I like this, but it needs to be drastically expanded. You should use an example of "let's make a script that does ___" And show how variables and statements are modified to produce the desired effect.

I never add namecheck.txt.


I'm pretty sure the game adds it automatically.

I don't know for sure though.


I'd wait until you understand more to write tutorials.

Some syntax errors, some messy formatting, also using echo in a server-side script does not print it in the client's console like you say.

I didn't say that it would print into the client's console. Someone who is learning with this would probably be in a single-player game, anyway. As for syntax errors and formatting, I was sleepy while I was writing this guide.

:D
I'm reading this.

:D
« Last Edit: October 21, 2011, 05:23:38 PM by mp7964 »

I didn't say that it would print into the client's console.
Quote
...Then, it prints in the client's console "Answer is [answer]."...


Someone who is learning with this would probably be in a single-player game, anyway
Very true, but it's still a good idea to teach them correctly from the start


I was sleepy while I was writing this guide.
Then don't write tutorials when you know you're not in a good state to write them. And knows a good time to fix it.
Not trying to be rude, but if you want to teach people you need to be good at it.

I never add namecheck.txt.


I'm pretty sure the game adds it automatically.

I don't know for sure though.
RTB will add it automatically.


k

had a feeling it was RTB
Blockland has support for it though, and won't execute it if you have one and the names aren't the same.

Blockland has support for it though, and won't execute it if you have one and the names aren't the same.
Now that I think of it, it's a good idea to have a namecheck.txt.