Hi all
After some people started to annoy me with basic questions about self and threads, i decided to write some tutorial about it in dutch.
Now i translated this tutorial into english:
Hi all
Some help for those of you who struggle with the use of self/player/other etc, and for those of you who want to know about threads and non-threaded stuff.
Lets start by opening some mod file, written by me.
This mod will count the no-aim shots a player makes, but this is of no importance for the tutorial.
Code:
main()
{
level thread waitforconnect();
}
waitforconnect()
{
for(;;)
{
level waittil("connecting",player);
player thread onconnect();
}
}
onconnect()
{
self endon("disconnect");
for(;;)
{
self waittill("spawned_player");
self thread onspawn();
}
}
onspawn()
{
self endon("disconnect");
self endon("killed_player");
self thread cleanupkilled();
self.noaimshots=0;
if(self getcurrentweaponslot()=="none")
ammo=self getweaponslotclipammo("primary");
else
ammo=self getweaponslotclipammo(self getcurrentweaponslot());
for(;;)
{
weap=self getcurrentweapon();
if(self getcurrentweaponslot()!="none")
{
ammo=self getweaponslotclipammo(self getcurrentweaponslot());
if(ammo<oldammo&&weap==oldweap)
{
if(self playerads()==0)
self.noaimshots+=oldammo-ammo;
else
self.noaimshots=0;
self iprintln(self.noaimshots);
}
}
oldweap=weap;
oldammo=ammo;
wait 0.05;
}
}
As you can see, the Main() routine has to be called when a map is started.
This is the main() routine:
Code:
main()
{
level thread waitforconnect();
}
As you can see, there is not much in there. The only thing it does is start some thread, and it tells "level" to do so. I will refer to level as being the god/server person. This person does not play, its just being the level/god/server.
Note: level thread does the same as thread, but level thread will make stuff clearer in the tutorial [/note]
What the code really does is telling level to execute some thread.
Level will do this, and will start this thread.
About threads and non-threads:
If you do not use threads, like i did here:
Code:
main()
{
level counttoten();
iprintln("done");
}
counttoten()
{
wait 10;
}
the compiler will read it as if the code was copy-pasted to there, so it does the same as:
Code:
main()
{
wait 10;
iprintln("done");
}
As you can see, the code has to wait 10 seconds before it can continue, and this is exactly the same as the code that uses level counttoten();
But, if you thread it like this:
Code:
main()
{
level thread counttoten(); //!!!!!! watch the thread here!
iprintln("done");
}
counttoten()
{
wait 10;
}
the code in the main() will continue, while the counttoten continues in the background, too. So this code will start the counttoten() thread, where another process will wait 10 seconds, but the main code will continue. As a result, the "done" will be printed to the screen directly, instead of after 10 seconds as with previous code examples.
I hope you all understand threading now..
To continue with self/player/other:
This is the thread that is called in the main() of the first example code:
Code:
waitforconnect()
{
for(;;)
{
level waittil("connecting",player);
player thread onconnect();
}
}
This code is executed by level, and as you can see, "level" has to wait until someone shouts to him: "HEY, IM CONNECTING!". Level will note down the name of the person shouting this in "player", and will instruct player to execute some thread (onconnect()). At this point, an example of what happens when "John" enters the server can be used.
Lets say John connects to the server. John will then shout to Level: Hey, I'm John and I'm connecting. Level will then instruct this John to do something. Basically, player thread onconnect() will tell John (in this case) to execute onconnect() as a THREAD. This way, level can wait for the next person to connect, instead of waiting until John has finished the onconnect() thread.
Now the onconnect() thread:
Code:
onconnect()
{
self endon("disconnect");
for(;;)
{
self waittill("spawned_player");
self thread onspawn();
}
}
In this code, executed by the example John, the story switches from third-person view to first person view. Instead of saying "John has to do this" it will say "I have to do this". As "I" is defined, being the player executing the code, the script can use the var Self. Self refers to the player/entity executing the code.
As you can see in the code, it has to stop as soon as "I" disconnects. As every player has this code running, it will stop running for only one player when that player disconnects.
Further, you can see that it has to wait until "I" realize that "I" have spawned. After that, "I" have to execute some thread.
As this thread is again called on the person running the code, Self can be used there too.
Lets take the example of John connecting to the server.
1. The server starts.
2. The server executes a thread
3. The server waits until John connects
4. The server tells John to execute some code
5. The story switches to first-person, and as a result the code can use Self referring to John
6. I have to wait until I spawn (I being John)
7. I have to execute some thread when i spawned.
8. I execute that thread, and wait until I spawn again.
I hope this is all clear to you guys, feel free to post questions, if you have any, or any comments.
I know my English is not top-notch, so, pleas, report any spelling/grammar errors. (can do by direct message)