Puma Posted April 4, 2010 Share Posted April 4, 2010 (edited) --------------------------------------------------------------------------------------------Creating moving objects in MTA:DM--------------------------------------------------------------------------------------------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.Ok, so how do I do that?--------------------------------------------------------------------------------------------1. PREPARATIONSFirst, 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".--------------------------------------------------------------------------------------------2. META.XMLOkay, 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 OBJECTSI 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 ()endSyntax (= 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:addEventHandler ( "onResourceStart", getRootElement(), resource_starts )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.Syntax:- "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.First, we create three objects by script:addEventHandler ( "onResourceStart", getRootElement(), resource_starts )function resource_starts ()createObject ( 17950, 4520.808105, -1569.693359, 34.411907 )createObject ( 17950, 4513.611816, -1569.802612, 46.449081 )createObject ( 17950, 4506.434570, -1569.778076, 34.411907 )endSyntax (I'm talking about the "createObject" stuff now):createObject ( model-ID, X-coördinate, Y-coördinate, Z-coördinate, X-rotation, Y-rotation, Z-rotation )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.Read more about createObject here: http://wiki.multitheftauto.com/wiki/CreateObjectWe have to define those objects, so we can refer to them later in this tutorial. Do this by simply putting "name =" in front of everything:addEventHandler ( "onResourceStart", getRootElement(), resource_starts )function resource_starts ()object1 = createObject ( 17950, 4520.808105, -1569.693359, 34.411907 )object2 = createObject ( 17950, 4513.611816, -1569.802612, 46.449081 )object3 = createObject ( 17950, 4506.434570, -1569.778076, 34.411907 )endAll names that are given to the objects must be different. Now we've created the objects, we must get them to move.--------------------------------------------------------------------------------------------4. THE SCRIPT: CREATING A FUNCTION, ADDING EVENTHANDLERS AND CREATING OBJECTSYou 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:addEventHandler ( "onResourceStart", getRootElement(), resource_starts )function resource_starts ()object1 = createObject ( 17950, 4520.808105, -1569.693359, 34.411907 )object2 = createObject ( 17950, 4513.611816, -1569.802612, 46.449081 )object3 = createObject ( 17950, 4506.434570, -1569.778076, 34.411907 )move1_1 ()endNow, after the three objects are created, the script will jump to a function called "move1_1". We'll create that function now:addEventHandler ( "onResourceStart", getRootElement(), resource_starts )function resource_starts ()object1 = createObject ( 17950, 4520.808105, -1569.693359, 34.411907 )object2 = createObject ( 17950, 4513.611816, -1569.802612, 46.449081 )object3 = createObject ( 17950, 4506.434570, -1569.778076, 34.411907 )move1_1 ()endfunction move1_1 ()endLet's fill it in. We need a function that makes the objects move: moveObject. With the "moveObject" function, you can make a specific object that is created in the same script (very important) move to a specific coördinate in a specific time. It will look like this:addEventHandler ( "onResourceStart", getRootElement(), resource_starts )function resource_starts ()object1 = createObject ( 17950, 4520.808105, -1569.693359, 34.411907 )object2 = createObject ( 17950, 4513.611816, -1569.802612, 46.449081 )object3 = createObject ( 17950, 4506.434570, -1569.778076, 34.411907 )move1_1 ()endfunction move1_1 ()moveObject ( object1, 4500, 4520.808105, -1569.693359, 46.449081 )moveObject ( object2, 4500, 4513.611816, -1569.802612, 34.411907 )moveObject ( object3, 4500, 4506.434570, -1569.778076, 46.449081 )endSyntax of moveObject:moveObject ( name_of_the_object, moving_time, new_x-coördinate, new_y-coördinate, new_z-coördinate, new_x-rotation, new_y-rotation, new_z-rotation )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.Read more about moveObject here: http://wiki.multitheftauto.com/wiki/MoveObjectNow comes a very important part of the script. After the objects have reached their new destinations, we want them to return to their first position. How do we do that? Simple. We use a timer:addEventHandler ( "onResourceStart", getRootElement(), resource_starts )function resource_starts ()object1 = createObject ( 17950, 4520.808105, -1569.693359, 34.411907 )object2 = createObject ( 17950, 4513.611816, -1569.802612, 46.449081 )object3 = createObject ( 17950, 4506.434570, -1569.778076, 34.411907 )move1_1 ()endfunction move1_1 ()moveObject ( object1, 4500, 4520.808105, -1569.693359, 46.449081 )moveObject ( object2, 4500, 4513.611816, -1569.802612, 34.411907 )moveObject ( object3, 4500, 4506.434570, -1569.778076, 46.449081 )setTimer ( move1_2, 4500, 1 )endSyntax: 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.Read more about "setTimer" here: http://wiki.multitheftauto.com/wiki/SetTimerNext function is the "move1_2" function which is basically the same function as "move1_1" but with other coördinates and a new timer that refers to "move1_1":function resource_starts ()object1 = createObject ( 17950, 4520.808105, -1569.693359, 34.411907 )object2 = createObject ( 17950, 4513.611816, -1569.802612, 46.449081 )object3 = createObject ( 17950, 4506.434570, -1569.778076, 34.411907 )move1_1 ()endfunction move1_1 ()moveObject ( object1, 4500, 4520.808105, -1569.693359, 46.449081 )moveObject ( object2, 4500, 4513.611816, -1569.802612, 34.411907 )moveObject ( object3, 4500, 4506.434570, -1569.778076, 46.449081 )setTimer ( move1_2, 4500, 1 )endfunction move1_2 ()moveObject ( object1, 4500, 4520.808105, -1569.693359, 34.411907 )moveObject ( object2, 4500, 4513.611816, -1569.802612, 46.449081 )moveObject ( object3, 4500, 4506.434570, -1569.778076, 34.411907 )setTimer ( move1_1, 4500, 1 )endWhat the script does in very short terms: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.--------------------------------------------------------------------------------------------Hope you learned something today, a comment would be nice! Have fun with the tutorial.by Pumahttp://www.pumastudios.nlhttp://www.youtube.com/StijnLinderman Edited April 4, 2010 by Puma Quote Link to comment
BlueYoshi97 Posted April 4, 2010 Share Posted April 4, 2010 Thank you so much, Puma! This topic arrived right on time for me too... Quote Link to comment
Lukkie Posted April 4, 2010 Share Posted April 4, 2010 As already posted on the mta forums, nice tutorial; but I finally had my script working a few hours earlier than this topic Quote Link to comment
Retard Posted April 4, 2010 Share Posted April 4, 2010 omg really amaising map. i would really like to play it. but it could get lagged cuz so many moving objects no? well u really got imagination there Quote Link to comment
BlueYoshi97 Posted August 28, 2010 Share Posted August 28, 2010 This is scary. I was just using this topic, and when I went to the Main Page I suddenly saw this topic again, it has been bumped while I was watching it!Um anyway. This is too hard for me it seems, because I tried a lot but I really can't get any of this to work. I even managed to fuck up editor_test and now I have to reinstall MTA all over again, just by trying this. Quote Link to comment
QTesla Posted August 28, 2010 Share Posted August 28, 2010 Although I don't play MTA, I gotta say it's a nice tutorial. Very detailed. Good job. Quote Link to comment
kevin Posted August 28, 2010 Share Posted August 28, 2010 This is scary. I was just using this topic, and when I went to the Main Page I suddenly saw this topic again, it has been bumped while I was watching it!Um anyway. This is too hard for me it seems, because I tried a lot but I really can't get any of this to work. I even managed to fuck up editor_test and now I have to reinstall MTA all over again, just by trying this.yes me 2 Quote Link to comment
Juggernaut Posted August 29, 2010 Share Posted August 29, 2010 Very nice tutorial. I am sure a lot of MTAr's here will find it very useful. Quote Link to comment
Ruff Posted August 29, 2010 Share Posted August 29, 2010 Looks nice and easy. I'll probably use it in one of my maps.Thanks a lot, Puma! Quote Link to comment
CsenaHUN Posted September 15, 2010 Share Posted September 15, 2010 (edited) "4. This continues forever, until the resource is stopped and the next resource is started."Not for me. This happens all the time when i tested the map in map editor:object created move1_1move1_2move1_1and after that my object stop. Why? I have no idea. I use this kind of script on my Floodrace map for explosion and that script never stoped like this. Any idea?Edit: works fine without Wojak's huntbot script. wierd. when the hunter start shooting at me the next moveing wont start. Edited September 15, 2010 by CsenaHUN Quote Link to comment
CsenaHUN Posted November 7, 2010 Share Posted November 7, 2010 Who dont want scripting:http://community.mtasa.com/index.php?p=resources&s=details&id=1224 Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.