<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Kamshak</id>
	<title>Multi Theft Auto: Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Kamshak"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/wiki/Special:Contributions/Kamshak"/>
	<updated>2026-04-06T02:21:23Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=Advanced_Topics&amp;diff=25753</id>
		<title>Advanced Topics</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=Advanced_Topics&amp;diff=25753"/>
		<updated>2011-05-08T19:21:03Z</updated>

		<summary type="html">&lt;p&gt;Kamshak: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Needs Checking|Outdated.}}&lt;br /&gt;
This page explains some advanced things you can do with scripts, this page should probably be changed later, depending how useful it is.&lt;br /&gt;
&lt;br /&gt;
==Custom element types==&lt;br /&gt;
One of the most powerful areas of MTA scripting is custom element types. These types can be defined in map files and their functionality implemented by scripts. In this example, we're going to look at a custom ''flag'' element, such as you would use in a ''capture-the-flag'' game-mode, though this same method could be used for many different things.&lt;br /&gt;
&lt;br /&gt;
First of all you need to decide what format you want the element to be defined in, we're going to use the following:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;[pua,N]&lt;br /&gt;
&amp;lt;flag id=&amp;quot;redFlag&amp;quot; name=&amp;quot;red team flag&amp;quot; posX=&amp;quot;1210.0&amp;quot; posY=&amp;quot;-2241.34&amp;quot; posZ=&amp;quot;18.4332&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you can define the flag element in the map file.&lt;br /&gt;
&lt;br /&gt;
Next, you can write some code to handle your flag element. First of all, lets add an event that happens when you pick it up, called ''onFlagPickup''. We should do this in an external script file, lets call this ''capture-the-flag.lua''. At the top of the file (outside any functions), define the event:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
addEvent ( &amp;quot;onFlagPickup&amp;quot;, &amp;quot;player&amp;quot; )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Easy enough! We've added an event that will have a parameter called ''player''. We'll use this to say which player picked up the flag. Next, we have a choice. Either we can add an event handler to the root of the map, and capture every flag pickup event, or we can add it to the individual flag. Its easiest if we use the root of the map, so lets do this here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
mapRoot = getRootElement ( )&lt;br /&gt;
addEventHandler ( &amp;quot;onFlagPickup&amp;quot;, mapRoot, &amp;quot;flagPickup&amp;quot; )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This says that we want to direct all ''onFlagPickup'' events to our function called ''flagPickup'', that we're about to define on the following line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function flagPickup ( player )&lt;br /&gt;
	flagName = getElementData ( source, &amp;quot;name&amp;quot; )&lt;br /&gt;
	playerName = getPlayerName ( player )&lt;br /&gt;
	outputChatBox ( playerName .. &amp;quot; picked up the flag &amp;quot; .. flagName )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The final part of the puzzle is to make it so that the event is triggered at the correct time. To do this, we'll need to create a timer that will be used to check if a player is trying to pick up the flag. Put the following code below your previous [[addEventHandler]] call:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
setTimer ( &amp;quot;flagCheckPulse&amp;quot;, 1000 )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will make our soon-to-be-defined function ''flagCheckPulse'' be called every second. Now we'll define this function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function flagCheckPulse ( )&lt;br /&gt;
	local flags = getElementsByType ( &amp;quot;flag&amp;quot; )&lt;br /&gt;
	local players = getElementsByType ( &amp;quot;player&amp;quot; )&lt;br /&gt;
	local flagPositionX, flagPositionY, flagPositionZ&lt;br /&gt;
	for flagKey,flagValue in flags do&lt;br /&gt;
		flagX = getElementData ( flagValue, &amp;quot;posX&amp;quot; )&lt;br /&gt;
		flagY = getElementData ( flagValue, &amp;quot;posY&amp;quot; )&lt;br /&gt;
		flagZ = getElementData ( flagValue, &amp;quot;posZ&amp;quot; )       &lt;br /&gt;
		for playerKey,playerValue in players do&lt;br /&gt;
			playerX, playerY, playerZ = getEntityPosition ( playerValue )&lt;br /&gt;
			distance = getDistanceBetweenPoints3D ( flagX, flagY, flagZ, playerX, playerY, playerZ )&lt;br /&gt;
			if ( distance &amp;lt; 1) then&lt;br /&gt;
				triggerEvent ( &amp;quot;onFlagPickup&amp;quot;, flagValue, playerValue )&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	setTimer ( flagCheckPulse, 1000 )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And thats it!&lt;br /&gt;
