<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.multitheftauto.com/wiki/Code_Snippets_General?action=history&amp;feed=atom</id>
	<title>Code Snippets General - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.multitheftauto.com/wiki/Code_Snippets_General?action=history&amp;feed=atom"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=Code_Snippets_General&amp;action=history"/>
	<updated>2026-05-12T23:42:55Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=Code_Snippets_General&amp;diff=79860&amp;oldid=prev</id>
		<title>FileEX at 10:47, 13 July 2024</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=Code_Snippets_General&amp;diff=79860&amp;oldid=prev"/>
		<updated>2024-07-13T10:47:23Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 10:47, 13 July 2024&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l143&quot;&gt;Line 143:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 143:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;addEventHandler(&amp;quot;onResourceStart&amp;quot;, getRootElement(), ResourceStarted)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;addEventHandler(&amp;quot;onResourceStart&amp;quot;, getRootElement(), ResourceStarted)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[Category:Scripting Concepts]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wiki-wiki_dev_:diff::1.12:old-24045:rev-79860 --&gt;
&lt;/table&gt;</summary>
		<author><name>FileEX</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=Code_Snippets_General&amp;diff=24045&amp;oldid=prev</id>
		<title>Chumillas at 11:34, 14 July 2010</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=Code_Snippets_General&amp;diff=24045&amp;oldid=prev"/>
		<updated>2010-07-14T11:34:44Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 11:34, 14 July 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;{{CatUp|Common Code Snippets}}&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;This page contains general purpose code snippets.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;This page contains general purpose code snippets.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wiki-wiki_dev_:diff::1.12:old-24044:rev-24045 --&gt;
&lt;/table&gt;</summary>
		<author><name>Chumillas</name></author>
	</entry>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=Code_Snippets_General&amp;diff=24044&amp;oldid=prev</id>
		<title>Chumillas: Created page with '{{CatUp|Common Code Snippets}} This page contains general purpose code snippets.  ''(You can test your code in http://www.lua.org/demo.html)''    = Mathematical Snippets =  === G…'</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=Code_Snippets_General&amp;diff=24044&amp;oldid=prev"/>
		<updated>2010-07-14T11:34:09Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;#039;{{CatUp|Common Code Snippets}} This page contains general purpose code snippets.  &amp;#039;&amp;#039;(You can test your code in http://www.lua.org/demo.html)&amp;#039;&amp;#039;    = Mathematical Snippets =  === G…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{CatUp|Common Code Snippets}}&lt;br /&gt;
This page contains general purpose code snippets.&lt;br /&gt;
&lt;br /&gt;
''(You can test your code in http://www.lua.org/demo.html)''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Mathematical Snippets =&lt;br /&gt;
&lt;br /&gt;
=== Generating Pseudo-Random Numbers ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
math.random( 3, 9 ) -- Generates a random integer between 3 and 9 inclusively.&lt;br /&gt;
math.Rand( 3, 9 ) -- Generates a random real number (floating point) between 3 and 9 inclusively.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Better Rounding ===&lt;br /&gt;
* This allows you to round to a decimal point instead of to a whole number.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- Rounds to the 2nd decimal point&lt;br /&gt;
-- Usage: math.AdvRound( 1.6443132, 2 ); &lt;br /&gt;
--&lt;br /&gt;
function math.AdvRound( val, d )&lt;br /&gt;
	d = d or 0;&lt;br /&gt;
	&lt;br /&gt;
	return math.Round( val * (10 ^ d) ) / (10 ^ d);&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Toggling a Boolean ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local Boolean = false&lt;br /&gt;
 -- Toggle the boolean.&lt;br /&gt;
Boolean = not Boolean&lt;br /&gt;
-- or, in C-style notation:&lt;br /&gt;
Boolean = !Boolean&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Variables =&lt;br /&gt;
=== Defining Multiple Variables ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- X = 1, Y = 2, Z = 3.&lt;br /&gt;
local X, Y, Z = 1, 2, 3&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Using OR ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- If Y is non-nil, set X to Y; otherwise, set X to 0.&lt;br /&gt;
local X = Y or 0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Switching Two Variables without Making a Third ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
a,b = b,a&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Loops and statements =&lt;br /&gt;
&lt;br /&gt;
=== Looping through Every Character in a String ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
 for i = 1, string.len( strToLoop ) do&lt;br /&gt;
    local Character = string.sub( strToLoop, i, i )&lt;br /&gt;
     -- Do stuff with Character here.&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Using 'repeat, until' ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
repeat&lt;br /&gt;
num = math.random( 200 )&lt;br /&gt;
Msg( num .. &amp;quot;\n&amp;quot; )&lt;br /&gt;
until( num == 25 )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Doing 'if .. then ... else' in One Line ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
local value = ( x &amp;gt; 1 ) &amp;amp;&amp;amp; &amp;quot;X is greater&amp;quot; || &amp;quot;X isn't greater&amp;quot;;&lt;br /&gt;
--and in STD. Lua notation:&lt;br /&gt;
local value = ( x &amp;gt; 1 ) and &amp;quot;X is greater&amp;quot; or &amp;quot;X isn't greater&amp;quot;;&lt;br /&gt;
--basic format for Inline Conditionals:&lt;br /&gt;
local value = (Condition) and TrueValue or FalseValue;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Returning 'if .. then ... else' ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
return current &amp;lt; max and func( current + 1, max ) or nil&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Miscellaneous =&lt;br /&gt;
=== Convert XYToRelative (by Cazomino05) ===&lt;br /&gt;
&lt;br /&gt;
Converts an XY co-ordinate to a relative size requires the resolution you tested it on (you can find it in gta display settings)&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
function ConvertXYToRelative(Resx, Resy, x, y)&lt;br /&gt;
    local Rx = (1/Resx)*x&lt;br /&gt;
    local Ry = (1/Resy)*y&lt;br /&gt;
    return Rx,Ry&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- EXAMPLE&lt;br /&gt;
function ResourceStarted(resource)&lt;br /&gt;
    if (resource ~= getThisResource()) then&lt;br /&gt;
        return&lt;br /&gt;
    end&lt;br /&gt;
    local x,y = ConvertXYToRelative(800, 600, 400, 300)&lt;br /&gt;
    outputChatBox(&amp;quot;Relative sizes:  &amp;quot;..x..&amp;quot;, &amp;quot;..y)&lt;br /&gt;
end&lt;br /&gt;
addEventHandler(&amp;quot;onResourceStart&amp;quot;, getRootElement(), ResourceStarted)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Chumillas</name></author>
	</entry>
</feed>