User Tools

Site Tools


scripting:other_language_features

Includes

Rather than having all of your script code in the one 'ServerScript.mit' file, it is expected that you will split your script across multiple files, with each file usually relating to one area of your game-world. (e.g. You may have a file to handle all the usable items on your world, or one for handling a particular cutscene or scripted building).

To link files into your script, use the format..

#include "<FILENAME>"

e.g. ServerScript.mit

#include "CrowTournament.mit"

CrowTournament.mit

Event( "Custom", "StartCrowTournament" )
{
    *say Crow Tournament commencing
    ...
}

Keeping related code separated like this can make it easier to share script across worlds - e.g. You could add your crow tournament feature to another world by copying the CrowTournament.mit file across and including it from the ServerScript.

Persistent Key Values

You can create persistent/saved variable & data that are attached to a player, a building or the world/server. Keys are referenced by their name, can be created at any point and can store integers or strings.

Event( "AccessBuilding", "" )
{
   $gPlayerKey[AccessCount] += 1
   $gBuildingKey[LastPlayerAccessed] = $gPlayerID
   $gServerKey[LastBuildingAccessed] = $gBuildingNum
}

Barring sudden outages, key values will be saved and restored whenever the server is restarted.

Timers

When you want to trigger an event at a certain time, you can add a timer using the script system function sysSetTimer. You specify the time delay and the name of an event to trigger.

Sleep command

Calling the Sleep function pauses the Event for a specified period of time (in 1/10th second steps). You can, for instance, write a small script that triggers a particle effect on a player, waits for a second, then triggers another particle effect. Example

Event( "UseItem", "Potion" )
{
    $loop = 0
    while ( $loop < 10 )
    {
        // Trigger effect
        *playereffect 6 $gPlayerID $gPlayerID 1
        // Wait one second
        Sleep(10)
        $loop += 1
    }
}

Functions

Functions are defined in your script as shown in this example:

Function	IsItemASword( $itemNum )
{
	if ( $itemNum = 112 )		// Sword
	{
		return( 1 )
	}
	else if ( $itemNum = 113 )	// Sword 2	
        {
		return( 1 )
	}
	return( 0 )
}

This example function would be called from another part of your script like this:

  $isSword = IsItemASword( $gTaskItem1 )

Functions can have any number of parameters, and always return a value.

The 'Sleep' command cannot be used from within a function.

Custom Events

You can add your own custom events, and then trigger them from a command. (And hence, you can trigger custom events from within other events). When you trigger a custom event in your script, the new event is executed immediately and the main event waits until it completes or when the new event sleeps.

A custom event is defined as shown:

   Event( "Custom", "YourCustomEventName" )
   {
      *say My Custom Event has been triggered for %PLAYER%
   }

This custom event can be triggered using the command *event or *eventallonline (*eao for short). e.g.

   *event Bob YourCustomEventName

would display My Custom Event has been triggered for Bob.

Be very careful with events calling other events, particularly when using *eventallonline or Sleeps. Many commands (*addbackgroundmodel for instance) replicate to everyone present, so mistakenly having an event triggered for all players can produce confusing and nasty side-effects. (e.g. For *addbackgroundmodel, duplicate models would be added to the map multiple times and the server could waste an awful lot of your bandwidth telling everyone about again and again..)
scripting/other_language_features.txt · Last modified: 2023/05/15 02:31 by mit

Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Public Domain Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki