Scripting Introduction: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
Resources are a key part of MTA. A resource is essentially a folder or zip file that contains a collection of files, plus a meta file that describes to the server how the resource should be loaded and what files it does contain. A resource can be seen as being partly equivalent to a program running in an operating system - it can be started and stopped, and multiple resources can run at once.
Resources are a key part of MTA. A resource is essentially a folder or zip file that contains a collection of files, plus a meta file that describes to the server how the resource should be loaded and what files it does contain. A resource can be seen as being partly equivalent to a program running in an operating system - it can be started and stopped, and multiple resources can run at once.


What a resource does and how it is written defines if it is a gamemode, a map or anything else.
Everything that has to do with scripting happens in resources, what a resource does defines if it is a gamemode, a map or anything else.


==Creating a simple script==
==Creating a simple script==
Firstly, we want to write something that adds a 'createvehicle' command players can use to spawn a vehicle beside their position.
===Preparetions===
As decribed above, a resource is a folder or zip file, so first you should create a folder. The folder name is the name of the resource, that is used to start or stop it or reference in scripts. In our example, we shall call it ''commands''.
As decribed above, a resource is a folder or zip file, so first you should create a folder. The folder name is the name of the resource, that is used to start or stop it or reference in scripts. In our example, we shall call it ''commands''.


Line 14: Line 16:
</syntaxhighlight>
</syntaxhighlight>
Now we have to create the ''script.lua'' file we defined above in the same directory as the meta.xml. Now we have the both files:
Now we have to create the ''script.lua'' file we defined above in the same directory as the meta.xml. Now we have the both files:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="xml">
/commands/meta.xml and
/commands/meta.xml
/commands/script.lua
/commands/script.lua
</syntaxhighlight>
</syntaxhighlight>
 
===Writing the script===
Let's start with the contents of the ''script.lua'' file. Let's say we want to provide a command to create a vehicle beside your current position in the game. Firstly we need to create a function we want to call and a command handler that creates the command the player will enter.
Let's start with the contents of the ''script.lua'' file. As mentioned above, we want to provide a command to create a vehicle beside your current position in the game. Firstly we need to create a function we want to call and a command handler that creates the command the player will be able to enter in the console.


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function createVehicleForPlayer(player, command, vehicle)
-- create the function the command handler calls, with the arguments: thePlayer, command, vehicle
-- create a vehicle and stuff
function createVehicleForPlayer(thePlayer, command, vehicle)
  -- create a vehicle and stuff
end
end


Line 29: Line 32:
addCommandHandler("createvehicle", createVehicleForPlayer)
addCommandHandler("createvehicle", createVehicleForPlayer)
</syntaxhighlight>
</syntaxhighlight>
'''Please note that function names are clickable in code examples on the wiki, which will lead you to the functions' documentation.'''
The first argument of ''addCommandHandler'' is the name of the command the player will be able to enter, the second argument is the function this will call, in this case ''createVehicleForPlayer''.
If you have already experience in scripting, you will know that you call a function like this:
<syntaxhighlight lang="lua">
functionName(argument1, argument2, argument3, ..)
</syntaxhighlight>
We called the ''addCommandHandler'' function this way already and since ''createVehicleForPlayer'' is a function too, it can be called that way as well. But we are using a command handler for that, which calls it in a similiar manner, internally.
For example: Someone types "createvehicle 468" ingame in the console to spawn a Sanchez, the command handler calls the createVehicleForPlayer function, as '''if''' we would have this line of code in the script:
<syntaxhighlight lang="lua">
createVehiceForPlayer(thePlayer,"createvehicle","468") -- thePlayer is the player element of the player who entered the command
</syntaxhighlight>
As we can see, it first provides the player who called the command as argument, then the command he entered and then whatever text he had after that, in this case "468" as vehicle id for the Sanchez.

Revision as of 22:02, 2 August 2007

Resources are a key part of MTA. A resource is essentially a folder or zip file that contains a collection of files, plus a meta file that describes to the server how the resource should be loaded and what files it does contain. A resource can be seen as being partly equivalent to a program running in an operating system - it can be started and stopped, and multiple resources can run at once.

Everything that has to do with scripting happens in resources, what a resource does defines if it is a gamemode, a map or anything else.

Creating a simple script

Firstly, we want to write something that adds a 'createvehicle' command players can use to spawn a vehicle beside their position.

Preparetions

As decribed above, a resource is a folder or zip file, so first you should create a folder. The folder name is the name of the resource, that is used to start or stop it or reference in scripts. In our example, we shall call it commands.

What every resource has and needs is the meta.xml file. In our case, we want to create a script that provides some simple commands to the user, thus we need to tell the server to load a script file, we name script.lua in our case.

<meta>
  <info author="YourName" description="A few simple commands" />
  <script src="script.lua" />
 </meta>

Now we have to create the script.lua file we defined above in the same directory as the meta.xml. Now we have the both files:

/commands/meta.xml
/commands/script.lua

Writing the script

Let's start with the contents of the script.lua file. As mentioned above, we want to provide a command to create a vehicle beside your current position in the game. Firstly we need to create a function we want to call and a command handler that creates the command the player will be able to enter in the console.

-- create the function the command handler calls, with the arguments: thePlayer, command, vehicle
function createVehicleForPlayer(thePlayer, command, vehicle)
   -- create a vehicle and stuff
end

-- create a command handler
addCommandHandler("createvehicle", createVehicleForPlayer)

Please note that function names are clickable in code examples on the wiki, which will lead you to the functions' documentation.

The first argument of addCommandHandler is the name of the command the player will be able to enter, the second argument is the function this will call, in this case createVehicleForPlayer.

If you have already experience in scripting, you will know that you call a function like this:

functionName(argument1, argument2, argument3, ..)

We called the addCommandHandler function this way already and since createVehicleForPlayer is a function too, it can be called that way as well. But we are using a command handler for that, which calls it in a similiar manner, internally.

For example: Someone types "createvehicle 468" ingame in the console to spawn a Sanchez, the command handler calls the createVehicleForPlayer function, as if we would have this line of code in the script:

createVehiceForPlayer(thePlayer,"createvehicle","468") -- thePlayer is the player element of the player who entered the command

As we can see, it first provides the player who called the command as argument, then the command he entered and then whatever text he had after that, in this case "468" as vehicle id for the Sanchez.