&lt;br /&gt;
Note: Although this should work, its not tested. Some things could be improved if better support was provided by us, for example, theres no getPlayerByIndex, no getElementCount(type), an onPlayerPulse event would be useful. This currently only works for one flag, as theres I'm not sure how to find how many flags getElementByType ( &amp;quot;flag&amp;quot; ) returned.&lt;br /&gt;
&lt;br /&gt;
Next: representing the flag in the world - spawning an object at the flags position.&lt;/div&gt;</summary>
		<author><name>Kamshak</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=Talk:GivePlayerMoney&amp;diff=25751</id>
		<title>Talk:GivePlayerMoney</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=Talk:GivePlayerMoney&amp;diff=25751"/>
		<updated>2011-05-08T15:21:24Z</updated>

		<summary type="html">&lt;p&gt;Kamshak: Created page with &amp;quot;what does this function do on the client? isn't money serverside? ~~~~&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;what does this function do on the client? isn't money serverside? [[User:Kamshak|Kamshak]] 17:21, 8 May 2011 (CEST)&lt;/div&gt;</summary>
		<author><name>Kamshak</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=GetDistanceBetweenPoints3D&amp;diff=25750</id>
		<title>GetDistanceBetweenPoints3D</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=GetDistanceBetweenPoints3D&amp;diff=25750"/>
		<updated>2011-05-08T12:30:18Z</updated>

		<summary type="html">&lt;p&gt;Kamshak: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Server client function}}&lt;br /&gt;
__NOTOC__&lt;br /&gt;
This function returns the distance between two 3 dimensional points using the pythagorean theorem.&lt;br /&gt;
This involves a square root operation and is thus computationally expensive.&lt;br /&gt;
&lt;br /&gt;
==Syntax==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;float getDistanceBetweenPoints3D ( float x1, float y1, float z1, float x2, float y2, float z2 )&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Required Arguments===&lt;br /&gt;
* '''x1''': The X position of the first point&lt;br /&gt;
* '''y1''': The Y position of the first point&lt;br /&gt;
* '''z1''': The Z position of the first point&lt;br /&gt;
* '''x2''': The X position of the second point&lt;br /&gt;
* '''y2''': The Y position of the second point&lt;br /&gt;
* '''z2''': The Z position of the second point&lt;br /&gt;
&lt;br /&gt;
===Returns===&lt;br /&gt;
Returns a float containing the distance between the two points as a [[float]]. Returns ''false'' if an argument passed was invalid.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&amp;lt;section name=&amp;quot;Server and client&amp;quot; class=&amp;quot;both&amp;quot; show=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
This example gets the distance between two vehicles and outputs it to the chat box.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
vehicle1x, vehicle1y, vehicle1z = getElementPosition ( vehicle1 )&lt;br /&gt;
vehicle2x, vehicle2y, vehicle2z = getElementPosition ( vehicle2 )&lt;br /&gt;
outputChatBox ( &amp;quot;The distance between vehicle1 and vehicle2 is &amp;quot;..tostring(getDistanceBetweenPoints3D ( vehicle1x, vehicle1y, vehicle1z, vehicle2x,&lt;br /&gt;
 vehicle2y, vehicle2z )) )&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/section&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
{{Utility functions}}&lt;/div&gt;</summary>
		<author><name>Kamshak</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=User:Kamshak&amp;diff=25697</id>
		<title>User:Kamshak</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=User:Kamshak&amp;diff=25697"/>
		<updated>2011-05-04T17:19:17Z</updated>

		<summary type="html">&lt;p&gt;Kamshak: Created page with &amp;quot;Hello :)&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Hello :)&lt;/div&gt;</summary>
		<author><name>Kamshak</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=Talk:Optional_Arguments&amp;diff=25696</id>
		<title>Talk:Optional Arguments</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=Talk:Optional_Arguments&amp;diff=25696"/>
		<updated>2011-05-04T17:18:44Z</updated>

		<summary type="html">&lt;p&gt;Kamshak: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Optional Arguments have one limitation. You cannot use any optional arguments unless all previous arguments are also supplied.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
What if you replace one of the previous arguments with 'nil'? Atleast that works with addBan().&lt;br /&gt;
: It depends on the function, some support that, some don't. [[User:EAi|eAi]] 21:43, 4 January 2011 (UTC)&lt;br /&gt;
: Lua always passes nil for elements that are not specified so it should work with all functions(cf. Programming in Lua 2nd edition p.36), unless one argument requires another optional argument to work(for example colors, you can not set red to nil and green and blue to 255 and expect it to work) [[User:Kamshak|Kamshak]] 19:18, 4 May 2011 (CEST)&lt;/div&gt;</summary>
		<author><name>Kamshak</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=Talk:Optional_Arguments&amp;diff=25695</id>
		<title>Talk:Optional Arguments</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=Talk:Optional_Arguments&amp;diff=25695"/>
		<updated>2011-05-04T17:17:03Z</updated>

		<summary type="html">&lt;p&gt;Kamshak: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;quot;Optional Arguments have one limitation. You cannot use any optional arguments unless all previous arguments are also supplied.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
What if you replace one of the previous arguments with 'nil'? Atleast that works with addBan().&lt;br /&gt;
: It depends on the function, some support that, some don't. [[User:EAi|eAi]] 21:43, 4 January 2011 (UTC)&lt;br /&gt;
: Lua always passes nil for elements that are not specified so it should work with all functions(cf. Programming in Lua 2nd edition p.36), unless one argument requires another optional argument to work(for example colors, you can not set red to nil and green and blue to 255 and expect it to work)&lt;/div&gt;</summary>
		<author><name>Kamshak</name></author>
	</entry>
</feed>