Ok, so I promised a few people how to create moving objects in maps for MTA DM, race gamemode. This tutorial might seem a bit long, but that is because everything is explained very detailed an not because of the toughness of it.
First of all, if you don't know what I'm talking about: I've made a few maps and they're like by a lot of people, because of the exclusivity. I add scripts to the map, which contain things like gun-pickups and, in this case, movable objects.
The movie about my last and, in my opinion, best map I made, you can see what I mean with "movable objects". YouTube:
This tutorial is an example of the scripting that causes the first three blocks to move up and down in the video at 0.21.
First, you must have a MTA-DM map. Go to the folder of that map (mostly "C:\Program Files\MTA San Andreas\server\mods\deathmatch\resources", you'll find a folder or a .zip-file here named with the name of your map). If it is a .zip-file, you must unpack it first. After that, delete the original .zip file so only a new folder remains with the name of the map and a few files in it. MTA can read both folders and .zip-files as a map, but if you got both a .zip-file ßnd a folder that contains the same map, MTA won't see any difference and use them as two seperate maps. NOTE: Make a copy of the map and put it somewhere safe, so you got a backup if anything goes wrong.
We start by creating a .lua-script in the map-folder. In this whole tutorial, I'm using Notepad as editor for the script.
Start up the programme (Notepad), and save the empty file in the map-folder (menu: File -> Save as). When you do this, choose "All files" in the "Save as type" box, below the box where you fill in the file name. Then, name the file "yourname.lua" (the name must not include spaces). The ".lua" at the end is VERY important. By selecting "All files" in the "Save as type" box, Notepad allows you to choose a filetype yourself, which you can assign to the file by typing the extension right after the name. Save the file. If you now hover over the file in the folder, it should say "LUA-file". If it doesn't, try again.
The next step is to tell the map that it must use the script. First, you must know that the folder or the .zip-file that contains the map, always contains two files: the "yourname.map" file, which is the map, and another file called "meta.xml". When MTA reads the map, it first reads "meta.xml", because that's the file that contains valuable information about the map: where can I find the scripts, the map itself, who is the author and even what weather the map uses, if you choose to define that in "meta.xml".
Okay, so we need to define the script in "meta.xml". Use Notepad to open the file. The file will contain a few things. First, on top and at the end it should contain a <meta> (the start) and a </meta> (the end) tag. Between these tags, several paths and information are defined (like in html). You'll probably see a "<map src="yourname.map"/>" tag, which defines where the .map-file can be found. The path to the .map file starts in the folder where "meta.xml" is found. "meta.xml" must always be directly in the map folder, otherwise MTA won't recognize the map.
Somewhere between the <meta> </meta> tags, you must create a line that defines the path to the .lua-script. If your script has <settings></settings> tags, do not put it between these tags!
You can copy-paste this line:
<script src="yourname.lua" type="server" />
Don't forget to change some things! First, you must change the name ("yourname.lua") to the name of your script (quite obvious). The second thing that is important to know, is the "type" bit. MTA uses two different sorts of scripts: client-side scripts and server-side scripts. Client-side scripts are scripts that are downloaded to the user's PC and can differ at actions per person in the server. For example, client-side scripts are usded to change models ingame, but also the syntax of some functions can differ depending on the script-type. Server-side scripts are scripts that perform actions which affect everyone in the server, such as explosions and objects that are created by scripts.
Our scripts must be server-side, because the objects must threat all players equal at the same time, so there's no player left behind in actions. The type is "server".
Save meta.xml and open the .lua-script you made earlier.
3. THE SCRIPT: CREATING A FUNCTION, ADDING EVENTHANDLERS AND CREATING OBJECTS
I want this tutorial to be clear too for people that are new to .lua-scripting, so I'll explain everything.
First, we must tell the script what to do when the map is started. I'll first explain how a .lua-script works.
Almost every script-language works with functions. A function contains actions and tells the game what to do when something happens.
Every function looks like this:
function resource_starts () end
Syntax (= what all parts mean):
- "function"": Tells the game that this is a new function - "resource_says_hai": The name of the function. A script can not contain multiple functions with the same name. The name must contain spaces ("blabla" is correct, "bla bla" is incorrect). The fact that I used "resource_starts" as name does not have anything to do with the function of the function (if you get what I mean). For example, I can call the function that is executed when the script starts "whazaaahhhhdupdupilikebeeer", because only the name itself is important, not the meaning of the words it contains. - "()": Between these brackets, you can type names that refer to things in the game, such as the player or a vehicle.
- "end": This is the end of the function. The amount of "ends" must always be equal to the amount of functions, otherwise the script will crash.
Ok, fun, we've got a function now. But erm.. If there is like ten functions in one script, how does MTA know what function to run when something happens? That's not hard to explain.
MTA uses "eventhandlers" to link certain events (for example sitting in a car), causes (the person that's sitting in the car) and effects (what happens if that person get's in a car = the function). There is a lot of different eventhandlers. In for this function, I'll use the eventhandler for starting the script:
Eventhandlers must be OUTSIDE other functions. The best way to avoid eventhandlers being included in functions, is to simply put all eventhandlers at the top of your script.
- "addEventHandler": The name of the function (you can't CAN NOT change that name).
After the name of the function, the brackets come with the information that is neede for the function between it:
- ""onresourcestart"": The name of the event (you can find more events here: http://wiki.multitheftauto.com/wiki/Scripting_Events ) - "getRootElement()": Don't ask my how or why, but this means "all players in the game". You can also put a name here, so only this player will be affected on what happens by this eventhandler. - "resource_starts": What function should be executed when the event (starting the resource) is caused by what element ("getRootElement()" in this case = everyone).
So, now we've scripted that our function "resource_starts" will be started when the resource is started!
Let's start scripting what happens when the function "resource_starts" is executed. In this tutorial I will use three objects to explain how it works.
If you don't use rotations at all (as in my case), you can simply leave them out of the function. You can not leave the co÷rdinates out of it. NOTE: If you do want a Z-rotation, but not a X- and Y-rotation, you DO HAVE to include the X- and Y-rotation otherwise the script will interpretate the Z-rotation as the X-rotation.
4. THE SCRIPT: CREATING A FUNCTION, ADDING EVENTHANDLERS AND CREATING OBJECTS
You don't always need an eventhandler to execute a function. You can also just execute the function by putting the name of the function inside another function. I'm using that method in this tutorial because that makes the script a lot clearer:
Again, you can leave the new rotation values out of the function if you want, just like I did.
Important to know is that time in scripts is ALWAYS given in milliseconds (1 second is 1000 milliseconds)!
What I've scripted: The object called "object1" will be moved in 4500 milliseconds = 4,5 second to X=4520.808105, Y=-1569.693359 and Z=46.449081. The speed depends on the distance between the new and old co÷rdinates. You might need to test it a few times and adjust the speed by changing the times.
setTimer ( function_that_is_executed_after_epilapsed_time, time, times_to_execute )
In this case, the function called " "move 1_2" will be executed 4,5 seconds after the timer has started, which is 4,5 seconds after the objects have began to move, which is the moment that they arrive at their new destination! If you want the objects to stand still for a moment, you just increase the time in the setTimer-function so that it's more than the time in the moveObject-function.
1. When the script starts, a few objects are created and after that the function called "move1_1" is executed. 2. In "move1_1" the objects are moved to a new position and at exactly the same time that the objects reach their new position, "move1_2" is executed. 3. Function "move1_2" moves the object back to their original position and at exactly the same time that the objects reach their new (= old) position, "move1_1" is executed and the proces (2 and 3) is started over again. 4. This continues forever, until the resource is stopped and the next resource is started.