Login x
User Name:
Password:
Social Links Facebook Twitter YouTube Steam RSS News Feeds
Watch MODSonair

Members Online

»
0 Active | 47 Guests
Online:

LATEST FORUM THREADS

»
Mohaa maps on Cod2?
CoD2 MP Mapping

Forums

»

Welcome to the MODSonline.com forums. Looking for Frequently Asked Questions? Check out our FAQs section or search it out using the SEARCH link below. If you are new here, you may want to check out our rules and this great user's guide to the forums and the website.
For more mapping and modding information, see our Wiki: MODSonWiki.com

Jump To:
Forum: All Forums : Call of Duty 2
Category: CoD2 Scripting
Scripting and coding with Call of Duty 2.
Moderators: foyleman, Foxhound, Mystic, StrYdeR, batistablr, Welshy, DrBiggzz, supersword, playername
Latest Posts
Subscribed Posts
Search
Restricted Access Topic is Locked subscribe
Author Topic: Tutorial about some scripting stuff
IzNoGoD
General Member
Since: Nov 29, 2008
Posts: 694
Last: Nov 10, 2012
[view latest posts]
Level 6
Category: CoD2 Scripting
Posted: Wednesday, Dec. 29, 2010 05:15 am
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)


Share |
r00t_
General Member
Since: Dec 14, 2010
Posts: 51
Last: May 16, 2011
[view latest posts]
Level 3
Category: CoD2 Scripting
Posted: Wednesday, Jan. 12, 2011 12:12 pm
Great Tutorial mate!
I knew most of it. But it sure cleared a lot up!
Share |
playername
Preferred Member
Since: Aug 24, 2006
Posts: 821
Last: Apr 15, 2011
[view latest posts]
Level 7
Forum Moderator
Im a fan of MODSonair
Category: CoD2 Scripting
Posted: Monday, Feb. 14, 2011 05:26 pm
Great post IzNoGoD! I liked the "switches from 'John has to do this' to 'I have to do this'" because, more or less, that's what self is. However, I would add something in saying it does not only have to be the player, it can be any game object/entity (a trigger for example). Also, maybe take a look at explaining how Notify, Endon, and Waittill work since threading and those statements are often used together.

Keep up the great work!!!!
nullFew tips for coding.
1. Keep the script as short as possible.
2. Don't comment every line. Only comment portions where they may be needed to point something out.
3. Don't over complicate the script, keep it organized and easy to read.

These help you find simple errors and makes it easy to make changes.
Share |
Restricted Access Topic is Locked subscribe
MODSonline.com Forums : Call of Duty 2 : CoD2 Scripting

Latest Syndicated News

»
Codutility.com up and runn...
Nice, and there still using the logo and template for the screenshots, which...
Codutility.com up and runn...
dundy writes...Quote:Call of Duty modding and mapping is barly alive only a ...
Codutility.com up and runn...
Mystic writes...Quote:It seems to me the like the site is completely dead? ...
Codutility.com up and runn...
It seems to me the like the site is completely dead?

Partners & Friends

»