https://wiki.multitheftauto.com/api.php?action=feedcontributions&user=Ixjf&feedformat=atomMulti Theft Auto: Wiki - User contributions [en]2024-03-28T12:10:49ZUser contributionsMediaWiki 1.39.3https://wiki.multitheftauto.com/index.php?title=OOP&diff=41350OOP2014-08-10T19:17:20Z<p>Ixjf: /* Turning it on */</p>
<hr />
<div>Object Orientated Programming is introduced in MTA:SA 1.4 and comes with special utility classes like [[Vector]] and [[Matrix]]. This page contains general information about the OOP functions and provides useful links.<br />
<br />
== Turning it on ==<br />
By default, OOP is disabled - this is mainly because the vast majority of servers will prefer to stick to what they know - procedural programming. In fact, functions are still available even when OOP is enabled, just like vectors and matrices are always available, regardless of whether OOP is enabled or disabled. Enabling OOP is as simple as adding the following line to the resource meta file:<br />
<syntaxhighlight lang="xml"><oop>true</oop></syntaxhighlight><br />
<br />
== Vectors and Matrices ==<br />
[[Vector|Vectors]] and [[Matrix|Matrices]] make it easier to drop the complex maths and go straight ahead with fun part of maths. As mentioned above, OOP does not have to be enabled in the server config for this to be enabled.<br />
<br />
==OOP Metatable Structure==<br />
You will undersand this if you're an advanced scripter with an understanding in metatables.<br />
<syntaxhighlight lang="lua"><br />
-- Exposed to global environment<br />
Element = {<br />
create = createElement,<br />
setPosition = setElementPosition,<br />
...<br />
}<br />
<br />
Vehicle = {<br />
create = createVehicle,<br />
setColor = setVehicleColor,<br />
...<br />
}<br />
<br />
-- Hidden in lua registry, applied to userdata<br />
ElementMT = {<br />
__index = CLuaClassDefs::Index,<br />
__newindex = CLuaClassDefs::NewIndex,<br />
__class = Element,<br />
__call = __class.create,<br />
__set = {<br />
type = CLuaClassDefs::ReadOnly,<br />
health = setElementHealth,<br />
...<br />
},<br />
__get = {<br />
type = getElementType,<br />
health = getElementHealth,<br />
...<br />
},<br />
}<br />
<br />
VehicleMT = {<br />
__index = CLuaClassDefs::Index,<br />
__newindex = CLuaClassDefs::NewIndex,<br />
__class = Vehicle,<br />
__parent = ElementMT,<br />
__call = __class.create,<br />
__set = {<br />
damageProof = setVehicleDamageProof<br />
...<br />
},<br />
__get = {<br />
damageProof = isVehicleDamageProof<br />
...<br />
},<br />
}<br />
</syntaxhighlight><br />
<br />
== Things to do ==<br />
* Document all the existing functions in OOP form on the documentation<br />
<br />
== Useful Links ==<br />
* '''[[OOP Introduction]]''' - teaches you about the basics of OOP<br />
* '''[[OOP_client|Function list (client)]]''' - the most up to date list of functions, you should be able to infer from this.<br />
* '''[[OOP_server|Function list (server)]]''' - the most up to date list of functions, you should be able to infer from this.</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=OOP&diff=41349OOP2014-08-10T19:16:15Z<p>Ixjf: </p>
<hr />
<div>Object Orientated Programming is introduced in MTA:SA 1.4 and comes with special utility classes like [[Vector]] and [[Matrix]]. This page contains general information about the OOP functions and provides useful links.<br />
<br />
== Turning it on ==<br />
By default, OOP is disabled (however, vectors and matrices are always available) - this is mainly because the vast majority of servers will prefer to stick to what they know - procedural programming. In fact, functions are still available even when OOP is enabled. Enabling OOP is as simple as adding the following line to the resource meta file:<br />
<syntaxhighlight lang="xml"><oop>true</oop></syntaxhighlight><br />
<br />
== Vectors and Matrices ==<br />
[[Vector|Vectors]] and [[Matrix|Matrices]] make it easier to drop the complex maths and go straight ahead with fun part of maths. As mentioned above, OOP does not have to be enabled in the server config for this to be enabled.<br />
<br />
==OOP Metatable Structure==<br />
You will undersand this if you're an advanced scripter with an understanding in metatables.<br />
<syntaxhighlight lang="lua"><br />
-- Exposed to global environment<br />
Element = {<br />
create = createElement,<br />
setPosition = setElementPosition,<br />
...<br />
}<br />
<br />
Vehicle = {<br />
create = createVehicle,<br />
setColor = setVehicleColor,<br />
...<br />
}<br />
<br />
-- Hidden in lua registry, applied to userdata<br />
ElementMT = {<br />
__index = CLuaClassDefs::Index,<br />
__newindex = CLuaClassDefs::NewIndex,<br />
__class = Element,<br />
__call = __class.create,<br />
__set = {<br />
type = CLuaClassDefs::ReadOnly,<br />
health = setElementHealth,<br />
...<br />
},<br />
__get = {<br />
type = getElementType,<br />
health = getElementHealth,<br />
...<br />
},<br />
}<br />
<br />
VehicleMT = {<br />
__index = CLuaClassDefs::Index,<br />
__newindex = CLuaClassDefs::NewIndex,<br />
__class = Vehicle,<br />
__parent = ElementMT,<br />
__call = __class.create,<br />
__set = {<br />
damageProof = setVehicleDamageProof<br />
...<br />
},<br />
__get = {<br />
damageProof = isVehicleDamageProof<br />
...<br />
},<br />
}<br />
</syntaxhighlight><br />
<br />
== Things to do ==<br />
* Document all the existing functions in OOP form on the documentation<br />
<br />
== Useful Links ==<br />
* '''[[OOP Introduction]]''' - teaches you about the basics of OOP<br />
* '''[[OOP_client|Function list (client)]]''' - the most up to date list of functions, you should be able to infer from this.<br />
* '''[[OOP_server|Function list (server)]]''' - the most up to date list of functions, you should be able to infer from this.</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=OOP_Introduction&diff=41348OOP Introduction2014-08-10T19:12:11Z<p>Ixjf: </p>
<hr />
<div>{{Note_box|If you've contributed by editing and tweaking this forum page, if you've benefited from this tutorial, or if you have anything to say; please give me feedback in the forum link provided on the left.}}<br />
This is a scripting tutorial explaining to you what object orientated programming is and teaching you how to use the OOP features of MTA. This was originally created by [[User:Qais|qaisjp]] ([[User talk:Qais|talk]]) 22:48, 8 June 2014 (UTC). [http://forum.mtasa.com/viewtopic.php?f=148&t=76388 Forum post.]<br />
<br />
__TOC__<br />
== Introduction to OOP ==<br />
OOP stands for ''object orientated programming''. Three simple words, and you'll probably understand the last word the most. OOP is where all functions relating to a single instance are called on that instance, an instance being a creation of a class - an element class, a database class, a player, a vehicle. Originally, everything was ''procedural'', you had to do things like:<br />
<syntaxhighlight lang="lua"><br />
local vehicle = createVehicle(411, 0, 0, 3)<br />
setVehicleDamageProof(vehicle, true)<br />
setElementFrozen(vehicle, true)<br />
setElementHealth(vehicle, 1000)<br />
setElementVelocity(vehicle, 0.2, 0.2, 0.2)<br />
destroyElement(vehicle)<br />
</syntaxhighlight><br />
More often than not, you know what you're dealing with. The variable almost always links back to the type, you would name a vehicle that exploded as '''explodedVehicle''', or at-least in context you would understand that '''exploded''' implies a vehicle when in the '''onVehicleExplode''' event. So you have to write a long function ''and'' refer to the vehicle manually when working procedurally. Painful. Object orientated programming is very different to this and works with each "object" individually. Instead of having to call a function and referencing it inside the call, you actually call the function INSIDE the class.<br />
<br />
You probably think everything you can create and pass to functions are elements. A vehicle is an element. A player is an element. Anything that is an element is also a class. Connections create an instance of a class, but "'''connection'''" isn't an element, it's an instance - an object. Throughout the tutorial when I say ''object'', I don't mean [[createObject]] (unless I actually mention it, but to make things clearer I will avoid mentioning physical objects as I write this article. Here is a fancy venn diagram I created to show the simple organisation of classes and elements.<br />
[[File:Classes,_Elements_and_Problem_children.png|500px|left|venn diagram]]<br />
The functions on the left are sorted to show what kind of category the returned value rests in. We've got Classes, Elements and "Problem children".<br />
Problem children aren't real categories written in the code, just functions that break rules. Everything you can play with are classes: resources, vehicles, and teams.<br />
All elements are classes, you can do: <syntaxhighlight lang="lua">destroyElement(ped)</syntaxhighlight> but you can't do: <syntaxhighlight lang="lua">destroyElement(resource)</syntaxhighlight><br />
<br />
Problem children are weird things. You can't do all the functions mentioned in (actually, all elements don't allow the full assortment of functions to be applied to them, but I've especially mentioned a few of them) in the "Element functions" section of the functions list, but you can do destroyElement() on them.<br />
There are children of classes, for example, with players, the system goes like: ''Element -> Ped -> Player". All Players are Peds and all Peds are Elements. Not all Peds are Players, and certainly not all Elements are Players. The main point here is that almost everything that you can create or retrieve and then reuse later use a class.<br />
<br />
Instead of the code before, the code could be replaced with this:<br />
<syntaxhighlight lang="lua"><br />
local vehicle = createVehicle(411, 0, 0, 3)<br />
vehicle:setDamageProof(true)<br />
vehicle:setFrozen(true)<br />
vehicle:setHealth(1000)<br />
vehicle:setVelocity(0.2, 0.2, 0.2)<br />
vehicle:destroy()<br />
</syntaxhighlight><br />
It works pretty similar to how a table works, it's just like customTable.setSomething() except the use of ''':''' makes Lua convert customTable:setSomething() convert to customTable.setSomething(customTable). This is pretty internal stuff about ''syntactic sugar'' and you don't really need to worry much about it.<br />
<br />
Those functions are pretty useful, but there are more changes with OOP, I'll explain this below.<br />
<br />
== Instantiation, variables ==<br />
OOP removes the need to say the "create" part of the function, so instead of saying '''createVehicle''', you just say '''Vehicle'''. It works exactly the same way, it's almost just like doing '''Vehicle = createVehicle'''. Fancy, isn't it? The only difference here is that you miss out on the extra things offered, Vehicle doesn't have these extra things, but Player definitely does. For example, instead of doing getPlayerFromName(), you would do Player.getFromName(). It's a nice and simple way to organise functions.<br />
<br />
Since OOP sits on top of procedural, many things have been inherited from the style of procedural, but to make things easier we have variables for all the functions that require a single input. We've shortened '''getElementDimension()''' down to '''element:getDimension()''', but we can also go one layer deeper: '''element.dimension'''. Yep, just like a variable. You can set this variable just like a normal variable and read from it just like a normal variable. Hey, you could even do this:<br />
<syntaxhighlight lang="lua">local function incrementDimension()<br />
local player = Player.getRandom() -- get a random player<br />
player.dimension = player.dimension + 1 -- increment dimension<br />
end<br />
setTimer(incrementDimension, 60*1000, 10) -- set a timer for sixty thousand milliseconds, sixty seconds, one minute<br />
</syntaxhighlight><br />
This code would take a random player and move them to the next dimension every minute for the next ten minutes.<br />
<br />
== Vectors ==<br />
player.position works too! But how do you change three arguments... using one variable? Vectors.<br />
Vectors are very powerful classes and they come in multiple forms, for the purpose of this introduction I'll just cover a three dimensional vector in terms of elements. Using a vector is very simple, and is, of course, optional. Wherever you can currently use positions, you can use a vector.<br />
<br />
So, this is a simple example of creating a vehicle and moving it to the the centre of the map using vectors<br />
<syntaxhighlight lang="lua"><br />
-- First, create a three-dimensional vector<br />
local position = Vector3(300, -200, 2) -- some place far away<br />
local vehicle = Vehicle(411, position) -- create a vehicle at the position<br />
vehicle.position = centreOfMap - Vector3(300, -200, 0) -- move the vehicle two units above the center of the map<br />
</syntaxhighlight><br />
<br />
Yes, I used the negative sign. Vectors aren't just fancy ways for positions or 3d rotations or whatever, you can use maths on them. The ''special'' maths hasn't been documented yet, but I'll try and work on that. So, as you can see in line one, I created a 3D vector at ''300, -200, 2'' and then in line two I created the vehicle at that position.<br />
<br />
'''vehicle.position''' returned a vector and also takes a vector - it is pretty much setElementPosition() without the "()". Just a simple variable; so, in line three, I changed the vector value of the position of the vehicle. This is where the maths happened, in simple terms this is what is going on:<br />
<syntaxhighlight lang="lua"><br />
x = 300 - 300 (> 300-300 = 0)<br />
y = -200 - -200 (> -200+200 = 0)<br />
y = 2 - 0 (> 2-0 = 0)<br />
</syntaxhighlight><br />
<br />
Vector maths is slightly complicated but it definitely allows for a wide variety of mathematical magic. Check out the useful links below related to Vectors and Matrices (Matrices = plural form of Matrix) to understand more about how this works.<br />
<br />
== Useful links ==<br />
Other useful OOP related tutorials:<br />
* [[Manipulating Vectors]]<br />
* [[Manipulating Matrices]]<br />
<br />
[[Category:Incomplete]]<br />
[[Category:Tutorials]]</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=Template:Client_cursor_functions&diff=41116Template:Client cursor functions2014-08-02T11:07:03Z<p>Ixjf: </p>
<hr />
<div>*[[getCursorPosition]]<br />
*[[setCursorPosition]]<br />
*[[isCursorShowing]]<br />
*[[showCursor]]<br />
{{New feature/item|3.0132|1.3.2|5340|<br />
* [[setCursorAlpha]]<br />
* [[getCursorAlpha]]<br />
}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=Template:GUI_functions&diff=41115Template:GUI functions2014-08-02T11:06:25Z<p>Ixjf: </p>
<hr />
<div>__NOTOC__<br />
* [[getChatboxLayout]]<br />
* [[guiBringToFront]]<br />
* [[guiCreateFont]]<br />
* [[guiGetAlpha]]<br />
* [[guiGetEnabled]]<br />
* [[guiGetFont]]<br />
* [[guiGetInputEnabled]]<br />
* [[guiGetInputMode]]<br />
* [[guiGetPosition]]<br />
* [[guiGetProperties]]<br />
* [[guiGetProperty]]<br />
* [[guiGetScreenSize]]<br />
* [[guiGetSize]]<br />
* [[guiGetText]]<br />
* [[guiGetVisible]]<br />
* [[guiMoveToBack]]<br />
* [[guiSetAlpha]]<br />
* [[guiSetEnabled]]<br />
* [[guiSetFont]]<br />
* [[guiSetInputEnabled]]<br />
* [[guiSetInputMode]]<br />
* [[guiSetPosition]]<br />
* [[guiSetProperty]]<br />
* [[guiSetSize]]<br />
* [[guiSetText]]<br />
* [[guiSetVisible]]<br />
* [[isChatBoxInputActive]]<br />
* [[isConsoleActive]]<br />
* [[isDebugViewActive]]<br />
* [[isMainMenuActive]]<br />
* [[isMTAWindowActive]]<br />
* [[isTransferBoxActive]]<br />
<br />
===Buttons===<br />
* [[guiCreateButton]]<br />
<br />
===Checkboxes===<br />
* [[guiCheckBoxGetSelected]]<br />
* [[guiCheckBoxSetSelected]]<br />
* [[guiCreateCheckBox]]<br />
<br />
===Comboboxes===<br />
* [[guiCreateComboBox]]<br />
* [[guiComboBoxAddItem]]<br />
* [[guiComboBoxClear]]<br />
* [[guiComboBoxGetItemText]]<br />
* [[guiComboBoxSetItemText]]<br />
* [[guiComboBoxRemoveItem]]<br />
* [[guiComboBoxGetSelected]]<br />
* [[guiComboBoxSetSelected]]<br />
<br />
===Edit Fields===<br />
* [[guiCreateEdit]]<br />
* [[guiEditSetMasked]]<br />
* [[guiEditSetMaxLength]]<br />
* [[guiEditSetReadOnly]]<br />
* [[guiEditSetCaretIndex]]<br />
{{New feature/item|3.0135|1.3.5|6054|<br />
* [[guiEditGetCaretIndex]]<br />
}}<br />
===Gridlists===<br />
* [[guiCreateGridList]]<br />
* [[guiGridListAddColumn]]<br />
* [[guiGridListAddRow]]<br />
* [[guiGridListAutoSizeColumn]]<br />
* [[guiGridListClear]]<br />
* [[guiGridListGetItemData]]<br />
* [[guiGridListGetItemText]]<br />
* [[guiGridListGetRowCount]]<br />
* [[guiGridListGetSelectedItem]]<br />
* [[guiGridListInsertRowAfter]]<br />
* [[guiGridListRemoveColumn]]<br />
* [[guiGridListRemoveRow]]<br />
* [[guiGridListSetItemData]]<br />
* [[guiGridListSetItemText]]<br />
* [[guiGridListSetScrollBars]]<br />
* [[guiGridListSetSelectedItem]]<br />
* [[guiGridListSetSelectionMode]]<br />
* [[guiGridListSetSortingEnabled]]<br />
* [[guiGridListGetSelectedCount]]<br />
* [[guiGridListGetSelectedItems]]<br />
* [[guiGridListSetColumnWidth]]<br />
* [[guiGridListGetColumnCount]]<br />
* [[guiGridListGetItemColor]]<br />
* [[guiGridListSetItemColor]]<br />
{{New feature/item|3.0132|1.3.1|4944|<br />
* [[guiGridListGetColumnTitle]]<br />
* [[guiGridListSetColumnTitle]]<br />
}}<br />
{{New feature/item|3.0132|1.3.1|5087|<br />
* [[guiGridListGetHorizontalScrollPosition]]<br />
* [[guiGridListSetHorizontalScrollPosition]]<br />
* [[guiGridListGetVerticalScrollPosition]]<br />
* [[guiGridListSetVerticalScrollPosition]]<br />
}}<br />
<br />
===Memos===<br />
* [[guiCreateMemo]]<br />
* [[guiMemoSetReadOnly]]<br />
* [[guiMemoSetCaretIndex]]<br />
{{New feature/item|3.0135|1.3.5|6054|<br />
* [[guiMemoGetCaretIndex]]<br />
}}<br />
<br />
===Progressbars===<br />
* [[guiCreateProgressBar]]<br />
* [[guiProgressBarGetProgress]]<br />
* [[guiProgressBarSetProgress]]<br />
<br />
===Radio Buttons===<br />
* [[guiCreateRadioButton]]<br />
* [[guiRadioButtonGetSelected]]<br />
* [[guiRadioButtonSetSelected]]<br />
<br />
===Scrollbars===<br />
* [[guiCreateScrollBar]]<br />
* [[guiScrollBarGetScrollPosition]]<br />
* [[guiScrollBarSetScrollPosition]]<br />
<br />
===Scrollpanes===<br />
* [[guiCreateScrollPane]]<br />
* [[guiScrollPaneGetHorizontalScrollPosition]]<br />
* [[guiScrollPaneGetVerticalScrollPosition]]<br />
* [[guiScrollPaneSetHorizontalScrollPosition]]<br />
* [[guiScrollPaneSetScrollBars]]<br />
* [[guiScrollPaneSetVerticalScrollPosition]]<br />
<br />
===Static Images===<br />
* [[guiCreateStaticImage]]<br />
* [[guiStaticImageGetNativeSize]]<br />
* [[guiStaticImageLoadImage]]<br />
<br />
===Tab Panels===<br />
* [[guiCreateTabPanel]]<br />
* [[guiGetSelectedTab]]<br />
* [[guiSetSelectedTab]]<br />
<br />
===Tabs===<br />
* [[guiCreateTab]]<br />
* [[guiDeleteTab]]<br />
<br />
===Text Labels===<br />
* [[guiCreateLabel]]<br />
* [[guiLabelGetFontHeight]]<br />
* [[guiLabelGetTextExtent]]<br />
* [[guiLabelSetColor]]<br />
* [[guiLabelSetHorizontalAlign]]<br />
* [[guiLabelSetVerticalAlign]]<br />
{{New feature/item|3.0132|1.3.1|4670|<br />
* [[guiLabelGetColor]]<br />
}}<br />
<br />
===Windows===<br />
* [[guiCreateWindow]]<br />
* [[guiWindowSetMovable]]<br />
* [[guiWindowSetSizable]]<br />
<br />
<noinclude>[[Category:Functions templates]]</noinclude></div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GenerateString&diff=40920GenerateString2014-07-18T17:10:23Z<p>Ixjf: </p>
<hr />
<div>{{Useful Function}}<br />
__NOTOC__<br />
Generates a random sequence of alphanumeric characters.<br />
<br />
==Syntax==<br />
<syntaxhighlight lang="lua">string generateString ( int length )</syntaxhighlight><br />
<br />
===Required Arguments===<br />
* '''length''': The length of the sequence to generate.<br />
<br />
===Returns===<br />
Returns a string with the specified length, or false if an invalid length argument was passed.<br />
<br />
==Code==<br />
<section name="Client- and/or serverside Script" class="both" show="true"><br />
<syntaxhighlight lang="lua">local allowed = { { 48, 57 }, { 65, 90 }, { 97, 122 } } -- numbers/lowercase chars/uppercase chars<br />
<br />
function GenerateString ( len )<br />
<br />
if tonumber ( len ) then<br />
math.randomseed ( getTickCount () )<br />
<br />
local str = ""<br />
for i = 1, len do<br />
local charlist = allowed[math.random ( 1, 3 )]<br />
str = str .. string.char ( math.random ( charlist[1], charlist[2] ) )<br />
end<br />
<br />
return str<br />
end<br />
<br />
return false<br />
<br />
end<br />
</syntaxhighlight><br />
</section><br />
<br />
==Example==<br />
<section name="Server" class="server" show="true"><br />
This example generates a sequence with 8 characters and outputs it to the chatbox.<br />
<syntaxhighlight lang="lua"><br />
outputChatBox ( "Generated string was: " .. generateString ( 8 ), root, 0, 255, 0 )<br />
</syntaxhighlight><br />
</section><br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GenerateString&diff=40919GenerateString2014-07-18T17:10:08Z<p>Ixjf: </p>
<hr />
<div>{{Useful Function}}<br />
Generates a random sequence of alphanumeric characters.<br />
<br />
==Syntax==<br />
<syntaxhighlight lang="lua">string generateString ( int length )</syntaxhighlight><br />
<br />
===Required Arguments===<br />
* '''length''': The length of the sequence to generate.<br />
<br />
===Returns===<br />
Returns a string with the specified length, or false if an invalid length argument was passed.<br />
<br />
==Code==<br />
<section name="Client- and/or serverside Script" class="both" show="true"><br />
<syntaxhighlight lang="lua">local allowed = { { 48, 57 }, { 65, 90 }, { 97, 122 } } -- numbers/lowercase chars/uppercase chars<br />
<br />
function GenerateString ( len )<br />
<br />
if tonumber ( len ) then<br />
math.randomseed ( getTickCount () )<br />
<br />
local str = ""<br />
for i = 1, len do<br />
local charlist = allowed[math.random ( 1, 3 )]<br />
str = str .. string.char ( math.random ( charlist[1], charlist[2] ) )<br />
end<br />
<br />
return str<br />
end<br />
<br />
return false<br />
<br />
end<br />
</syntaxhighlight><br />
</section><br />
<br />
==Example==<br />
<section name="Server" class="server" show="true"><br />
This example generates a sequence with 8 characters and outputs it to the chatbox.<br />
<syntaxhighlight lang="lua"><br />
outputChatBox ( "Generated string was: " .. generateString ( 8 ), root, 0, 255, 0 )<br />
</syntaxhighlight><br />
</section><br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GenerateString&diff=40918GenerateString2014-07-18T17:09:36Z<p>Ixjf: </p>
<hr />
<div>{{Useful Function}}<br />
__NOTOC__<br />
Generates a random sequence of alphanumeric characters.<br />
<br />
==Syntax==<br />
<syntaxhighlight lang="lua">string generateString ( int length )</syntaxhighlight><br />
<br />
===Required Arguments===<br />
* '''length''': The length of the sequence to generate.<br />
<br />
===Returns===<br />
Returns a string with the specified length, or false if an invalid length argument was passed.<br />
<br />
==Code==<br />
<section name="Client- and/or serverside Script" class="both" show="true"><br />
<syntaxhighlight lang="lua">local allowed = { { 48, 57 }, { 65, 90 }, { 97, 122 } } -- numbers/lowercase chars/uppercase chars<br />
<br />
function GenerateString ( len )<br />
<br />
if tonumber ( len ) then<br />
math.randomseed ( getTickCount () )<br />
<br />
local str = ""<br />
for i = 1, len do<br />
local charlist = allowed[math.random ( 1, 3 )]<br />
str = str .. string.char ( math.random ( charlist[1], charlist[2] ) )<br />
end<br />
<br />
return str<br />
end<br />
<br />
return false<br />
<br />
end<br />
</syntaxhighlight><br />
</section><br />
<br />
==Example==<br />
<section name="Server" class="server" show="true"><br />
This example generates a sequence with 8 characters and outputs it to the chatbox.<br />
<syntaxhighlight lang="lua"><br />
outputChatBox ( "Generated string was: " .. generateString ( 8 ), root, 0, 255, 0 )<br />
</syntaxhighlight><br />
</section><br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=OOP_Introduction&diff=40671OOP Introduction2014-07-11T20:47:04Z<p>Ixjf: /* Enabling OOP */</p>
<hr />
<div>{{Note_box|If you've contributed by editing and tweaking this forum page, if you've benefited from this tutorial, or if you have anything to say; please give me feedback in the forum link provided on the left.}}<br />
This is a scripting tutorial explaining to you what object orientated programming is and teaching you how to use the OOP features of MTA. This was originally created by [[User:Qais|qaisjp]] ([[User talk:Qais|talk]]) 22:48, 8 June 2014 (UTC). [http://forum.mtasa.com/viewtopic.php?f=148&t=76388 Forum post.]<br />
<br />
__TOC__<br />
== Introduction to OOP ==<br />
OOP stands for ''object orientated programming''. Three simple words, and you'll probably understand the last word the most. OOP is where all functions relating to a single instance are called on that instance, an instance being a creation of a class - an element class, a database class, a player, a vehicle. Originally, everything was ''procedural'', you had to do things like:<br />
<syntaxhighlight lang="lua"><br />
local vehicle = createVehicle(411, 0, 0, 3)<br />
setVehicleDamageProof(vehicle, true)<br />
setElementFrozen(vehicle, true)<br />
setElementHealth(vehicle, 1000)<br />
setElementVelocity(vehicle, 0.2, 0.2, 0.2)<br />
destroyElement(vehicle)<br />
</syntaxhighlight><br />
More often than not, you know what you're dealing with. The variable almost always links back to the type, you would name a vehicle that exploded as '''explodedVehicle''', or at-least in context you would understand that '''exploded''' implies a vehicle when in the '''onVehicleExplode''' event. So you have to write a long function ''and'' refer to the vehicle manually when working procedurally. Painful. Object orientated programming is very different to this and works with each "object" individually. Instead of having to call a function and referencing it inside the call, you actually call the function INSIDE the class.<br />
<br />
You probably think everything you can create and pass to functions are elements. A vehicle is an element. A player is an element. Anything that is an element is also a class. Connections create an instance of a class, but "'''connection'''" isn't an element, it's an instance - an object. Throughout the tutorial when I say ''object'', I don't mean [[createObject]] (unless I actually mention it, but to make things clearer I will avoid mentioning physical objects as I write this article. Here is a fancy venn diagram I created to show the simple organisation of classes and elements.<br />
[[File:Classes,_Elements_and_Problem_children.png|500px|left|venn diagram]]<br />
The functions on the left are sorted to show what kind of category the returned value rests in. We've got Classes, Elements and "Problem children".<br />
Problem children aren't real categories written in the code, just functions that break rules. Everything you can play with are classes: resources, vehicles, and teams.<br />
All elements are classes, you can do: <syntaxhighlight lang="lua">destroyElement(ped)</syntaxhighlight> but you can't do: <syntaxhighlight lang="lua">destroyElement(resource)</syntaxhighlight><br />
<br />
Problem children are weird things. You can't do all the functions mentioned in (actually, all elements don't allow the full assortment of functions to be applied to them, but I've especially mentioned a few of them) in the "Element functions" section of the functions list, but you can do destroyElement() on them.<br />
There are children of classes, for example, with players, the system goes like: ''Element -> Ped -> Player". All Players are Peds and all Peds are Elements. Not all Peds are Players, and certainly not all Elements are Players. The main point here is that almost everything that you can create or retrieve and then reuse later use a class.<br />
<br />
Instead of the code before, the code could be replaced with this:<br />
<syntaxhighlight lang="lua"><br />
local vehicle = createVehicle(411, 0, 0, 3)<br />
vehicle:setDamageProof(true)<br />
vehicle:setFrozen(true)<br />
vehicle:setHealth(1000)<br />
vehicle:setVelocity(0.2, 0.2, 0.2)<br />
vehicle:destroy()<br />
</syntaxhighlight><br />
It works pretty similar to how a table works, it's just like customTable.setSomething() except the use of ''':''' makes Lua convert customTable:setSomething() convert to customTable.setSomething(customTable). This is pretty internal stuff about ''syntactic sugar'' and you don't really need to worry much about it.<br />
<br />
Those functions are pretty useful, but there are more changes with OOP, I'll explain this below.<br />
<br />
== Enabling OOP ==<br />
By default, object oriented programming is disabled - this is mainly because the vast majority of servers will prefer to stick to what they know - same old boring procedural functions. Of course, to support the great backwards-compatible-code feature of MTA, the old functions will always work and will never get removed (or atleast not in the near future). Even with OOP enabled everything will "just work". So, enabling OOP is as simple as adding:<br />
<syntaxhighlight lang="xml"><oop>true</oop></syntaxhighlight> <br />
to the resource meta file.<br />
<br />
== Instantiation, variables ==<br />
OOP removes the need to say the "create" part of the function, so instead of saying '''createVehicle''', you just say '''Vehicle'''. It works exactly the same way, it's almost just like doing '''Vehicle = createVehicle'''. Fancy, isn't it? The only difference here is that you miss out on the extra things offered, Vehicle doesn't have these extra things, but Player definitely does. For example, instead of doing getPlayerFromName(), you would do Player.getFromName(). It's a nice and simple way to organise functions.<br />
<br />
Since OOP sits on top of procedural, many things have been inherited from the style of procedural, but to make things easier we have variables for all the functions that require a single input. We've shortened '''getElementDimension()''' down to '''element:getDimension()''', but we can also go one layer deeper: '''element.dimension'''. Yep, just like a variable. You can set this variable just like a normal variable and read from it just like a normal variable. Hey, you could even do this:<br />
<syntaxhighlight lang="lua">local function incrementDimension()<br />
local player = Player.getRandom() -- get a random player<br />
player.dimension = player.dimension + 1 -- increment dimension<br />
end<br />
setTimer(incrementDimension, 60*1000, 10) -- set a timer for sixty thousand milliseconds, sixty seconds, one minute<br />
</syntaxhighlight><br />
This code would take a random player and move them to the next dimension every minute for the next ten minutes.<br />
<br />
== Vectors ==<br />
player.position works too! But how do you change three arguments... using one variable? Vectors.<br />
Vectors are very powerful classes and they come in multiple forms, for the purpose of this introduction I'll just cover a three dimensional vector in terms of elements. Using a vector is very simple, and is, of course, optional. Wherever you can currently use positions, you can use a vector.<br />
<br />
So, this is a simple example of creating a vehicle and moving it to the the centre of the map using vectors<br />
<syntaxhighlight lang="lua"><br />
-- First, create a three-dimensional vector<br />
local position = Vector3(300, -200, 2) -- some place far away<br />
local vehicle = Vehicle(411, position) -- create a vehicle at the position<br />
vehicle.position = centreOfMap - Vector3(300, -200, 0) -- move the vehicle two units above the center of the map<br />
</syntaxhighlight><br />
<br />
Yes, I used the negative sign. Vectors aren't just fancy ways for positions or 3d rotations or whatever, you can use maths on them. The ''specjal'' maths hasn't been documented yet, but I'll try and work on that. So, as you can see in line one, I created a 3D vector at ''300, -200, 2'' and then in line two I created the vehicle at that position.<br />
<br />
'''vehicle.position''' returned a vector and also takes a vector - it is pretty much setElementPosition() without the "()". Just a simple variable; so, in line three, I changed the vector value of the position of the vehicle. This is where the maths happened, in simple terms this is what is going on:<br />
<syntaxhighlight lang="lua"><br />
x = 300 - 300 (> 300-300 = 0)<br />
y = -200 - -200 (> -200+200 = 0)<br />
y = 2 - 0 (> 2-0 = 0)<br />
</syntaxhighlight><br />
<br />
Vector maths is slightly complicated but it definitely allows for a wide variety of mathematical magic. Check out the useful links below related to Vectors and Matrices (Matrices = plural form of Matrix) to understand more about how this works.<br />
<br />
== Useful links ==<br />
Other useful OOP related tutorials:<br />
* [[Manipulating Vectors]]<br />
* [[Manipulating Matrices]]<br />
<br />
[[Category:Incomplete]]<br />
[[Category:Tutorials]]</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=OOP_Introduction&diff=40670OOP Introduction2014-07-11T20:46:49Z<p>Ixjf: /* Enabling OOP */</p>
<hr />
<div>{{Note_box|If you've contributed by editing and tweaking this forum page, if you've benefited from this tutorial, or if you have anything to say; please give me feedback in the forum link provided on the left.}}<br />
This is a scripting tutorial explaining to you what object orientated programming is and teaching you how to use the OOP features of MTA. This was originally created by [[User:Qais|qaisjp]] ([[User talk:Qais|talk]]) 22:48, 8 June 2014 (UTC). [http://forum.mtasa.com/viewtopic.php?f=148&t=76388 Forum post.]<br />
<br />
__TOC__<br />
== Introduction to OOP ==<br />
OOP stands for ''object orientated programming''. Three simple words, and you'll probably understand the last word the most. OOP is where all functions relating to a single instance are called on that instance, an instance being a creation of a class - an element class, a database class, a player, a vehicle. Originally, everything was ''procedural'', you had to do things like:<br />
<syntaxhighlight lang="lua"><br />
local vehicle = createVehicle(411, 0, 0, 3)<br />
setVehicleDamageProof(vehicle, true)<br />
setElementFrozen(vehicle, true)<br />
setElementHealth(vehicle, 1000)<br />
setElementVelocity(vehicle, 0.2, 0.2, 0.2)<br />
destroyElement(vehicle)<br />
</syntaxhighlight><br />
More often than not, you know what you're dealing with. The variable almost always links back to the type, you would name a vehicle that exploded as '''explodedVehicle''', or at-least in context you would understand that '''exploded''' implies a vehicle when in the '''onVehicleExplode''' event. So you have to write a long function ''and'' refer to the vehicle manually when working procedurally. Painful. Object orientated programming is very different to this and works with each "object" individually. Instead of having to call a function and referencing it inside the call, you actually call the function INSIDE the class.<br />
<br />
You probably think everything you can create and pass to functions are elements. A vehicle is an element. A player is an element. Anything that is an element is also a class. Connections create an instance of a class, but "'''connection'''" isn't an element, it's an instance - an object. Throughout the tutorial when I say ''object'', I don't mean [[createObject]] (unless I actually mention it, but to make things clearer I will avoid mentioning physical objects as I write this article. Here is a fancy venn diagram I created to show the simple organisation of classes and elements.<br />
[[File:Classes,_Elements_and_Problem_children.png|500px|left|venn diagram]]<br />
The functions on the left are sorted to show what kind of category the returned value rests in. We've got Classes, Elements and "Problem children".<br />
Problem children aren't real categories written in the code, just functions that break rules. Everything you can play with are classes: resources, vehicles, and teams.<br />
All elements are classes, you can do: <syntaxhighlight lang="lua">destroyElement(ped)</syntaxhighlight> but you can't do: <syntaxhighlight lang="lua">destroyElement(resource)</syntaxhighlight><br />
<br />
Problem children are weird things. You can't do all the functions mentioned in (actually, all elements don't allow the full assortment of functions to be applied to them, but I've especially mentioned a few of them) in the "Element functions" section of the functions list, but you can do destroyElement() on them.<br />
There are children of classes, for example, with players, the system goes like: ''Element -> Ped -> Player". All Players are Peds and all Peds are Elements. Not all Peds are Players, and certainly not all Elements are Players. The main point here is that almost everything that you can create or retrieve and then reuse later use a class.<br />
<br />
Instead of the code before, the code could be replaced with this:<br />
<syntaxhighlight lang="lua"><br />
local vehicle = createVehicle(411, 0, 0, 3)<br />
vehicle:setDamageProof(true)<br />
vehicle:setFrozen(true)<br />
vehicle:setHealth(1000)<br />
vehicle:setVelocity(0.2, 0.2, 0.2)<br />
vehicle:destroy()<br />
</syntaxhighlight><br />
It works pretty similar to how a table works, it's just like customTable.setSomething() except the use of ''':''' makes Lua convert customTable:setSomething() convert to customTable.setSomething(customTable). This is pretty internal stuff about ''syntactic sugar'' and you don't really need to worry much about it.<br />
<br />
Those functions are pretty useful, but there are more changes with OOP, I'll explain this below.<br />
<br />
== Enabling OOP ==<br />
By default, object oriented programming is disabled - this is mainly because the vast majority of servers will prefer to stick to what they know - same old boring procedural functions. Of course, to support the great backwards-compatible-code feature of MTA, the old functions will always work and will never get removed (or atleast not in the near future). Even with OOP enabled everything will "just work". So, enabling OOP is as simple as adding<br />
<syntaxhighlight lang="xml"><oop>true</oop></syntaxhighlight> <br />
to the resource meta file.<br />
<br />
== Instantiation, variables ==<br />
OOP removes the need to say the "create" part of the function, so instead of saying '''createVehicle''', you just say '''Vehicle'''. It works exactly the same way, it's almost just like doing '''Vehicle = createVehicle'''. Fancy, isn't it? The only difference here is that you miss out on the extra things offered, Vehicle doesn't have these extra things, but Player definitely does. For example, instead of doing getPlayerFromName(), you would do Player.getFromName(). It's a nice and simple way to organise functions.<br />
<br />
Since OOP sits on top of procedural, many things have been inherited from the style of procedural, but to make things easier we have variables for all the functions that require a single input. We've shortened '''getElementDimension()''' down to '''element:getDimension()''', but we can also go one layer deeper: '''element.dimension'''. Yep, just like a variable. You can set this variable just like a normal variable and read from it just like a normal variable. Hey, you could even do this:<br />
<syntaxhighlight lang="lua">local function incrementDimension()<br />
local player = Player.getRandom() -- get a random player<br />
player.dimension = player.dimension + 1 -- increment dimension<br />
end<br />
setTimer(incrementDimension, 60*1000, 10) -- set a timer for sixty thousand milliseconds, sixty seconds, one minute<br />
</syntaxhighlight><br />
This code would take a random player and move them to the next dimension every minute for the next ten minutes.<br />
<br />
== Vectors ==<br />
player.position works too! But how do you change three arguments... using one variable? Vectors.<br />
Vectors are very powerful classes and they come in multiple forms, for the purpose of this introduction I'll just cover a three dimensional vector in terms of elements. Using a vector is very simple, and is, of course, optional. Wherever you can currently use positions, you can use a vector.<br />
<br />
So, this is a simple example of creating a vehicle and moving it to the the centre of the map using vectors<br />
<syntaxhighlight lang="lua"><br />
-- First, create a three-dimensional vector<br />
local position = Vector3(300, -200, 2) -- some place far away<br />
local vehicle = Vehicle(411, position) -- create a vehicle at the position<br />
vehicle.position = centreOfMap - Vector3(300, -200, 0) -- move the vehicle two units above the center of the map<br />
</syntaxhighlight><br />
<br />
Yes, I used the negative sign. Vectors aren't just fancy ways for positions or 3d rotations or whatever, you can use maths on them. The ''specjal'' maths hasn't been documented yet, but I'll try and work on that. So, as you can see in line one, I created a 3D vector at ''300, -200, 2'' and then in line two I created the vehicle at that position.<br />
<br />
'''vehicle.position''' returned a vector and also takes a vector - it is pretty much setElementPosition() without the "()". Just a simple variable; so, in line three, I changed the vector value of the position of the vehicle. This is where the maths happened, in simple terms this is what is going on:<br />
<syntaxhighlight lang="lua"><br />
x = 300 - 300 (> 300-300 = 0)<br />
y = -200 - -200 (> -200+200 = 0)<br />
y = 2 - 0 (> 2-0 = 0)<br />
</syntaxhighlight><br />
<br />
Vector maths is slightly complicated but it definitely allows for a wide variety of mathematical magic. Check out the useful links below related to Vectors and Matrices (Matrices = plural form of Matrix) to understand more about how this works.<br />
<br />
== Useful links ==<br />
Other useful OOP related tutorials:<br />
* [[Manipulating Vectors]]<br />
* [[Manipulating Matrices]]<br />
<br />
[[Category:Incomplete]]<br />
[[Category:Tutorials]]</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=OOP_Introduction&diff=40669OOP Introduction2014-07-11T20:46:29Z<p>Ixjf: /* Enabling OOP */</p>
<hr />
<div>{{Note_box|If you've contributed by editing and tweaking this forum page, if you've benefited from this tutorial, or if you have anything to say; please give me feedback in the forum link provided on the left.}}<br />
This is a scripting tutorial explaining to you what object orientated programming is and teaching you how to use the OOP features of MTA. This was originally created by [[User:Qais|qaisjp]] ([[User talk:Qais|talk]]) 22:48, 8 June 2014 (UTC). [http://forum.mtasa.com/viewtopic.php?f=148&t=76388 Forum post.]<br />
<br />
__TOC__<br />
== Introduction to OOP ==<br />
OOP stands for ''object orientated programming''. Three simple words, and you'll probably understand the last word the most. OOP is where all functions relating to a single instance are called on that instance, an instance being a creation of a class - an element class, a database class, a player, a vehicle. Originally, everything was ''procedural'', you had to do things like:<br />
<syntaxhighlight lang="lua"><br />
local vehicle = createVehicle(411, 0, 0, 3)<br />
setVehicleDamageProof(vehicle, true)<br />
setElementFrozen(vehicle, true)<br />
setElementHealth(vehicle, 1000)<br />
setElementVelocity(vehicle, 0.2, 0.2, 0.2)<br />
destroyElement(vehicle)<br />
</syntaxhighlight><br />
More often than not, you know what you're dealing with. The variable almost always links back to the type, you would name a vehicle that exploded as '''explodedVehicle''', or at-least in context you would understand that '''exploded''' implies a vehicle when in the '''onVehicleExplode''' event. So you have to write a long function ''and'' refer to the vehicle manually when working procedurally. Painful. Object orientated programming is very different to this and works with each "object" individually. Instead of having to call a function and referencing it inside the call, you actually call the function INSIDE the class.<br />
<br />
You probably think everything you can create and pass to functions are elements. A vehicle is an element. A player is an element. Anything that is an element is also a class. Connections create an instance of a class, but "'''connection'''" isn't an element, it's an instance - an object. Throughout the tutorial when I say ''object'', I don't mean [[createObject]] (unless I actually mention it, but to make things clearer I will avoid mentioning physical objects as I write this article. Here is a fancy venn diagram I created to show the simple organisation of classes and elements.<br />
[[File:Classes,_Elements_and_Problem_children.png|500px|left|venn diagram]]<br />
The functions on the left are sorted to show what kind of category the returned value rests in. We've got Classes, Elements and "Problem children".<br />
Problem children aren't real categories written in the code, just functions that break rules. Everything you can play with are classes: resources, vehicles, and teams.<br />
All elements are classes, you can do: <syntaxhighlight lang="lua">destroyElement(ped)</syntaxhighlight> but you can't do: <syntaxhighlight lang="lua">destroyElement(resource)</syntaxhighlight><br />
<br />
Problem children are weird things. You can't do all the functions mentioned in (actually, all elements don't allow the full assortment of functions to be applied to them, but I've especially mentioned a few of them) in the "Element functions" section of the functions list, but you can do destroyElement() on them.<br />
There are children of classes, for example, with players, the system goes like: ''Element -> Ped -> Player". All Players are Peds and all Peds are Elements. Not all Peds are Players, and certainly not all Elements are Players. The main point here is that almost everything that you can create or retrieve and then reuse later use a class.<br />
<br />
Instead of the code before, the code could be replaced with this:<br />
<syntaxhighlight lang="lua"><br />
local vehicle = createVehicle(411, 0, 0, 3)<br />
vehicle:setDamageProof(true)<br />
vehicle:setFrozen(true)<br />
vehicle:setHealth(1000)<br />
vehicle:setVelocity(0.2, 0.2, 0.2)<br />
vehicle:destroy()<br />
</syntaxhighlight><br />
It works pretty similar to how a table works, it's just like customTable.setSomething() except the use of ''':''' makes Lua convert customTable:setSomething() convert to customTable.setSomething(customTable). This is pretty internal stuff about ''syntactic sugar'' and you don't really need to worry much about it.<br />
<br />
Those functions are pretty useful, but there are more changes with OOP, I'll explain this below.<br />
<br />
== Enabling OOP ==<br />
By default, object oriented programming is disabled - this is mainly because the vast majority of servers will prefer to stick to what they know - same old boring procedural functions. Of course, to support the great backwards-compatible-code feature of MTA, the old functions will always work and will never get removed (or atleast not in the near future). Even with OOP enabled everything will "just work". So, enabling OOP is as simple as<br />
<syntaxhighlight lang="xml"><oop>true</oop></syntaxhighlight> <br />
to the resource meta file.<br />
<br />
== Instantiation, variables ==<br />
OOP removes the need to say the "create" part of the function, so instead of saying '''createVehicle''', you just say '''Vehicle'''. It works exactly the same way, it's almost just like doing '''Vehicle = createVehicle'''. Fancy, isn't it? The only difference here is that you miss out on the extra things offered, Vehicle doesn't have these extra things, but Player definitely does. For example, instead of doing getPlayerFromName(), you would do Player.getFromName(). It's a nice and simple way to organise functions.<br />
<br />
Since OOP sits on top of procedural, many things have been inherited from the style of procedural, but to make things easier we have variables for all the functions that require a single input. We've shortened '''getElementDimension()''' down to '''element:getDimension()''', but we can also go one layer deeper: '''element.dimension'''. Yep, just like a variable. You can set this variable just like a normal variable and read from it just like a normal variable. Hey, you could even do this:<br />
<syntaxhighlight lang="lua">local function incrementDimension()<br />
local player = Player.getRandom() -- get a random player<br />
player.dimension = player.dimension + 1 -- increment dimension<br />
end<br />
setTimer(incrementDimension, 60*1000, 10) -- set a timer for sixty thousand milliseconds, sixty seconds, one minute<br />
</syntaxhighlight><br />
This code would take a random player and move them to the next dimension every minute for the next ten minutes.<br />
<br />
== Vectors ==<br />
player.position works too! But how do you change three arguments... using one variable? Vectors.<br />
Vectors are very powerful classes and they come in multiple forms, for the purpose of this introduction I'll just cover a three dimensional vector in terms of elements. Using a vector is very simple, and is, of course, optional. Wherever you can currently use positions, you can use a vector.<br />
<br />
So, this is a simple example of creating a vehicle and moving it to the the centre of the map using vectors<br />
<syntaxhighlight lang="lua"><br />
-- First, create a three-dimensional vector<br />
local position = Vector3(300, -200, 2) -- some place far away<br />
local vehicle = Vehicle(411, position) -- create a vehicle at the position<br />
vehicle.position = centreOfMap - Vector3(300, -200, 0) -- move the vehicle two units above the center of the map<br />
</syntaxhighlight><br />
<br />
Yes, I used the negative sign. Vectors aren't just fancy ways for positions or 3d rotations or whatever, you can use maths on them. The ''specjal'' maths hasn't been documented yet, but I'll try and work on that. So, as you can see in line one, I created a 3D vector at ''300, -200, 2'' and then in line two I created the vehicle at that position.<br />
<br />
'''vehicle.position''' returned a vector and also takes a vector - it is pretty much setElementPosition() without the "()". Just a simple variable; so, in line three, I changed the vector value of the position of the vehicle. This is where the maths happened, in simple terms this is what is going on:<br />
<syntaxhighlight lang="lua"><br />
x = 300 - 300 (> 300-300 = 0)<br />
y = -200 - -200 (> -200+200 = 0)<br />
y = 2 - 0 (> 2-0 = 0)<br />
</syntaxhighlight><br />
<br />
Vector maths is slightly complicated but it definitely allows for a wide variety of mathematical magic. Check out the useful links below related to Vectors and Matrices (Matrices = plural form of Matrix) to understand more about how this works.<br />
<br />
== Useful links ==<br />
Other useful OOP related tutorials:<br />
* [[Manipulating Vectors]]<br />
* [[Manipulating Matrices]]<br />
<br />
[[Category:Incomplete]]<br />
[[Category:Tutorials]]</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=OOP_Introduction&diff=40668OOP Introduction2014-07-11T20:45:50Z<p>Ixjf: /* Enabling OOP */</p>
<hr />
<div>{{Note_box|If you've contributed by editing and tweaking this forum page, if you've benefited from this tutorial, or if you have anything to say; please give me feedback in the forum link provided on the left.}}<br />
This is a scripting tutorial explaining to you what object orientated programming is and teaching you how to use the OOP features of MTA. This was originally created by [[User:Qais|qaisjp]] ([[User talk:Qais|talk]]) 22:48, 8 June 2014 (UTC). [http://forum.mtasa.com/viewtopic.php?f=148&t=76388 Forum post.]<br />
<br />
__TOC__<br />
== Introduction to OOP ==<br />
OOP stands for ''object orientated programming''. Three simple words, and you'll probably understand the last word the most. OOP is where all functions relating to a single instance are called on that instance, an instance being a creation of a class - an element class, a database class, a player, a vehicle. Originally, everything was ''procedural'', you had to do things like:<br />
<syntaxhighlight lang="lua"><br />
local vehicle = createVehicle(411, 0, 0, 3)<br />
setVehicleDamageProof(vehicle, true)<br />
setElementFrozen(vehicle, true)<br />
setElementHealth(vehicle, 1000)<br />
setElementVelocity(vehicle, 0.2, 0.2, 0.2)<br />
destroyElement(vehicle)<br />
</syntaxhighlight><br />
More often than not, you know what you're dealing with. The variable almost always links back to the type, you would name a vehicle that exploded as '''explodedVehicle''', or at-least in context you would understand that '''exploded''' implies a vehicle when in the '''onVehicleExplode''' event. So you have to write a long function ''and'' refer to the vehicle manually when working procedurally. Painful. Object orientated programming is very different to this and works with each "object" individually. Instead of having to call a function and referencing it inside the call, you actually call the function INSIDE the class.<br />
<br />
You probably think everything you can create and pass to functions are elements. A vehicle is an element. A player is an element. Anything that is an element is also a class. Connections create an instance of a class, but "'''connection'''" isn't an element, it's an instance - an object. Throughout the tutorial when I say ''object'', I don't mean [[createObject]] (unless I actually mention it, but to make things clearer I will avoid mentioning physical objects as I write this article. Here is a fancy venn diagram I created to show the simple organisation of classes and elements.<br />
[[File:Classes,_Elements_and_Problem_children.png|500px|left|venn diagram]]<br />
The functions on the left are sorted to show what kind of category the returned value rests in. We've got Classes, Elements and "Problem children".<br />
Problem children aren't real categories written in the code, just functions that break rules. Everything you can play with are classes: resources, vehicles, and teams.<br />
All elements are classes, you can do: <syntaxhighlight lang="lua">destroyElement(ped)</syntaxhighlight> but you can't do: <syntaxhighlight lang="lua">destroyElement(resource)</syntaxhighlight><br />
<br />
Problem children are weird things. You can't do all the functions mentioned in (actually, all elements don't allow the full assortment of functions to be applied to them, but I've especially mentioned a few of them) in the "Element functions" section of the functions list, but you can do destroyElement() on them.<br />
There are children of classes, for example, with players, the system goes like: ''Element -> Ped -> Player". All Players are Peds and all Peds are Elements. Not all Peds are Players, and certainly not all Elements are Players. The main point here is that almost everything that you can create or retrieve and then reuse later use a class.<br />
<br />
Instead of the code before, the code could be replaced with this:<br />
<syntaxhighlight lang="lua"><br />
local vehicle = createVehicle(411, 0, 0, 3)<br />
vehicle:setDamageProof(true)<br />
vehicle:setFrozen(true)<br />
vehicle:setHealth(1000)<br />
vehicle:setVelocity(0.2, 0.2, 0.2)<br />
vehicle:destroy()<br />
</syntaxhighlight><br />
It works pretty similar to how a table works, it's just like customTable.setSomething() except the use of ''':''' makes Lua convert customTable:setSomething() convert to customTable.setSomething(customTable). This is pretty internal stuff about ''syntactic sugar'' and you don't really need to worry much about it.<br />
<br />
Those functions are pretty useful, but there are more changes with OOP, I'll explain this below.<br />
<br />
== Enabling OOP ==<br />
By default, object orientating programming is disabled - this is mainly because the vast majority of servers will prefer to stick to what they know - same old boring procedural functions. Of course, to support the great backwards-compatible-code feature of MTA, the old functions will always work and will never get removed (or atleast not in the near future). Even with OOP enabled everything will "just work". So, to enable OOP it's as simple as adding<br />
<syntaxhighlight lang="xml"><oop>true</oop></syntaxhighlight> <br />
to the resource meta file.<br />
<br />
== Instantiation, variables ==<br />
OOP removes the need to say the "create" part of the function, so instead of saying '''createVehicle''', you just say '''Vehicle'''. It works exactly the same way, it's almost just like doing '''Vehicle = createVehicle'''. Fancy, isn't it? The only difference here is that you miss out on the extra things offered, Vehicle doesn't have these extra things, but Player definitely does. For example, instead of doing getPlayerFromName(), you would do Player.getFromName(). It's a nice and simple way to organise functions.<br />
<br />
Since OOP sits on top of procedural, many things have been inherited from the style of procedural, but to make things easier we have variables for all the functions that require a single input. We've shortened '''getElementDimension()''' down to '''element:getDimension()''', but we can also go one layer deeper: '''element.dimension'''. Yep, just like a variable. You can set this variable just like a normal variable and read from it just like a normal variable. Hey, you could even do this:<br />
<syntaxhighlight lang="lua">local function incrementDimension()<br />
local player = Player.getRandom() -- get a random player<br />
player.dimension = player.dimension + 1 -- increment dimension<br />
end<br />
setTimer(incrementDimension, 60*1000, 10) -- set a timer for sixty thousand milliseconds, sixty seconds, one minute<br />
</syntaxhighlight><br />
This code would take a random player and move them to the next dimension every minute for the next ten minutes.<br />
<br />
== Vectors ==<br />
player.position works too! But how do you change three arguments... using one variable? Vectors.<br />
Vectors are very powerful classes and they come in multiple forms, for the purpose of this introduction I'll just cover a three dimensional vector in terms of elements. Using a vector is very simple, and is, of course, optional. Wherever you can currently use positions, you can use a vector.<br />
<br />
So, this is a simple example of creating a vehicle and moving it to the the centre of the map using vectors<br />
<syntaxhighlight lang="lua"><br />
-- First, create a three-dimensional vector<br />
local position = Vector3(300, -200, 2) -- some place far away<br />
local vehicle = Vehicle(411, position) -- create a vehicle at the position<br />
vehicle.position = centreOfMap - Vector3(300, -200, 0) -- move the vehicle two units above the center of the map<br />
</syntaxhighlight><br />
<br />
Yes, I used the negative sign. Vectors aren't just fancy ways for positions or 3d rotations or whatever, you can use maths on them. The ''specjal'' maths hasn't been documented yet, but I'll try and work on that. So, as you can see in line one, I created a 3D vector at ''300, -200, 2'' and then in line two I created the vehicle at that position.<br />
<br />
'''vehicle.position''' returned a vector and also takes a vector - it is pretty much setElementPosition() without the "()". Just a simple variable; so, in line three, I changed the vector value of the position of the vehicle. This is where the maths happened, in simple terms this is what is going on:<br />
<syntaxhighlight lang="lua"><br />
x = 300 - 300 (> 300-300 = 0)<br />
y = -200 - -200 (> -200+200 = 0)<br />
y = 2 - 0 (> 2-0 = 0)<br />
</syntaxhighlight><br />
<br />
Vector maths is slightly complicated but it definitely allows for a wide variety of mathematical magic. Check out the useful links below related to Vectors and Matrices (Matrices = plural form of Matrix) to understand more about how this works.<br />
<br />
== Useful links ==<br />
Other useful OOP related tutorials:<br />
* [[Manipulating Vectors]]<br />
* [[Manipulating Matrices]]<br />
<br />
[[Category:Incomplete]]<br />
[[Category:Tutorials]]</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=OOP_Introduction&diff=40667OOP Introduction2014-07-11T20:45:28Z<p>Ixjf: </p>
<hr />
<div>{{Note_box|If you've contributed by editing and tweaking this forum page, if you've benefited from this tutorial, or if you have anything to say; please give me feedback in the forum link provided on the left.}}<br />
This is a scripting tutorial explaining to you what object orientated programming is and teaching you how to use the OOP features of MTA. This was originally created by [[User:Qais|qaisjp]] ([[User talk:Qais|talk]]) 22:48, 8 June 2014 (UTC). [http://forum.mtasa.com/viewtopic.php?f=148&t=76388 Forum post.]<br />
<br />
__TOC__<br />
== Introduction to OOP ==<br />
OOP stands for ''object orientated programming''. Three simple words, and you'll probably understand the last word the most. OOP is where all functions relating to a single instance are called on that instance, an instance being a creation of a class - an element class, a database class, a player, a vehicle. Originally, everything was ''procedural'', you had to do things like:<br />
<syntaxhighlight lang="lua"><br />
local vehicle = createVehicle(411, 0, 0, 3)<br />
setVehicleDamageProof(vehicle, true)<br />
setElementFrozen(vehicle, true)<br />
setElementHealth(vehicle, 1000)<br />
setElementVelocity(vehicle, 0.2, 0.2, 0.2)<br />
destroyElement(vehicle)<br />
</syntaxhighlight><br />
More often than not, you know what you're dealing with. The variable almost always links back to the type, you would name a vehicle that exploded as '''explodedVehicle''', or at-least in context you would understand that '''exploded''' implies a vehicle when in the '''onVehicleExplode''' event. So you have to write a long function ''and'' refer to the vehicle manually when working procedurally. Painful. Object orientated programming is very different to this and works with each "object" individually. Instead of having to call a function and referencing it inside the call, you actually call the function INSIDE the class.<br />
<br />
You probably think everything you can create and pass to functions are elements. A vehicle is an element. A player is an element. Anything that is an element is also a class. Connections create an instance of a class, but "'''connection'''" isn't an element, it's an instance - an object. Throughout the tutorial when I say ''object'', I don't mean [[createObject]] (unless I actually mention it, but to make things clearer I will avoid mentioning physical objects as I write this article. Here is a fancy venn diagram I created to show the simple organisation of classes and elements.<br />
[[File:Classes,_Elements_and_Problem_children.png|500px|left|venn diagram]]<br />
The functions on the left are sorted to show what kind of category the returned value rests in. We've got Classes, Elements and "Problem children".<br />
Problem children aren't real categories written in the code, just functions that break rules. Everything you can play with are classes: resources, vehicles, and teams.<br />
All elements are classes, you can do: <syntaxhighlight lang="lua">destroyElement(ped)</syntaxhighlight> but you can't do: <syntaxhighlight lang="lua">destroyElement(resource)</syntaxhighlight><br />
<br />
Problem children are weird things. You can't do all the functions mentioned in (actually, all elements don't allow the full assortment of functions to be applied to them, but I've especially mentioned a few of them) in the "Element functions" section of the functions list, but you can do destroyElement() on them.<br />
There are children of classes, for example, with players, the system goes like: ''Element -> Ped -> Player". All Players are Peds and all Peds are Elements. Not all Peds are Players, and certainly not all Elements are Players. The main point here is that almost everything that you can create or retrieve and then reuse later use a class.<br />
<br />
Instead of the code before, the code could be replaced with this:<br />
<syntaxhighlight lang="lua"><br />
local vehicle = createVehicle(411, 0, 0, 3)<br />
vehicle:setDamageProof(true)<br />
vehicle:setFrozen(true)<br />
vehicle:setHealth(1000)<br />
vehicle:setVelocity(0.2, 0.2, 0.2)<br />
vehicle:destroy()<br />
</syntaxhighlight><br />
It works pretty similar to how a table works, it's just like customTable.setSomething() except the use of ''':''' makes Lua convert customTable:setSomething() convert to customTable.setSomething(customTable). This is pretty internal stuff about ''syntactic sugar'' and you don't really need to worry much about it.<br />
<br />
Those functions are pretty useful, but there are more changes with OOP, I'll explain this below.<br />
<br />
== Enabling OOP ==<br />
By default, object orientating programming is disabled - this is mainly because the vast majority of servers will prefer to stick to what they know - same old boring procedural functions. Of course, to support the great backwards-compatible-code feature of MTA, the old functions will always work and will never get removed (or atleast not in the near future). Even with OOP enabled everything will "just work". So, to enable OOP it's as simple as adding<br />
<syntaxhighlight lang="xml"><oop>true</oop></syntaxhighlight> to the resource meta file.<br />
<br />
== Instantiation, variables ==<br />
OOP removes the need to say the "create" part of the function, so instead of saying '''createVehicle''', you just say '''Vehicle'''. It works exactly the same way, it's almost just like doing '''Vehicle = createVehicle'''. Fancy, isn't it? The only difference here is that you miss out on the extra things offered, Vehicle doesn't have these extra things, but Player definitely does. For example, instead of doing getPlayerFromName(), you would do Player.getFromName(). It's a nice and simple way to organise functions.<br />
<br />
Since OOP sits on top of procedural, many things have been inherited from the style of procedural, but to make things easier we have variables for all the functions that require a single input. We've shortened '''getElementDimension()''' down to '''element:getDimension()''', but we can also go one layer deeper: '''element.dimension'''. Yep, just like a variable. You can set this variable just like a normal variable and read from it just like a normal variable. Hey, you could even do this:<br />
<syntaxhighlight lang="lua">local function incrementDimension()<br />
local player = Player.getRandom() -- get a random player<br />
player.dimension = player.dimension + 1 -- increment dimension<br />
end<br />
setTimer(incrementDimension, 60*1000, 10) -- set a timer for sixty thousand milliseconds, sixty seconds, one minute<br />
</syntaxhighlight><br />
This code would take a random player and move them to the next dimension every minute for the next ten minutes.<br />
<br />
== Vectors ==<br />
player.position works too! But how do you change three arguments... using one variable? Vectors.<br />
Vectors are very powerful classes and they come in multiple forms, for the purpose of this introduction I'll just cover a three dimensional vector in terms of elements. Using a vector is very simple, and is, of course, optional. Wherever you can currently use positions, you can use a vector.<br />
<br />
So, this is a simple example of creating a vehicle and moving it to the the centre of the map using vectors<br />
<syntaxhighlight lang="lua"><br />
-- First, create a three-dimensional vector<br />
local position = Vector3(300, -200, 2) -- some place far away<br />
local vehicle = Vehicle(411, position) -- create a vehicle at the position<br />
vehicle.position = centreOfMap - Vector3(300, -200, 0) -- move the vehicle two units above the center of the map<br />
</syntaxhighlight><br />
<br />
Yes, I used the negative sign. Vectors aren't just fancy ways for positions or 3d rotations or whatever, you can use maths on them. The ''specjal'' maths hasn't been documented yet, but I'll try and work on that. So, as you can see in line one, I created a 3D vector at ''300, -200, 2'' and then in line two I created the vehicle at that position.<br />
<br />
'''vehicle.position''' returned a vector and also takes a vector - it is pretty much setElementPosition() without the "()". Just a simple variable; so, in line three, I changed the vector value of the position of the vehicle. This is where the maths happened, in simple terms this is what is going on:<br />
<syntaxhighlight lang="lua"><br />
x = 300 - 300 (> 300-300 = 0)<br />
y = -200 - -200 (> -200+200 = 0)<br />
y = 2 - 0 (> 2-0 = 0)<br />
</syntaxhighlight><br />
<br />
Vector maths is slightly complicated but it definitely allows for a wide variety of mathematical magic. Check out the useful links below related to Vectors and Matrices (Matrices = plural form of Matrix) to understand more about how this works.<br />
<br />
== Useful links ==<br />
Other useful OOP related tutorials:<br />
* [[Manipulating Vectors]]<br />
* [[Manipulating Matrices]]<br />
<br />
[[Category:Incomplete]]<br />
[[Category:Tutorials]]</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=OOP&diff=40507OOP2014-07-03T14:54:28Z<p>Ixjf: </p>
<hr />
<div>Object Orientated Programming is introduced in MTA:SA 1.4 and comes with special utility classes like [[Vector]] and [[Matrix]]. This page contains general information about the OOP functions and provides useful links.<br />
<br />
== Turning it on ==<br />
You must set the <oop> key in the server configuration file from false to true to enable OOP in your server. However, Vectors and Matrices will always work regardless of OOP being enabled.<br />
<br />
== Vectors and Matrices ==<br />
[[Vector|Vectors]] and [[Matrix|Matrices]] make it easier to drop the complex maths and go straight ahead with fun part of maths. As mentioned above, OOP does not have to be enabled in the server config for this to be enabled.<br />
<br />
==OOP Metatable Structure==<br />
You will undersand this if you're an advanced scripter with an understanding in metatables.<br />
<syntaxhighlight lang="lua"><br />
-- Exposed to global environment<br />
Element = {<br />
create = createElement,<br />
setPosition = setElementPosition,<br />
...<br />
}<br />
<br />
Vehicle = {<br />
create = createVehicle,<br />
setColor = setVehicleColor,<br />
...<br />
}<br />
<br />
-- Hidden in lua registry, applied to userdata<br />
ElementMT = {<br />
__index = CLuaClassDefs::Index,<br />
__newindex = CLuaClassDefs::NewIndex,<br />
__class = Element,<br />
__call = __class.create,<br />
__set = {<br />
type = CLuaClassDefs::ReadOnly,<br />
health = setElementHealth,<br />
...<br />
},<br />
__get = {<br />
type = getElementType,<br />
health = getElementHealth,<br />
...<br />
},<br />
}<br />
<br />
VehicleMT = {<br />
__index = CLuaClassDefs::Index,<br />
__newindex = CLuaClassDefs::NewIndex,<br />
__class = Vehicle,<br />
__parent = ElementMT,<br />
__call = __class.create,<br />
__set = {<br />
damageProof = setVehicleDamageProof<br />
...<br />
},<br />
__get = {<br />
damageProof = isVehicleDamageProof<br />
...<br />
},<br />
}<br />
</syntaxhighlight><br />
<br />
== Things to do ==<br />
* Document all the existing functions in OOP form on the documentation<br />
<br />
== Useful Links ==<br />
* '''[[OOP Introduction]]''' - teaches you about the basics of OOP<br />
* '''[https://wiki.multitheftauto.com/wiki/OOP_client Function list (client)]''' - the most up to date list of functions, you should be able to infer from this.<br />
* '''[https://wiki.multitheftauto.com/wiki/OOP_server Function list (server)]''' - the most up to date list of functions, you should be able to infer from this.</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=XmlFindChild&diff=40354XmlFindChild2014-06-26T16:28:24Z<p>Ixjf: </p>
<hr />
<div>__NOTOC__<br />
{{Server client function}}<br />
This function returns a named child node of an XML node.<br />
<br />
==Syntax==<br />
<syntaxhighlight lang="lua">xmlnode xmlFindChild ( xmlnode parent, string tagName, int index )</syntaxhighlight><br />
<br />
===Required Arguments===<br />
* '''parent''': This is an [[xmlnode]] that you want to find the child node under.<br />
* '''tagName''': This is the name of the child node you wish to find (case-sensitive).<br />
* '''index''': This is the 0-based index of the node you wish to find. For example, to find the 5th subnode with a particular name, you would use 4 as the index value. To find the first occurence, use 0.<br />
<br />
===Returns===<br />
Returns an [[xmlnode]] if the node was found, ''false'' otherwise.<br />
<br />
==Example==<br />
<section name="Server" class="server" show="true"><br />
If you wanted to find an ''instructions'' node in an xml file like this:<br />
<syntaxhighlight lang="xml"><br />
<root version="2.0"><br />
<options><br />
<instructions>Start at the beginning and keep going until the end!</instructions><br />
</options><br />
</root><br />
</syntaxhighlight><br />
<br />
You could use the following code to print the text in the ''instructions'' node to the chatbox:<br />
<syntaxhighlight lang="lua"><br />
local rootNode = xmlLoadFile ( "test.xml" )<br />
local optionsNode = xmlFindChild ( rootNode, "options", 0 )<br />
local instructionsNode = xmlFindChild ( optionsNode, "instructions", 0 )<br />
local instructions = xmlNodeGetValue ( instructionsNode )<br />
<br />
outputChatBox ( instructions )<br />
</syntaxhighlight><br />
</section><br />
<br />
==See Also==<br />
{{XML functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=XmlFindChild&diff=40353XmlFindChild2014-06-26T16:27:50Z<p>Ixjf: Added a little more fun to the example and fixed 'root' predefined variable being overwritten</p>
<hr />
<div>__NOTOC__<br />
{{Server client function}}<br />
This function returns a named child node of an XML node.<br />
<br />
==Syntax==<br />
<syntaxhighlight lang="lua">xmlnode xmlFindChild ( xmlnode parent, string tagName, int index )</syntaxhighlight><br />
<br />
===Required Arguments===<br />
* '''parent''': This is an [[xmlnode]] that you want to find the child node under.<br />
* '''tagName''': This is the name of the child node you wish to find (case-sensitive).<br />
* '''index''': This is the 0-based index of the node you wish to find. For example, to find the 5th subnode with a particular name, you would use 4 as the index value. To find the first occurence, use 0.<br />
<br />
===Returns===<br />
Returns an [[xmlnode]] if the node was found, ''false'' otherwise.<br />
<br />
==Example==<br />
<section name="Server" class="server" show="true"><br />
If you wanted to find an 'instructions' node in an xml file like this:<br />
<syntaxhighlight lang="xml"><br />
<root version="2.0"><br />
<options><br />
<instructions>Start at the beginning and keep going until the end!</instructions><br />
</options><br />
</root><br />
</syntaxhighlight><br />
<br />
You could use the following code to print the text in the ''instructions'' node to the chatbox:<br />
<syntaxhighlight lang="lua"><br />
local rootNode = xmlLoadFile ( "test.xml" )<br />
local optionsNode = xmlFindChild ( rootNode, "options", 0 )<br />
local instructionsNode = xmlFindChild ( optionsNode, "instructions", 0 )<br />
local instructions = xmlNodeGetValue ( instructionsNode )<br />
<br />
outputChatBox ( instructions )<br />
</syntaxhighlight><br />
</section><br />
<br />
==See Also==<br />
{{XML functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=XmlFindChild&diff=40352XmlFindChild2014-06-26T16:25:44Z<p>Ixjf: </p>
<hr />
<div>__NOTOC__<br />
{{Server client function}}<br />
This function returns a named child node of an XML node.<br />
<br />
==Syntax==<br />
<syntaxhighlight lang="lua">xmlnode xmlFindChild ( xmlnode parent, string tagName, int index )</syntaxhighlight><br />
<br />
===Required Arguments===<br />
* '''parent''': This is an [[xmlnode]] that you want to find the child node under.<br />
* '''tagName''': This is the name of the child node you wish to find (case-sensitive).<br />
* '''index''': This is the 0-based index of the node you wish to find. For example, to find the 5th subnode with a particular name, you would use 4 as the index value. To find the first occurence, use 0.<br />
<br />
===Returns===<br />
Returns an [[xmlnode]] if the node was found, ''false'' otherwise.<br />
<br />
==Example==<br />
<section name="Server" class="server" show="true"><br />
If you wanted to find an 'instructions' node in an xml file like this:<br />
<syntaxhighlight lang="xml"><br />
<root version="2.0"><br />
<options><br />
<instructions>Start at the beginning and keep going until the end!</instructions><br />
</options><br />
</root><br />
</syntaxhighlight><br />
<br />
You could use the following code:<br />
<syntaxhighlight lang="lua"><br />
local rootNode = xmlLoadFile ( "test.xml" )<br />
local optionsNode = xmlFindChild ( rootNode, "options", 0 )<br />
local instructionsNode = xmlFindChild ( optionsNode, "instructions", 0 )<br />
</syntaxhighlight><br />
</section><br />
<br />
==See Also==<br />
{{XML functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40055GetTeamWithFewestPlayers2014-06-21T23:35:48Z<p>Ixjf: Undo revision 40042 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
An useful function that gives you a table with the teams with less players. I've only put this here to replace a crappy function.<br />
==Syntax==<br />
<syntaxhighlight lang="lua">table getTeamWithFewestPlayers([table teams])</syntaxhighlight><br />
<br />
===Optional arguments===<br />
* '''teams''': a '''regular indexed''' table of teams. Example: {team, team, team, [4]=team}<br />
<br />
===Return===<br />
Returns a table containing [[team|teams]].<br />
If it fails (invalid table provided) or if there are no teams; returns false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamsWithFewestPlayers(t)<br />
if t and type(t)=="table" then -- Do checks for validity<br />
for i,v in ipairs(t) do<br />
if (not isElement(v)) or (type(v) ~= "team") then<br />
-- Use stacktrace from debug to output a message to parent function<br />
return false<br />
end<br />
end<br />
else t = getElementsByType("team") end<br />
local lowestScorers, lowestCount = {}, math.huge<br />
for i,v in ipairs(t) do<br />
local count = countPlayersInTeam(v)<br />
if count < lowestCount then<br />
lowestScorers = {v}<br />
lowestCount = count<br />
elseif count == lowestCount then<br />
table.insert(lowestScorers, v)<br />
end<br />
end<br />
return lowestScorers<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
This example adds a "/lowest" to tell the player which team has the lowest amount of players.<br />
<syntaxhighlight lang="lua"><br />
-- Create a local function for the command "/lowest"<br />
function lowestTeam(player)<br />
local teams = getTeamsWithFewestPlayers()<br />
local multiple = #teams > 1 -- is there more than one team?<br />
local teamListStr = ""<br />
for i,v in ipairs(teams) do<br />
local name = getTeamName(v)<br />
teamListStr = teamListStr .. " " .. name<br />
if (" "..teamListStr ~= name) and (i ~= #teams) then<br />
teamListStr = teamListStr .. " and"<br />
end<br />
end<br />
local str = string.format("The %s with the fewest players %s %s!", multiple and "teams" or "team", multiple and "are" or "is", teamListStr)<br />
outputChatBox(str, player)<br />
end<br />
addCommandHandler("lowest", lowestTeam)<br />
</syntaxhighlight><br />
</section><br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40054GetTeamWithFewestPlayers2014-06-21T23:35:33Z<p>Ixjf: Undo revision 40043 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
<br />
__NOTOC__<br />
This function compares number of players in two teams specifed and returns the team with less players.<br />
'''Note:''' If both teams have the same amount of players, then the first one will be returned!<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1,element team2)</syntaxhighlight><br />
<br />
<br />
===Return===<br />
Returns a [[team]] team with less players, ''false'' if invalid arguments were specified.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamWithFewestPlayers(team1,team2)<br />
if (isElement(team1) and getElementType(team1) == "team") and (isElement(team2) and getElementType(team2) == "team") then<br />
local team1C = countPlayersInTeam (team1)<br />
local team2C = countPlayersInTeam (team2)<br />
if team1C == team2C then return team1<br />
else<br />
if team1C == math.min(team1C,team2C) then<br />
return team1<br />
else<br />
return team2<br />
end<br />
end<br />
end<br />
return false<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
<br />
<br />
function setPlayerToTeam(source)<br />
local redteam = getTeamFromName ( "Red" )<br />
local blueteam = getTeamFromName ( "Blue" )<br />
if redteam and blueteam then<br />
local theteam = getTeamWithFewestPlayers(redteam,blueteam)<br />
if theteam then<br />
setPlayerTeam(source,theteam)<br />
local PlayerName = getPlayerName ( source )<br />
outputChatBox ( " " .. joinedPlayerName .. " Joined "..getTeamName(theteam).." team !" , root, 255, 255, 255 )<br />
end<br />
else<br />
createTeam ("Red",255,0,0)<br />
createTeam ("Blue",0,0,255)<br />
setPlayerToTeam(source)<br />
end<br />
end<br />
addEventHandler ( "onPlayerJoin", getRootElement(), setPlayerToTeam )<br />
<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: Booo<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40053GetTeamWithFewestPlayers2014-06-21T23:35:16Z<p>Ixjf: Undo revision 40044 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
{{Needs_Checking|This page needs to be locked or lxjf needs to be banned}}<br />
__NOTOC__<br />
This function compares number of players in two teams specifed and returns the team with less players.<br />
'''Note:''' If both teams have the same amount of players, then the first one will be returned!<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1,element team2)</syntaxhighlight><br />
<br />
<br />
===Return===<br />
Returns a [[team]] team with less players, ''false'' if invalid arguments were specified.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamWithFewestPlayers(team1,team2)<br />
if (isElement(team1) and getElementType(team1) == "team") and (isElement(team2) and getElementType(team2) == "team") then<br />
local team1C = countPlayersInTeam (team1)<br />
local team2C = countPlayersInTeam (team2)<br />
if team1C == team2C then return team1<br />
else<br />
if team1C == math.min(team1C,team2C) then<br />
return team1<br />
else<br />
return team2<br />
end<br />
end<br />
end<br />
return false<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
<br />
<br />
function setPlayerToTeam(source)<br />
local redteam = getTeamFromName ( "Red" )<br />
local blueteam = getTeamFromName ( "Blue" )<br />
if redteam and blueteam then<br />
local theteam = getTeamWithFewestPlayers(redteam,blueteam)<br />
if theteam then<br />
setPlayerTeam(source,theteam)<br />
local PlayerName = getPlayerName ( source )<br />
outputChatBox ( " " .. joinedPlayerName .. " Joined "..getTeamName(theteam).." team !" , root, 255, 255, 255 )<br />
end<br />
else<br />
createTeam ("Red",255,0,0)<br />
createTeam ("Blue",0,0,255)<br />
setPlayerToTeam(source)<br />
end<br />
end<br />
addEventHandler ( "onPlayerJoin", getRootElement(), setPlayerToTeam )<br />
<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: Booo<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40052GetTeamWithFewestPlayers2014-06-21T23:35:00Z<p>Ixjf: Undo revision 40045 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
{{Needs_Checking|This page needs to be locked or xjf needs to be banned }}<br />
__NOTOC__<br />
This function compares number of players in two teams specifed and returns the team with less players.<br />
'''Note:''' If both teams have the same amount of players, then the first one will be returned!<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1,element team2)</syntaxhighlight><br />
<br />
<br />
===Return===<br />
Returns a [[team]] team with less players, ''false'' if invalid arguments were specified.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamWithFewestPlayers(team1,team2)<br />
if (isElement(team1) and getElementType(team1) == "team") and (isElement(team2) and getElementType(team2) == "team") then<br />
local team1C = countPlayersInTeam (team1)<br />
local team2C = countPlayersInTeam (team2)<br />
if team1C == team2C then return team1<br />
else<br />
if team1C == math.min(team1C,team2C) then<br />
return team1<br />
else<br />
return team2<br />
end<br />
end<br />
end<br />
return false<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
<br />
<br />
function setPlayerToTeam(source)<br />
local redteam = getTeamFromName ( "Red" )<br />
local blueteam = getTeamFromName ( "Blue" )<br />
if redteam and blueteam then<br />
local theteam = getTeamWithFewestPlayers(redteam,blueteam)<br />
if theteam then<br />
setPlayerTeam(source,theteam)<br />
local PlayerName = getPlayerName ( source )<br />
outputChatBox ( " " .. joinedPlayerName .. " Joined "..getTeamName(theteam).." team !" , root, 255, 255, 255 )<br />
end<br />
else<br />
createTeam ("Red",255,0,0)<br />
createTeam ("Blue",0,0,255)<br />
setPlayerToTeam(source)<br />
end<br />
end<br />
addEventHandler ( "onPlayerJoin", getRootElement(), setPlayerToTeam )<br />
<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: Booo<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40051GetTeamWithFewestPlayers2014-06-21T23:28:49Z<p>Ixjf: Undo revision 40048 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
{{Needs_Checking|This page needs to be locked or lxjf needs to be banned }}<br />
__NOTOC__<br />
This function compares number of players in two teams specifed and returns the team with less players.<br />
'''Note:''' If both teams have the same amount of players, then the first one will be returned!<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1,element team2)</syntaxhighlight><br />
<br />
<br />
===Return===<br />
Returns a [[team]] team with less players, ''false'' if invalid arguments were specified.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamWithFewestPlayers(team1,team2)<br />
if (isElement(team1) and getElementType(team1) == "team") and (isElement(team2) and getElementType(team2) == "team") then<br />
local team1C = countPlayersInTeam (team1)<br />
local team2C = countPlayersInTeam (team2)<br />
if team1C == team2C then return team1<br />
else<br />
if team1C == math.min(team1C,team2C) then<br />
return team1<br />
else<br />
return team2<br />
end<br />
end<br />
end<br />
return false<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
<br />
<br />
function setPlayerToTeam(source)<br />
local redteam = getTeamFromName ( "Red" )<br />
local blueteam = getTeamFromName ( "Blue" )<br />
if redteam and blueteam then<br />
local theteam = getTeamWithFewestPlayers(redteam,blueteam)<br />
if theteam then<br />
setPlayerTeam(source,theteam)<br />
local PlayerName = getPlayerName ( source )<br />
outputChatBox ( " " .. joinedPlayerName .. " Joined "..getTeamName(theteam).." team !" , root, 255, 255, 255 )<br />
end<br />
else<br />
createTeam ("Red",255,0,0)<br />
createTeam ("Blue",0,0,255)<br />
setPlayerToTeam(source)<br />
end<br />
end<br />
addEventHandler ( "onPlayerJoin", getRootElement(), setPlayerToTeam )<br />
<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: Booo<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40050GetTeamWithFewestPlayers2014-06-21T23:28:36Z<p>Ixjf: Undo revision 40049 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
{{Needs_Checking|This page needs to be locked or lxjf needs to be banned. }}<br />
__NOTOC__<br />
This function compares number of players in two teams specifed and returns the team with less players.<br />
'''Note:''' If both teams have the same amount of players, then the first one will be returned!<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1,element team2)</syntaxhighlight><br />
<br />
<br />
===Return===<br />
Returns a [[team]] team with less players, ''false'' if invalid arguments were specified.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamWithFewestPlayers(team1,team2)<br />
if (isElement(team1) and getElementType(team1) == "team") and (isElement(team2) and getElementType(team2) == "team") then<br />
local team1C = countPlayersInTeam (team1)<br />
local team2C = countPlayersInTeam (team2)<br />
if team1C == team2C then return team1<br />
else<br />
if team1C == math.min(team1C,team2C) then<br />
return team1<br />
else<br />
return team2<br />
end<br />
end<br />
end<br />
return false<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
<br />
<br />
function setPlayerToTeam(source)<br />
local redteam = getTeamFromName ( "Red" )<br />
local blueteam = getTeamFromName ( "Blue" )<br />
if redteam and blueteam then<br />
local theteam = getTeamWithFewestPlayers(redteam,blueteam)<br />
if theteam then<br />
setPlayerTeam(source,theteam)<br />
local PlayerName = getPlayerName ( source )<br />
outputChatBox ( " " .. joinedPlayerName .. " Joined "..getTeamName(theteam).." team !" , root, 255, 255, 255 )<br />
end<br />
else<br />
createTeam ("Red",255,0,0)<br />
createTeam ("Blue",0,0,255)<br />
setPlayerToTeam(source)<br />
end<br />
end<br />
addEventHandler ( "onPlayerJoin", getRootElement(), setPlayerToTeam )<br />
<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: Booo<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40040GetTeamWithFewestPlayers2014-06-21T22:57:02Z<p>Ixjf: Undo revision 40039 by Qais (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
An useful function that gives you a table with the teams with less players. I've only put this here to replace a crappy function.<br />
==Syntax==<br />
<syntaxhighlight lang="lua">table getTeamWithFewestPlayers([table teams])</syntaxhighlight><br />
<br />
===Optional arguments===<br />
* '''teams''': a '''regular indexed''' table of teams. Example: {team, team, team, [4]=team}<br />
<br />
===Return===<br />
Returns a table containing [[team|teams]].<br />
If it fails (invalid table provided) or if there are no teams; returns false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamsWithFewestPlayers(t)<br />
if t and type(t)=="table" then -- Do checks for validity<br />
for i,v in ipairs(t) do<br />
if (not isElement(v)) or (type(v) ~= "team") then<br />
-- Use stacktrace from debug to output a message to parent function<br />
return false<br />
end<br />
end<br />
else t = getElementsByType("team") end<br />
local lowestScorers, lowestCount = {}, math.huge<br />
for i,v in ipairs(t) do<br />
local count = countPlayersInTeam(v)<br />
if count < lowestCount then<br />
lowestScorers = {v}<br />
lowestCount = count<br />
elseif count == lowestCount then<br />
table.insert(lowestScorers, v)<br />
end<br />
end<br />
return lowestScorers<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
This example adds a "/lowest" to tell the player which team has the lowest amount of players.<br />
<syntaxhighlight lang="lua"><br />
-- Create a local function for the command "/lowest"<br />
function lowestTeam(player)<br />
local teams = getTeamsWithFewestPlayers()<br />
local multiple = #teams > 1 -- is there more than one team?<br />
local teamListStr = ""<br />
for i,v in ipairs(teams) do<br />
local name = getTeamName(v)<br />
teamListStr = teamListStr .. " " .. name<br />
if (" "..teamListStr ~= name) and (i ~= #teams) then<br />
teamListStr = teamListStr .. " and"<br />
end<br />
end<br />
local str = string.format("The %s with the fewest players %s %s!", multiple and "teams" or "team", multiple and "are" or "is", teamListStr)<br />
outputChatBox(str, player)<br />
end<br />
addCommandHandler("lowest", lowestTeam)<br />
</syntaxhighlight><br />
</section><br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40038GetTeamWithFewestPlayers2014-06-21T22:00:50Z<p>Ixjf: Undo revision 40035 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
An useful function that gives you a table with the teams with less players. I've only put this here to replace a crappy function.<br />
==Syntax==<br />
<syntaxhighlight lang="lua">table getTeamWithFewestPlayers([table teams])</syntaxhighlight><br />
<br />
===Optional arguments===<br />
* '''teams''': a '''regular indexed''' table of teams. Example: {team, team, team, [4]=team}<br />
<br />
===Return===<br />
Returns a table containing [[team|teams]].<br />
If it fails (invalid table provided) or if there are no teams; returns false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamsWithFewestPlayers(t)<br />
if t and type(t)=="table" then -- Do checks for validity<br />
for i,v in ipairs(t) do<br />
if (not isElement(v)) or (type(v) ~= "team") then<br />
-- Use stacktrace from debug to output a message to parent function<br />
return false<br />
end<br />
end<br />
else t = getElementsByType("team") end<br />
local lowestScorers, lowestCount = {}, math.huge<br />
for i,v in ipairs(t) do<br />
local count = countPlayersInTeam(v)<br />
if count < lowestCount then<br />
lowestScorers = {v}<br />
lowestCount = count<br />
elseif count == lowestCount then<br />
table.insert(lowestScorers, v)<br />
end<br />
end<br />
return lowestScorers<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
This example adds a "/lowest" to tell the player which team has the lowest amount of players.<br />
<syntaxhighlight lang="lua"><br />
-- Create a local function for the command "/lowest"<br />
function lowestTeam(player)<br />
local teams = getTeamsWithFewestPlayers()<br />
local multiple = #teams > 1 -- is there more than one team?<br />
local teamListStr = ""<br />
for i,v in ipairs(teams) do<br />
local name = getTeamName(v)<br />
teamListStr = teamListStr .. " " .. name<br />
if (" "..teamListStr ~= name) and (i ~= #teams) then<br />
teamListStr = teamListStr .. " and"<br />
end<br />
end<br />
local str = string.format("The %s with the fewest players %s %s!", multiple and "teams" or "team", multiple and "are" or "is", teamListStr)<br />
outputChatBox(str, player)<br />
end<br />
addCommandHandler("lowest", lowestTeam)<br />
</syntaxhighlight><br />
</section><br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40037GetTeamWithFewestPlayers2014-06-21T22:00:28Z<p>Ixjf: Undo revision 40036 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
<br />
__NOTOC__<br />
This function gets a team from it's few players.<br />
'''Note:''' If 2 teams has the same count, then the first one found will be returned!<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1,element team2)</syntaxhighlight><br />
<br />
<br />
===Return===<br />
Returns a [[team]] else false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamWithFewestPlayers(team1,team2)<br />
if (isElement(team1) and getElementType(team1) == "team") and (isElement(team2) and getElementType(team2) == "team") then<br />
local team1C = countPlayersInTeam (team1)<br />
local team2C = countPlayersInTeam (team2)<br />
if team1C == team2C then return team1<br />
else<br />
if team1C == math.min(team1C,team2C) then<br />
return team1<br />
else<br />
return team2<br />
end<br />
end<br />
end<br />
return false<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
<br />
<br />
function setPlayerToTeam(source)<br />
local redteam = getTeamFromName ( "Red" )<br />
local blueteam = getTeamFromName ( "Blue" )<br />
if redteam and blueteam then<br />
local theteam = getTeamWithFewestPlayers(redteam,blueteam)<br />
if theteam then<br />
setPlayerTeam(source,theteam)<br />
local PlayerName = getPlayerName ( source )<br />
outputChatBox ( " " .. joinedPlayerName .. " Joined "..getTeamName(theteam).." team !" , root, 255, 255, 255 )<br />
end<br />
else<br />
createTeam ("Red",255,0,0)<br />
createTeam ("Blue",0,0,255)<br />
setPlayerToTeam(source)<br />
end<br />
end<br />
addEventHandler ( "onPlayerJoin", getRootElement(), setPlayerToTeam )<br />
<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: Booo<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40032GetTeamWithFewestPlayers2014-06-21T19:14:02Z<p>Ixjf: </p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
An useful function that gives you a table with the teams with less players. I've only put this here to replace a crappy function.<br />
==Syntax==<br />
<syntaxhighlight lang="lua">table getTeamWithFewestPlayers([table teams])</syntaxhighlight><br />
<br />
===Optional arguments===<br />
* '''teams''': a '''regular indexed''' table of teams. Example: {team, team, team, [4]=team}<br />
<br />
===Return===<br />
Returns a table containing [[team|teams]].<br />
If it fails (invalid table provided) or if there are no teams; returns false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamsWithFewestPlayers(t)<br />
if t and type(t)=="table" then -- Do checks for validity<br />
for i,v in ipairs(t) do<br />
if (not isElement(v)) or (type(v) ~= "team") then<br />
-- Use stacktrace from debug to output a message to parent function<br />
return false<br />
end<br />
end<br />
else t = getElementsByType("team") end<br />
local lowestScorers, lowestCount = {}, math.huge<br />
for i,v in ipairs(t) do<br />
local count = countPlayersInTeam(v)<br />
if count < lowestCount then<br />
lowestScorers = {v}<br />
lowestCount = count<br />
elseif count == lowestCount then<br />
table.insert(lowestScorers, v)<br />
end<br />
end<br />
return lowestScorers<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
This example adds a "/lowest" to tell the player which team has the lowest amount of players.<br />
<syntaxhighlight lang="lua"><br />
-- Create a local function for the command "/lowest"<br />
function lowestTeam(player)<br />
local teams = getTeamsWithFewestPlayers()<br />
local multiple = #teams > 1 -- is there more than one team?<br />
local teamListStr = ""<br />
for i,v in ipairs(teams) do<br />
local name = getTeamName(v)<br />
teamListStr = teamListStr .. " " .. name<br />
if (" "..teamListStr ~= name) and (i ~= #teams) then<br />
teamListStr = teamListStr .. " and"<br />
end<br />
end<br />
local str = string.format("The %s with the fewest players %s %s!", multiple and "teams" or "team", multiple and "are" or "is", teamListStr)<br />
outputChatBox(str, player)<br />
end<br />
addCommandHandler("lowest", lowestTeam)<br />
</syntaxhighlight><br />
</section><br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40031GetTeamWithFewestPlayers2014-06-21T19:12:41Z<p>Ixjf: Undo revision 40026 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
An useful function that gives you a table with the teams with less players. I've only put this here to replace a crappy function.<br />
==Syntax==<br />
<syntaxhighlight lang="lua">table getTeamWithFewestPlayers([table teams])</syntaxhighlight><br />
<br />
===Optional arguments===<br />
* '''teams''': a '''regular indexed''' table of teams. Example: {team, team, team, [4]=team}<br />
<br />
===Return===<br />
Returns a table containing [[team|teams]].<br />
If it fails (invalid table provided) or if there are no teams; returns false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamsWithFewestPlayers(t)<br />
if t and type(t)=="table" then -- Do checks for validity<br />
for i,v in ipairs(t) do<br />
if (not isElement(v)) or (type(v) ~= "team") then<br />
-- Use stacktrace from debug to output a message to parent function<br />
return false<br />
end<br />
end<br />
else t = getElementsByType("team") end<br />
local lowestScorers, lowestCount = {}, math.huge<br />
for i,v in ipairs(t) do<br />
local count = countPlayersInTeam(v)<br />
if count < lowestCount then<br />
lowestScorers = {v}<br />
lowestCount = count<br />
elseif count == lowestCount then<br />
table.insert(lowestScorers, v)<br />
end<br />
end<br />
return lowestScorers<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
This example adds a "/lowest" to tell the player which team has the lowest amount of players.<br />
<syntaxhighlight lang="lua"><br />
-- Create a local function for the command "/lowest"<br />
function lowestTeam(player)<br />
local teams = getTeamsWithFewestPlayers()<br />
local multiple = #teams > 1 -- is there more than one team?<br />
local teamListStr = ""<br />
for i,v in ipairs(teams) do<br />
local name = getTeamName(v)<br />
teamListStr = teamListStr .. " " .. name<br />
if (" "..teamListStr ~= name) and (i ~= #teams) then<br />
teamListStr = teamListStr .. " and"<br />
end<br />
end<br />
local str = string.format("The %s with the fewest players %s %s!", multiple and "teams" or "team", multiple and "are" or "is", teamListStr)<br />
outputChatBox(str, player)<br />
end<br />
addCommandHandler("lowest", lowestTeam)<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: qaisjp<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40030GetTeamWithFewestPlayers2014-06-21T19:10:15Z<p>Ixjf: Undo revision 40027 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
<br />
__NOTOC__<br />
This function gets a team from it's few players.<br />
'''Note:''' If 2 teams has the same count, then the first one found will be returned!<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1,element team2)</syntaxhighlight><br />
<br />
<br />
===Return===<br />
Returns a [[team]] else false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamWithFewestPlayers(team1,team2)<br />
if (isElement(team1) and getElementType(team1) == "team") and (isElement(team2) and getElementType(team2) == "team") then<br />
local team1C = countPlayersInTeam (team1)<br />
local team2C = countPlayersInTeam (team2)<br />
if team1C == team2C then return team1<br />
else<br />
if team1C == math.min(team1C,team2C) then<br />
return team1<br />
else<br />
return team2<br />
end<br />
end<br />
end<br />
return false<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
<br />
<br />
function setPlayerToTeam(source)<br />
local redteam = getTeamFromName ( "Red" )<br />
local blueteam = getTeamFromName ( "Blue" )<br />
if redteam and blueteam then<br />
local theteam = getTeamWithFewestPlayers(redteam,blueteam)<br />
if theteam then<br />
setPlayerTeam(source,theteam)<br />
local PlayerName = getPlayerName ( source )<br />
outputChatBox ( " " .. joinedPlayerName .. " Joined "..getTeamName(theteam).." team !" , root, 255, 255, 255 )<br />
end<br />
else<br />
createTeam ("Red",255,0,0)<br />
createTeam ("Blue",0,0,255)<br />
setPlayerToTeam(source)<br />
end<br />
end<br />
addEventHandler ( "onPlayerJoin", getRootElement(), setPlayerToTeam )<br />
<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: Booo<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40029GetTeamWithFewestPlayers2014-06-21T19:09:59Z<p>Ixjf: Undo revision 40028 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
<br />
__NOTOC__<br />
This function gets a team from it's few players.<br />
'''Note:''' If 2 teams has the same count, then the first one found will be returned!<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1,element team2)</syntaxhighlight><br />
<br />
<br />
===Return===<br />
Returns a [[team]] else false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
[lua]<br />
function getTeamWithFewestPlayers(team1,team2)<br />
if (isElement(team1) and getElementType(team1) == "team") and (isElement(team2) and getElementType(team2) == "team") then<br />
local team1C = countPlayersInTeam (team1)<br />
local team2C = countPlayersInTeam (team2)<br />
if team1C == team2C then return team1<br />
else<br />
if team1C == math.min(team1C,team2C) then<br />
return team1<br />
else<br />
return team2<br />
end<br />
end<br />
end<br />
return false<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
<br />
<br />
function setPlayerToTeam(source)<br />
local redteam = getTeamFromName ( "Red" )<br />
local blueteam = getTeamFromName ( "Blue" )<br />
if redteam and blueteam then<br />
local theteam = getTeamWithFewestPlayers(redteam,blueteam)<br />
if theteam then<br />
setPlayerTeam(source,theteam)<br />
local PlayerName = getPlayerName ( source )<br />
outputChatBox ( " " .. joinedPlayerName .. " Joined "..getTeamName(theteam).." team !" , root, 255, 255, 255 )<br />
end<br />
else<br />
createTeam ("Red",255,0,0)<br />
createTeam ("Blue",0,0,255)<br />
setPlayerToTeam(source)<br />
end<br />
end<br />
addEventHandler ( "onPlayerJoin", getRootElement(), setPlayerToTeam )<br />
<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: Booo<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetLocalization&diff=40025GetLocalization2014-06-21T18:05:50Z<p>Ixjf: </p>
<hr />
<div>{{Client function}}<br />
__NOTOC__<br />
{{New items|4.0132|1.4|<br />
This function gets the player's localization setting as set in the MTA client.<br />
}}<br />
==Syntax== <br />
<syntaxhighlight lang="lua"><br />
table getLocalization ( )<br />
</syntaxhighlight> <br />
<br />
===Returns===<br />
Returns a [[table]] with the following entries:<br />
*'''code :''' The language code ''(eg. "en_US" for "English (United States)" or "ar" for "Arabic")''.<br />
*'''name :''' The name of the language ''(eg. "English (United States)" or "Arabic")''.<br />
<br />
==Example==<br />
This example outputs simple ''Welcome'' message at the resource start (also when player joins the game if the resource is already running).<br />
<syntaxhighlight lang="lua"><br />
local msg = {cs = "Vítejte", fr = "Accueil", de = "Willkommen", pl = "Powitanie"}<br />
<br />
addEventHandler("onClientResourceStart", resourceRoot, <br />
function ()<br />
local languageCode = getLocalization()["code"]<br />
if msg[languageCode] then --Check if the message is avaible in client's language<br />
outputChatBox(msg[languageCode] .. "!") --Output it<br />
else<br />
outputChatBox("Welcome!") --Output English for any other language<br />
end<br />
end)<br />
</syntaxhighlight><br />
<br />
==See Also==<br />
{{Client_utility_functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40004GetTeamWithFewestPlayers2014-06-21T14:05:20Z<p>Ixjf: I'll seriously request deletion of this page if you revert this back</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
An useful function that gives you a table with the teams with less players. I've only put this here to replace a crappy function.<br />
==Syntax==<br />
<syntaxhighlight lang="lua">table getTeamWithFewestPlayers([table teams])</syntaxhighlight><br />
<br />
===Optional arguments===<br />
* '''teams''': a '''regular indexed''' table of teams. Example: {team, team, team, [4]=team}<br />
<br />
===Return===<br />
Returns a table containing [[team|teams]].<br />
If it fails (invalid table provided) or if there are no teams; returns false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamsWithFewestPlayers(t)<br />
if t and type(t)=="table" then -- Do checks for validity<br />
for i,v in ipairs(t) do<br />
if (not isElement(v)) or (type(v) ~= "team") then<br />
-- Use stacktrace from debug to output a message to parent function<br />
return false<br />
end<br />
end<br />
else t = getElementsByType("team") end<br />
local lowestScorers, lowestCount = {}, math.huge<br />
for i,v in ipairs(t) do<br />
local count = countPlayersInTeam(v)<br />
if count < lowestCount then<br />
lowestScorers = {v}<br />
lowestCount = count<br />
elseif count == lowestCount then<br />
table.insert(lowestScorers, v)<br />
end<br />
end<br />
return lowestScorers<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
This example adds a "/lowest" to tell the player which team has the lowest amount of players.<br />
<syntaxhighlight lang="lua"><br />
-- Create a local function for the command "/lowest"<br />
function lowestTeam(player)<br />
local teams = getTeamsWithFewestPlayers()<br />
local multiple = #teams > 1 -- is there more than one team?<br />
local teamListStr = ""<br />
for i,v in ipairs(teams) do<br />
local name = getTeamName(v)<br />
teamListStr = teamListStr .. " " .. name<br />
if (" "..teamListStr ~= name) and (i ~= #teams) then<br />
teamListStr = teamListStr .. " and"<br />
end<br />
end<br />
local str = string.format("The %s with the fewest players %s %s!", multiple and "teams" or "team", multiple and "are" or "is", teamListStr)<br />
outputChatBox(str, player)<br />
end<br />
addCommandHandler("lowest", lowestTeam)<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: qaisjp<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40003GetTeamWithFewestPlayers2014-06-21T14:02:39Z<p>Ixjf: Undo revision 40000 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
A potentially useful function to give you a table containing the fewest players. I've only put this here to replace a crappy function.<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers([table teams])</syntaxhighlight><br />
<br />
===Optional arguments===<br />
* '''teams''': a '''regular indexed''' table of teams. Example: {team, team, team, [4]=team}<br />
<br />
===Return===<br />
Returns a table containing [[team|teams]].<br />
If it fails (invalid table provided) or if there are no teams; returns false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamsWithFewestPlayers(t)<br />
if t and type(t)=="table" then -- Do checks for validity<br />
for i,v in ipairs(t) do<br />
if (not isElement(v)) or (type(v) ~= "team") then<br />
-- Use stacktrace from debug to output a message to parent function<br />
return false<br />
end<br />
end<br />
else t = getElementsByType("team") end<br />
local lowestScorers, lowestCount = {}, math.huge<br />
for i,v in ipairs(t) do<br />
local count = countPlayersInTeam(v)<br />
if count < lowestCount then<br />
lowestScorers = {v}<br />
lowestCount = count<br />
elseif count == lowestCount then<br />
table.insert(lowestScorers, v)<br />
end<br />
end<br />
return lowestScorers<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
This example adds a "/lowest" to tell the player which team has the lowest amount of players.<br />
<syntaxhighlight lang="lua"><br />
-- Create a local function for the command "/lowest"<br />
function lowestTeam(player)<br />
local teams = getTeamsWithFewestPlayers()<br />
local multiple = #teams > 1 -- is there more than one team?<br />
local teamListStr = ""<br />
for i,v in ipairs(teams) do<br />
local name = getTeamName(v)<br />
teamListStr = teamListStr .. " " .. name<br />
if (" "..teamListStr ~= name) and (i ~= #teams) then<br />
teamListStr = teamListStr .. " and"<br />
end<br />
end<br />
local str = string.format("The %s with the fewest players %s %s!", multiple and "teams" or "team", multiple and "are" or "is", teamListStr)<br />
outputChatBox(str, player)<br />
end<br />
addCommandHandler("lowest", lowestTeam)<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: qaisjp<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=40002GetTeamWithFewestPlayers2014-06-21T14:02:11Z<p>Ixjf: Undo revision 40001 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
<br />
__NOTOC__<br />
This function gets a team from it's few players.<br />
'''Note:''' If 2 teams has the same count, then the first one found will be returned!<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1,element team2)</syntaxhighlight><br />
<br />
<br />
===Return===<br />
Returns a [[team]] else false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamWithFewestPlayers(team1,team2)<br />
if (isElement(team1) and getElementType(team1) == "team") and (isElement(team2) and getElementType(team2) == "team") then<br />
local team1C = countPlayersInTeam (team1)<br />
local team2C = countPlayersInTeam (team2)<br />
if team1C == team2C then return team1<br />
else<br />
if team1C == math.min(team1C,team2C) then<br />
return team1<br />
else<br />
return team2<br />
end<br />
end<br />
end<br />
return false<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
<br />
<br />
function setPlayerToTeam(source)<br />
local redteam = getTeamFromName ( "Red" )<br />
local blueteam = getTeamFromName ( "Blue" )<br />
if redteam and blueteam then<br />
local theteam = getTeamWithFewestPlayers(redteam,blueteam)<br />
if theteam then<br />
setPlayerTeam(source,theteam)<br />
local PlayerName = getPlayerName ( source )<br />
outputChatBox ( " " .. joinedPlayerName .. " Joined "..getTeamName(theteam).." team !" , root, 255, 255, 255 )<br />
end<br />
else<br />
createTeam ("Red",255,0,0)<br />
createTeam ("Blue",0,0,255)<br />
setPlayerToTeam(source)<br />
end<br />
end<br />
addEventHandler ( "onPlayerJoin", getRootElement(), setPlayerToTeam )<br />
<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: Booo<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=39992GetTeamWithFewestPlayers2014-06-20T15:51:29Z<p>Ixjf: Okay</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
A potentially useful function to give you a table containing the fewest players. I've only put this here to replace a crappy function.<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers([table teams])</syntaxhighlight><br />
<br />
===Optional arguments===<br />
* '''teams''': a '''regular indexed''' table of teams. Example: {team, team, team, [4]=team}<br />
<br />
===Return===<br />
Returns a table containing [[team|teams]].<br />
If it fails (invalid table provided) or if there are no teams; returns false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamsWithFewestPlayers(t)<br />
if t and type(t)=="table" then -- Do checks for validity<br />
for i,v in ipairs(t) do<br />
if (not isElement(v)) or (type(v) ~= "team") then<br />
-- Use stacktrace from debug to output a message to parent function<br />
return false<br />
end<br />
end<br />
else t = getElementsByType("team") end<br />
local lowestScorers, lowestCount = {}, math.huge<br />
for i,v in ipairs(t) do<br />
local count = countPlayersInTeam(v)<br />
if count < lowestCount then<br />
lowestScorers = {v}<br />
lowestCount = count<br />
elseif count == lowestCount then<br />
table.insert(lowestScorers, v)<br />
end<br />
end<br />
return lowestScorers<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
This example adds a "/lowest" to tell the player which team has the lowest amount of players.<br />
<syntaxhighlight lang="lua"><br />
-- Create a local function for the command "/lowest"<br />
function lowestTeam(player)<br />
local teams = getTeamsWithFewestPlayers()<br />
local multiple = #teams > 1 -- is there more than one team?<br />
local teamListStr = ""<br />
for i,v in ipairs(teams) do<br />
local name = getTeamName(v)<br />
teamListStr = teamListStr .. " " .. name<br />
if (" "..teamListStr ~= name) and (i ~= #teams) then<br />
teamListStr = teamListStr .. " and"<br />
end<br />
end<br />
local str = string.format("The %s with the fewest players %s %s!", multiple and "teams" or "team", multiple and "are" or "is", teamListStr)<br />
outputChatBox(str, player)<br />
end<br />
addCommandHandler("lowest", lowestTeam)<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: qaisjp<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=39989GetTeamWithFewestPlayers2014-06-20T15:46:53Z<p>Ixjf: Undo revision 39986 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
This function gets a team from it's few players.<br />
'''Note:''' If 2 teams has the same count, then the first one found will be returned!<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1,element team2)</syntaxhighlight><br />
<br />
<br />
===Return===<br />
Returns a [[team]] else false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamWithFewestPlayers(team1,team2)<br />
if (isElement(team1) and getElementType(team1) == "team") and (isElement(team2) and getElementType(team2) == "team") then<br />
local team1C = countPlayersInTeam (team1)<br />
local team2C = countPlayersInTeam (team2)<br />
if team1C == team2C then return team1<br />
else<br />
if team1C == math.min(team1C,team2C) then<br />
return team1<br />
else<br />
return team2<br />
end<br />
end<br />
end<br />
return false<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
<br />
<br />
function setPlayerToTeam(source)<br />
local redteam = getTeamFromName ( "Red" )<br />
local blueteam = getTeamFromName ( "Blue" )<br />
if redteam and blueteam then<br />
local theteam = getTeamWithFewestPlayers(redteam,blueteam)<br />
if theteam then<br />
setPlayerTeam(source,theteam)<br />
local PlayerName = getPlayerName ( source )<br />
outputChatBox ( " " .. joinedPlayerName .. " Joined "..getTeamName(theteam).." team !" , root, 255, 255, 255 )<br />
end<br />
else<br />
createTeam ("Red",255,0,0)<br />
createTeam ("Blue",0,0,255)<br />
setPlayerToTeam(source)<br />
end<br />
end<br />
addEventHandler ( "onPlayerJoin", getRootElement(), setPlayerToTeam )<br />
<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: Booo<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=39988GetTeamWithFewestPlayers2014-06-20T15:46:33Z<p>Ixjf: Undo revision 39987 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
<br />
<div style="background: #FFCFCF; padding: 5px; font-weight:bold; border: 1px dotted #AAAAAA;padding:10px;margin:10px;"><center><br />
<font color="#0066cc" size="5">stop edit my Code Nob , and go create your page !</font><br />
</center></div><br />
<br />
<br />
__NOTOC__<br />
This function gets a team from it's few players.<br />
'''Note:''' If 2 teams has the same count, then the first one found will be returned!<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1,element team2)</syntaxhighlight><br />
<br />
<br />
===Return===<br />
Returns a [[team]] else false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamWithFewestPlayers(team1,team2)<br />
if (isElement(team1) and getElementType(team1) == "team") and (isElement(team2) and getElementType(team2) == "team") then<br />
local team1C = countPlayersInTeam (team1)<br />
local team2C = countPlayersInTeam (team2)<br />
if team1C == team2C then return team1<br />
else<br />
if team1C == math.min(team1C,team2C) then<br />
return team1<br />
else<br />
return team2<br />
end<br />
end<br />
end<br />
return false<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
<br />
<br />
function setPlayerToTeam(source)<br />
local redteam = getTeamFromName ( "Red" )<br />
local blueteam = getTeamFromName ( "Blue" )<br />
if redteam and blueteam then<br />
local theteam = getTeamWithFewestPlayers(redteam,blueteam)<br />
if theteam then<br />
setPlayerTeam(source,theteam)<br />
local PlayerName = getPlayerName ( source )<br />
outputChatBox ( " " .. joinedPlayerName .. " Joined "..getTeamName(theteam).." team !" , root, 255, 255, 255 )<br />
end<br />
else<br />
createTeam ("Red",255,0,0)<br />
createTeam ("Blue",0,0,255)<br />
setPlayerToTeam(source)<br />
end<br />
end<br />
addEventHandler ( "onPlayerJoin", getRootElement(), setPlayerToTeam )<br />
<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: Booo<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=39959GetTeamWithFewestPlayers2014-06-18T18:08:34Z<p>Ixjf: Undo revision 39955 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
A potentially useful function to give you a table containing the fewest players. I've only put this here to replace a crappy function.<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers([table teams])</syntaxhighlight><br />
<br />
===Optional arguments===<br />
* '''teams''': a '''regular indexed''' table of teams. Example: {team, team, team, [4]=team}<br />
<br />
===Return===<br />
Returns a table containing [[team|teams]].<br />
If it fails (invalid table provided) or if there are no teams; returns false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamsWithFewestPlayers(t)<br />
if t and type(t)=="table" then -- Do checks for validity<br />
for i,v in ipairs(t) do<br />
if (not isElement(v)) or (type(v) ~= "team") then<br />
-- Use stacktrace from debug to output a message to parent function<br />
return false<br />
end<br />
end<br />
else t = getElementsByType("team") end<br />
local lowestScorers, lowestCount = {}, math.huge<br />
for i,v in ipairs(t) do<br />
local count = countPlayersInTeam(v)<br />
if count < lowestCount then<br />
lowestScorers = {v}<br />
lowestCount = count<br />
elseif count == lowestCount then<br />
table.insert(lowestScorers, v)<br />
end<br />
end<br />
return lowestScorers<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
This example adds a "/lowest" to tell the player which team has the lowest amount of players.<br />
<syntaxhighlight lang="lua"><br />
-- Create a local function for the command "/lowest"<br />
function lowestTeam(player)<br />
local teams = getTeamsWithFewestPlayers() -- get table of teams<br />
local multiple = #teams > 1 -- is there more than one team?<br />
local teamListStr = ""<br />
for i,v in ipairs(teams) do<br />
local name = getTeamName(v)<br />
teamListStr = teamListStr .. " " .. name<br />
if (" "..teamListStr ~= name) and (i ~= #teams) then<br />
teamListStr = teamListStr .. " and"<br />
end<br />
end<br />
local str = string.format("The %s with the fewest players %s %s!", multiple and "teams" or "team", multiple and "are" or "is", teamListStr)<br />
outputChatBox(str, player)<br />
end<br />
addCommandHandler("lowest", lowestTeam)<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: qaisjp<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=39958GetTeamWithFewestPlayers2014-06-18T18:08:13Z<p>Ixjf: Undo revision 39956 by Booo (talk)</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
This function gets a team from it's few players.<br />
'''Note:''' If 2 teams has the same count, then the first one found will be returned!<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getFewPlayersOnTeams ( element team1,element team2)</syntaxhighlight><br />
<br />
<br />
===Return===<br />
Returns a [[team]] else false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getFewPlayersOnTeams(team1,team2)<br />
if (isElement(team1) and getElementType(team1) == "team") and (isElement(team2) and getElementType(team2) == "team") then<br />
local team1C = countPlayersInTeam (team1)<br />
local team2C = countPlayersInTeam (team2)<br />
if team1C == team2C then return team1<br />
else<br />
if team1C == math.min(team1C,team2C) then<br />
return team1<br />
else<br />
return team2<br />
end<br />
end<br />
end<br />
return false<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
<br />
<br />
function setPlayerToTeam(source)<br />
local redteam = getTeamFromName ( "Red" )<br />
local blueteam = getTeamFromName ( "Blue" )<br />
if redteam and blueteam then<br />
local theteam = getfewPlayersOnTeams(redteam,blueteam)<br />
if theteam then<br />
setPlayerTeam(source,theteam)<br />
local PlayerName = getPlayerName ( source )<br />
outputChatBox ( " " .. joinedPlayerName .. " Joined "..getTeamName(theteam).." team !" , root, 255, 255, 255 )<br />
end<br />
else<br />
createTeam ("Red",255,0,0)<br />
createTeam ("Blue",0,0,255)<br />
setPlayerToTeam(source)<br />
end<br />
end<br />
addEventHandler ( "onPlayerJoin", getRootElement(), setPlayerToTeam )<br />
<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: Booo<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=39922GetTeamWithFewestPlayers2014-06-14T16:54:56Z<p>Ixjf: </p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
A potentially useful function to give you a table containing the fewest players. I've only put this here to replace a crappy function.<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers([table teams])</syntaxhighlight><br />
<br />
===Optional arguments===<br />
* '''teams''': a '''regular indexed''' table of teams. Example: {team, team, team, [4]=team}<br />
<br />
===Return===<br />
Returns a table containing [[team|teams]].<br />
If it fails (invalid table provided) or if there are no teams; returns false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamsWithFewestPlayers(t)<br />
if t and type(t)=="table" then -- Do checks for validity<br />
for i,v in ipairs(t) do<br />
if (not isElement(v)) or (type(v) ~= "team") then<br />
-- Use stacktrace from debug to output a message to parent function<br />
return false<br />
end<br />
end<br />
else t = getElementsByType("team") end<br />
local lowestScorers, lowestCount = {}, math.huge<br />
for i,v in ipairs(t) do<br />
local count = countPlayersInTeam(v)<br />
if count < lowestCount then<br />
lowestScorers = {v}<br />
lowestCount = count<br />
elseif count == lowestCount then<br />
table.insert(lowestScorers, v)<br />
end<br />
end<br />
return lowestScorers<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
This example adds a "/lowest" to tell the player which team has the lowest amount of players.<br />
<syntaxhighlight lang="lua"><br />
-- Create a local function for the command "/lowest"<br />
function lowestTeam(player)<br />
local teams = getTeamsWithFewestPlayers() -- get table of teams<br />
local multiple = #teams > 1 -- is there more than one team?<br />
local teamListStr = ""<br />
for i,v in ipairs(teams) do<br />
local name = getTeamName(v)<br />
teamListStr = teamListStr .. " " .. name<br />
if (" "..teamListStr ~= name) and (i ~= #teams) then<br />
teamListStr = teamListStr .. " and"<br />
end<br />
end<br />
local str = string.format("The %s with the fewest players %s %s!", multiple and "teams" or "team", multiple and "are" or "is", teamListStr)<br />
outputChatBox(str, player)<br />
end<br />
addCommandHandler("lowest", lowestTeam)<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: qaisjp<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=39921GetTeamWithFewestPlayers2014-06-14T16:53:49Z<p>Ixjf: /* Example */</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
A potentially useful function to give you a table containing the fewest players. I've only put this here to replace a crappy function.<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers([table teams])</syntaxhighlight><br />
<br />
===Optional arguments===<br />
* '''teams''': a '''regular indexed''' table of teams. Example: {team, team, team, [4]=team}<br />
<br />
===Return===<br />
Returns a table containing [[team|teams]].<br />
If it fails (invalid table provided) or if there are no teams; returns false.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamsWithFewestPlayers(t)<br />
if t and type(t)=="table" then -- Do checks for validity<br />
for i,v in ipairs(t) do<br />
if (not isElement(v)) or (type(v) ~= "team") then<br />
-- Use stacktrace from debug to output a message to parent function<br />
return false<br />
end<br />
end<br />
else t = getElementsByType("team") end<br />
local lowestScorers, lowestCount = {}, math.huge<br />
for i,v in ipairs(t) do<br />
local count = countPlayersInTeam(v)<br />
if count < lowestCount then<br />
lowestScorers = {v}<br />
lowestCount = count<br />
elseif count == lowestCount then<br />
table.insert(lowestScorers, v)<br />
end<br />
end<br />
return lowestScorers<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
This example adds a "/lowest" to tell the player which team has the lowest amount of players.<br />
<syntaxhighlight lang="lua"><br />
-- Create a local function for the command "/lowest"<br />
local function(player)<br />
local teams = getTeamsWithFewestPlayers() -- get table of teams<br />
local multiple = #teams > 1 -- is there more than one team?<br />
local teamListStr = ""<br />
for i,v in ipairs(teams) do<br />
local name = getTeamName(v)<br />
teamListStr = teamListStr .. " " .. name<br />
if (" "..teamListStr ~= name) and (i ~= #teams) then<br />
teamListStr = teamListStr .. " and"<br />
end<br />
end<br />
local str = string.format("The %s with the fewest players %s %s!", multiple and "teams" or "team", multiple and "are" or "is", teamListStr)<br />
outputChatBox(str, player)<br />
end<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: qaisjp<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=39915GetTeamWithFewestPlayers2014-06-14T16:37:15Z<p>Ixjf: /* Code */</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
This function compares the number of players between multiple teams and returns the team with the least players. If all the teams have the same amount of players, the first team will be returned<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1, element team2, ... )</syntaxhighlight><br />
<br />
===Return===<br />
Returns the [[team]] with least players, ''false'' if invalid arguments were specified.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamWithFewestPlayers ( ... )<br />
<br />
local teamsParam = {...}<br />
<br />
for _, team in ipairs ( teamsParam ) do<br />
if not ( isElement ( team ) and getElementType ( team ) == "team" ) then<br />
return false<br />
end<br />
end<br />
<br />
local sortFunc = function ( a, b )<br />
<br />
return countPlayersInTeam ( a ) < countPlayersInTeam ( b )<br />
<br />
end<br />
<br />
table.sort ( teamsParam, sortFunc )<br />
<br />
return teamsParam[1]<br />
<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
local redTeam = createTeam ( "Red", 255, 0, 0 )<br />
local blueTeam = createTeam ( "Blue", 0, 0, 255 )<br />
<br />
function assignTeamToPlayer ()<br />
<br />
local team = getTeamWithFewestPlayers ( redTeam, blueTeam )<br />
<br />
setPlayerTeam ( source, team )<br />
outputChatBox ( getPlayerName ( source ) .. " joined the " .. getTeamName ( team ) .. " team!" )<br />
<br />
end<br />
addEventHandler ( "onPlayerJoin", root, assignTeamToPlayer )<br />
</syntaxhighlight><br />
</section><br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=39914GetTeamWithFewestPlayers2014-06-14T16:36:35Z<p>Ixjf: Fixed typo</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
This function compares the number of players between multiple teams and returns the team with the least players. If all the teams have the same amount of players, the first team will be returned<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1, element team2, ... )</syntaxhighlight><br />
<br />
===Return===<br />
Returns the [[team]] with least players, ''false'' if invalid arguments were specified.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamWithFewestPlayers ( ... )<br />
<br />
local teamsParam = {...}<br />
<br />
for _, team in ipairs ( teamsParam ) do<br />
if not ( isElement ( team ) and getElementType ( team ) == "team" ) then<br />
return false<br />
end<br />
end<br />
<br />
local sortFunc = function ( a, b )<br />
<br />
return countPlayersInTeam ( a ) < countPlayersInTeam ( b )<br />
<br />
end<br />
<br />
table.sort ( teamsParam, sortFunc )<br />
<br />
return teams[1]<br />
<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
local redTeam = createTeam ( "Red", 255, 0, 0 )<br />
local blueTeam = createTeam ( "Blue", 0, 0, 255 )<br />
<br />
function assignTeamToPlayer ()<br />
<br />
local team = getTeamWithFewestPlayers ( redTeam, blueTeam )<br />
<br />
setPlayerTeam ( source, team )<br />
outputChatBox ( getPlayerName ( source ) .. " joined the " .. getTeamName ( team ) .. " team!" )<br />
<br />
end<br />
addEventHandler ( "onPlayerJoin", root, assignTeamToPlayer )<br />
</syntaxhighlight><br />
</section><br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=39913GetTeamWithFewestPlayers2014-06-14T16:36:15Z<p>Ixjf: </p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
This function compares the number of players between multiple times and returns the team with the least players. If all the teams have the same amount of players, the first team will be returned<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1, element team2, ... )</syntaxhighlight><br />
<br />
===Return===<br />
Returns the [[team]] with least players, ''false'' if invalid arguments were specified.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
function getTeamWithFewestPlayers ( ... )<br />
<br />
local teamsParam = {...}<br />
<br />
for _, team in ipairs ( teamsParam ) do<br />
if not ( isElement ( team ) and getElementType ( team ) == "team" ) then<br />
return false<br />
end<br />
end<br />
<br />
local sortFunc = function ( a, b )<br />
<br />
return countPlayersInTeam ( a ) < countPlayersInTeam ( b )<br />
<br />
end<br />
<br />
table.sort ( teamsParam, sortFunc )<br />
<br />
return teams[1]<br />
<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
local redTeam = createTeam ( "Red", 255, 0, 0 )<br />
local blueTeam = createTeam ( "Blue", 0, 0, 255 )<br />
<br />
function assignTeamToPlayer ()<br />
<br />
local team = getTeamWithFewestPlayers ( redTeam, blueTeam )<br />
<br />
setPlayerTeam ( source, team )<br />
outputChatBox ( getPlayerName ( source ) .. " joined the " .. getTeamName ( team ) .. " team!" )<br />
<br />
end<br />
addEventHandler ( "onPlayerJoin", root, assignTeamToPlayer )<br />
</syntaxhighlight><br />
</section><br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=39912GetTeamWithFewestPlayers2014-06-14T16:18:41Z<p>Ixjf: </p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
This function compares number of players in two teams specifed and returns the team with less players.<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1,element team2)</syntaxhighlight><br />
<br />
<br />
===Return===<br />
Returns a [[team]] team with less players, ''false'' if invalid arguments were specified.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
local function isTeam ( ele )<br />
<br />
return isElement ( ele ) and getElementType ( ele ) == "team"<br />
<br />
end<br />
<br />
function getTeamWithFewestPlayers ( team1, team2 )<br />
<br />
if isTeam ( team1 ) and isTeam ( team2 ) then<br />
return countPlayersInTeam ( team1 ) < countPlayersInTeam ( team2 ) and team1 or team2<br />
end<br />
<br />
return false<br />
<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
<br />
<br />
function setPlayerToTeam(source)<br />
local redteam = getTeamFromName ( "Red" )<br />
local blueteam = getTeamFromName ( "Blue" )<br />
if redteam and blueteam then<br />
local theteam = getTeamWithFewestPlayers(redteam,blueteam)<br />
if theteam then<br />
setPlayerTeam(source,theteam)<br />
local PlayerName = getPlayerName ( source )<br />
outputChatBox ( " " .. joinedPlayerName .. " Joined "..getTeamName(theteam).." team !" , root, 255, 255, 255 )<br />
end<br />
else<br />
createTeam ("Red",255,0,0)<br />
createTeam ("Blue",0,0,255)<br />
setPlayerToTeam(source)<br />
end<br />
end<br />
addEventHandler ( "onPlayerJoin", getRootElement(), setPlayerToTeam )<br />
<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: Booo<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=GetTeamWithFewestPlayers&diff=39911GetTeamWithFewestPlayers2014-06-14T16:18:24Z<p>Ixjf: /* Code */</p>
<hr />
<div>{{Useful Function}}<br />
<lowercasetitle/><br />
__NOTOC__<br />
This function compares number of players in two teams specifed and returns the team with less players.<br />
'''Note:''' If both teams have the same amount of players, then the first one will be returned!<br />
==Syntax==<br />
<syntaxhighlight lang="lua">team getTeamWithFewestPlayers ( element team1,element team2)</syntaxhighlight><br />
<br />
<br />
===Return===<br />
Returns a [[team]] team with less players, ''false'' if invalid arguments were specified.<br />
<br />
==Code==<br />
<syntaxhighlight lang="lua"><br />
local function isTeam ( ele )<br />
<br />
return isElement ( ele ) and getElementType ( ele ) == "team"<br />
<br />
end<br />
<br />
function getTeamWithFewestPlayers ( team1, team2 )<br />
<br />
if isTeam ( team1 ) and isTeam ( team2 ) then<br />
return countPlayersInTeam ( team1 ) < countPlayersInTeam ( team2 ) and team1 or team2<br />
end<br />
<br />
return false<br />
<br />
end<br />
</syntaxhighlight><br />
<br />
==Example==<br />
<section name="Example" class="server" show="true"><br />
<br />
<br />
<syntaxhighlight lang="lua"><br />
<br />
<br />
function setPlayerToTeam(source)<br />
local redteam = getTeamFromName ( "Red" )<br />
local blueteam = getTeamFromName ( "Blue" )<br />
if redteam and blueteam then<br />
local theteam = getTeamWithFewestPlayers(redteam,blueteam)<br />
if theteam then<br />
setPlayerTeam(source,theteam)<br />
local PlayerName = getPlayerName ( source )<br />
outputChatBox ( " " .. joinedPlayerName .. " Joined "..getTeamName(theteam).." team !" , root, 255, 255, 255 )<br />
end<br />
else<br />
createTeam ("Red",255,0,0)<br />
createTeam ("Blue",0,0,255)<br />
setPlayerToTeam(source)<br />
end<br />
end<br />
addEventHandler ( "onPlayerJoin", getRootElement(), setPlayerToTeam )<br />
<br />
</syntaxhighlight><br />
</section><br />
<br />
<br />
<br />
Author: Booo<br />
<br />
==See Also==<br />
{{Useful_Functions}}</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=Client_Scripting_Functions&diff=39892Client Scripting Functions2014-06-14T15:48:35Z<p>Ixjf: /* Utility functions */</p>
<hr />
<div><pageclass class="client"></pageclass><br />
{{Adding_Pages_to_Categories_and_Templates}}<br />
This page lists all the '''client-side''' scripting functions that have been implemented and are available as native functions from the Deathmatch mod. To request a function or event, use [[Requested Functions and Events]]<br />
<br />
For more functions, check the [[Useful_Functions|useful functions page]].<br />
__TOC__<br />
<br />
==Audio functions==<br />
{{Client_audio_functions}}<br />
<br />
==Blip functions==<br />
{{Client_blip_functions}}<br />
<br />
==Camera functions==<br />
{{Client_camera_functions}}<br />
<br />
==Clothes and body functions==<br />
{{Client_Clothes and body functions}}<br />
<br />
==Collision shape functions==<br />
{{Client_collision_shape_functions}}<br />
<br />
==Cursor functions==<br />
{{Client_cursor_functions}}<br />
<br />
==Drawing functions==<br />
{{Drawing_functions}}<br />
<br />
==Effects functions==<br />
{{Client_Effects_functions}}<br />
<br />
==Element functions==<br />
{{Client_element_functions}}<br />
<br />
==Engine functions==<br />
{{Engine_functions}}<br />
<br />
==Event functions==<br />
{{Client_event_functions}}<br />
<br />
==Explosion functions==<br />
{{Client_explosion_functions}}<br />
<br />
==File functions==<br />
{{Client file functions}}<br />
<br />
==Fire functions==<br />
{{Client fire functions}}<br />
<br />
==GUI functions==<br />
{{GUI_functions}}<br />
<br />
==Input functions==<br />
{{Client_input_functions}}<br />
<br />
==Marker functions==<br />
{{Client_marker_functions}}<br />
<br />
==Object functions==<br />
{{Client_object_functions}}<br />
<br />
==Output functions==<br />
{{Client_output_functions}}<br />
<br />
==Ped functions==<br />
{{Client_ped_functions}}<br />
<br />
==Pickup functions==<br />
{{Client_pickup_functions}}<br />
<br />
==Player functions==<br />
{{Client_player_functions}}<br />
<br />
==Projectile functions==<br />
{{Client_projectile_functions}}<br />
<br />
==Radar-area functions==<br />
{{Client_radar-area_functions}}<br />
<br />
==Resource functions==<br />
{{Client_resource_functions}}<br />
<br />
==Team functions==<br />
{{Client_team_functions}}<br />
<br />
==Utility functions==<br />
{{Client_utility_functions}}<br />
<br />
==Vehicle functions==<br />
{{Client_vehicle_functions}}<br />
<br />
==Water functions==<br />
{{Client_water_functions}}<br />
<br />
==Weapon functions==<br />
{{Client_weapon_functions}}<br />
<br />
==Weapon Creation functions==<br />
Documentation can be found [http://code.google.com/p/mtasa-blue/source/detail?r=4555 here] and [http://code.google.com/p/mtasa-blue/source/detail?r=4557 here]<br />
{{Client_weapon_creation_functions}}<br />
<br />
==World functions==<br />
{{Client_world_functions}}<br />
<br />
==XML functions==<br />
{{Client XML functions}}<br />
<br />
[[ar:Client Scripting Functions]]<br />
[[pl:Funkcje po stronie klienta]]<br />
[[it:Funzioni Client-side]]<br />
[[ru:Client Scripting Functions]]<br />
[[es:Funciones del cliente]]<br />
[[de:Clientseitige_Funktionen]]<br />
[[tr:Client Taraflı Fonksiyonlar]]</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=Useful_Functions&diff=39891Useful Functions2014-06-14T15:46:09Z<p>Ixjf: Undo revision 39871 by Le'CC (talk) - Nop.</p>
<hr />
<div><pageclass class="#228B22"></pageclass><br />
{{Adding_Pages_to_Categories_and_Templates}}<br />
<br />
This page list's functions that are not included in the server or the client, but are still useful.<br />
<br/><br />
{{Useful_Functions}}<br />
[[ES:Funciones Útiles]]<br />
[[ru:Useful Functions]]<br />
[[de:Nützliche_Funktionen]]<br />
[[Ar:وظائف مفيدة]]</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=Client_Scripting_Events&diff=39888Client Scripting Events2014-06-14T15:40:47Z<p>Ixjf: Undo revision 39884 by Le'CC (talk)</p>
<hr />
<div><pageclass class="client"></pageclass><br />
This page lists all '''client-side''' scripting [[event]]s that have been implemented and are available in the Deathmatch mod.<br />
<br />
To request a function or event, use [[Requested Functions and Events]].<br />
<br />
'''Server-side scripting events can be found here: [[Server Scripting Events]]<br />
<br />
==Colshape events==<br />
{{Client_colshape_events}}<br />
<br />
==Element events==<br />
{{Client_element_events}}<br />
<br />
==GUI events==<br />
{{GUI_events}}<br />
<br />
==Marker events==<br />
{{Client_marker_events}}<br />
<br />
==Ped events==<br />
{{Client_ped_events}}<br />
<br />
==Pickup events==<br />
{{Client_pickup_events}}<br />
<br />
==Player events==<br />
{{Client_player_events}}<br />
<br />
==Object events==<br />
{{Client_object_events}}<br />
<br />
==Projectile events==<br />
{{Client_projectile_events}}<br />
<br />
==Resource events==<br />
{{Client_resource_events}}<br />
<br />
==Sound events==<br />
{{Sound_events}}<br />
<br />
==Vehicle events==<br />
{{Client_vehicle_events}}<br />
<br />
==Weapon creation events==<br />
{{Client_weapon_creation_events}}<br />
<br />
==Other events==<br />
{{Client_other_events}}<br />
<br />
[[pl:Zdarzenia_po_stronie_klienta]]<br />
[[ru:Client Scripting Events]]<br />
[[es:Client Scripting Events]]</div>Ixjfhttps://wiki.multitheftauto.com/index.php?title=Client_Scripting_Functions&diff=39887Client Scripting Functions2014-06-14T15:40:07Z<p>Ixjf: Undo revision 39882 by Le'CC (talk)</p>
<hr />
<div><pageclass class="client"></pageclass><br />
{{Adding_Pages_to_Categories_and_Templates}}<br />
This page lists all the '''client-side''' scripting functions that have been implemented and are available as native functions from the Deathmatch mod. To request a function or event, use [[Requested Functions and Events]]<br />
<br />
For more functions, check the [[Useful_Functions|useful functions page]].<br />
__TOC__<br />
<br />
==Audio functions==<br />
{{Client_audio_functions}}<br />
<br />
==Blip functions==<br />
{{Client_blip_functions}}<br />
<br />
==Camera functions==<br />
{{Client_camera_functions}}<br />
<br />
==Clothes and body functions==<br />
{{Client_Clothes and body functions}}<br />
<br />
==Collision shape functions==<br />
{{Client_collision_shape_functions}}<br />
<br />
==Cursor functions==<br />
{{Client_cursor_functions}}<br />
<br />
==Drawing functions==<br />
{{Drawing_functions}}<br />
<br />
==Effects functions==<br />
{{Client_Effects_functions}}<br />
<br />
==Element functions==<br />
{{Client_element_functions}}<br />
<br />
==Engine functions==<br />
{{Engine_functions}}<br />
<br />
==Event functions==<br />
{{Client_event_functions}}<br />
<br />
==Explosion functions==<br />
{{Client_explosion_functions}}<br />
<br />
==File functions==<br />
{{Client file functions}}<br />
<br />
==Fire functions==<br />
{{Client fire functions}}<br />
<br />
==GUI functions==<br />
{{GUI_functions}}<br />
<br />
==Input functions==<br />
{{Client_input_functions}}<br />
<br />
==Marker functions==<br />
{{Client_marker_functions}}<br />
<br />
==Object functions==<br />
{{Client_object_functions}}<br />
<br />
==Output functions==<br />
{{Client_output_functions}}<br />
<br />
==Ped functions==<br />
{{Client_ped_functions}}<br />
<br />
==Pickup functions==<br />
{{Client_pickup_functions}}<br />
<br />
==Player functions==<br />
{{Client_player_functions}}<br />
<br />
==Projectile functions==<br />
{{Client_projectile_functions}}<br />
<br />
==Radar-area functions==<br />
{{Client_radar-area_functions}}<br />
<br />
==Resource functions==<br />
{{Client_resource_functions}}<br />
<br />
==Team functions==<br />
{{Client_team_functions}}<br />
<br />
==Utility functions==<br />
* [[binary]]<br />
{{Client_utility_functions}}<br />
<br />
==Vehicle functions==<br />
{{Client_vehicle_functions}}<br />
<br />
==Water functions==<br />
{{Client_water_functions}}<br />
<br />
==Weapon functions==<br />
{{Client_weapon_functions}}<br />
<br />
==Weapon Creation functions==<br />
Documentation can be found [http://code.google.com/p/mtasa-blue/source/detail?r=4555 here] and [http://code.google.com/p/mtasa-blue/source/detail?r=4557 here]<br />
{{Client_weapon_creation_functions}}<br />
<br />
==World functions==<br />
{{Client_world_functions}}<br />
<br />
==XML functions==<br />
{{Client XML functions}}<br />
<br />
[[ar:Client Scripting Functions]]<br />
[[pl:Funkcje po stronie klienta]]<br />
[[it:Funzioni Client-side]]<br />
[[ru:Client Scripting Functions]]<br />
[[es:Funciones del cliente]]<br />
[[de:Clientseitige_Funktionen]]<br />
[[tr:Client Taraflı Fonksiyonlar]]</div>Ixjf