Mutation Operators: Difference between revisions

From GiderosMobile
Apollo14 (talk | contribs)
No edit summary
Apollo14 (talk | contribs)
No edit summary
Line 17: Line 17:
=== <translate>Examples</translate> ===
=== <translate>Examples</translate> ===
'''Simple larger example'''<br/>
'''Simple larger example'''<br/>
<source lang="lua">  
<source lang="lua">score+=1  -- faster and less typing than score=score+1</source>
score+=1  -- faster and less typing than score=score+1</source>
<br/>'''"getDistance" function:'''
<source lang="lua">--Non-optimized variant (5x slower):
function getDistance( x1, y1, x2, y2 )
return math.sqrt ( math.pow ( x2 - x1, 2 ) + math.pow ( y2 - y1, 2 ) )
end
 
--Optimized variant (5x faster):
function getDistance2( x1, y1, x2, y2 )
return ((x1 - x2)^2 + (y1 - y2)^2)^0.5
end
 
</source>
{|-
{|-
| style="width: 50%; vertical-align:top;"|
| style="width: 50%; vertical-align:top;"|

Revision as of 06:54, 8 December 2018


Supported platforms:
Available since: Gideros 2017.11

Description


Operators to mutate a value.

a += b   Add 'b' to 'a'.
a -= b   Subtract 'b from 'a'.
a *= b   Multiply 'a' by 'b', result in 'a'.
a /= b   Divide 'a' by 'b', result in 'a'.
a %= b   The result of 'a%b' is placed in 'a'.
a ^= b   The result of 'a^b' is placed in 'a'. ^ is faster than a=math.pow(a,b)
a ^= 0.5   The result is faster than a=math.sqrt(a)

Examples

Simple larger example

score+=1  -- faster and less typing than score=score+1


"getDistance" function:

--Non-optimized variant (5x slower):
function getDistance( x1, y1, x2, y2 )
	return math.sqrt ( math.pow ( x2 - x1, 2 ) + math.pow ( y2 - y1, 2 ) )
end

--Optimized variant (5x faster):
function getDistance2( x1, y1, x2, y2 )
	return ((x1 - x2)^2 + (y1 - y2)^2)^0.5
end

Methods

Events

Constants