This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
gdevelop5:all-features:expressions [2019/07/30 23:30]
gdevelop5:all-features:expressions [2020/05/10 12:17] (current)
Line 1: Line 1:
 # Writing expressions # Writing expressions
-GDevelop supports expressions! ​Expressions are dynamic ​values ​that can be used in event parameters. For example, to set an object position, ​ you can use a fixed number as ``75`or you can use an expression such as ``AnotherObject.X() - Variable(position_offset)``.+ 
 +Expressions are **dynamic ​formulas** ​that can be used in event parameters. For example, to set an object position, you can use a fixed number as "`75`" ​or you can use an expression such as "`Player.X()`" ​(to set the object to the same X coordinate as the Player object living on the scene).  
 +<note tip>More complex expressions can include mathematical functions and use functions from other objects and behaviors. See the explanation later in this page.</​note>​
 There are two types of expressions:​ There are two types of expressions:​
Line 6: Line 9:
   -  //​**Numerics**// ​   -  //​**Numerics**// ​
-Numerics can be used in event parameters. They receive a numeric value such as object position, z-order, variable number, etc.+{{ :​gdevelop5:​all-features:​number_expression.png?​nolink |}} 
 +Numerics can be used in event parameters. They receive a numeric value such as object position, z-order, ​variable number, etc. 
 +  -  //​**Strings**// ​ (also called "​Text"​) 
 +{{ :​gdevelop5:​all-features:​text_expression.png?​nolink |}} 
 +Strings can be used in parameters that receive a text value. You can use this expression to read the content of a text object string, the text of another variable, etc. 
 +## How to write an expression?
-  -  //​**Strings**// ​+### Simple expressions:​ just enter a number or a text
-Strings ​can be used in parameters that receive ​text/string valueYou can use this expression ​to read the content of text object ​string, ​it's color, the text of another variable, etc.+If you don't need to do a complex calculation,​ you can just enter number (for example: 123)If the expression ​is *string*you can enter a text *surrounded by quotes* (this is important).
-**[[gdevelop5:​all-features:​functions|You can learn more about specific ​expressions ​here.]]**+For example: 
 +{{ :gdevelop5:​all-features:​expressions:​pasted:​20200510-115714.png }} 
 +{{ :​gdevelop5:​all-features:​expressions:​pasted:​20200510-115743.png }}
-## Numeric expressions +In these exampleswe're setting a bullet speed to 310 pixels per second, and setting the animation of an object to SpaceshipIdle (this could be anything else, it depends on what animations are stored ​in the object).
-As stated aboveNumeric expressions can be used in numeric parameters.+
-### Random values+<note important>​If you enter an invalid formula, for example if you forget to put double quotes around your text, the field will be red and will display the error what was found: 
-``Random(max)`` - Returns an integer between `0` and the `max` value that is defined. ​ The "​range"​ is [0, max].+### Complex expressions:​ use functions ​and operators
-//Examples using the Random() Expression://​ +If you need a more complex calculationyou can use *operators* ​and *functions*.
-<code Javascript>​Random(3) ​ // The 3 possible integer values are 01,  2,  ​and +
-``RandomInRange(min, max)`` - Returns an integer between ​`min` and `max`.  ​The ​"range" ​is [minmax].+* Operators are the usual mathematical operators: ​`+` (add)`-` (subtract), ​`*` (multiply) ​and `/(divide)For example, you can enter "10+10". For stringsyou can only use `+` to concatenate two texts. 
 +* Functions are values that will be computed during the game.
-//Examples using the RandomInRange() Expression://​ +When you start to write a function name, GDevelop will show you a list of functions ​(or objectscorresponding to what your typing ​(this is called "​smart"​ autocompletion). You need to enter the function namefollowed by parenthesis ​
-<code Javascript>​RandomInRange(-1, 2// The 4 possible values are are  -1, 0, 1 and 2  ​+
-// Note that "​RandomInRange(min,​ max)" is equivalent to "min + Random(max ​min)": +<note tip>You can find a **[[gdevelop5:​all-features:expressions-reference|list of all expressions and their descriptions here]]**.</note>
--1 + Random(2 - -1) // This gives the same results as above +
-``RandomFloat(max)`` - Returns ​float between `0` and `max`.  The range is ``[0, max)``. Note that ``max`` is not included in the range. It's important ​to also note that the possible results are infiniteThis is because there are infinite real numbers between any two different values.\\+* Functions without objects are called with just their names: ​`FunctionName()`.  
 +* Functions of objects are started with the object name and dot: `Object.FunctionName()`.  
 +* Finally, for objects having behavior, using a double colon (`::`to separate ​the behavior name from the function: `Object.BehaviorName::​PropertyName()` ​
-//Examples using the RandomFloat(max) Expression://​ 
-<​code>​RandomFloat(2.5) //SOME of the many possible values are 0, 1.467798, 2.000587, and 2.499999</​code> ​ 
-``RandomFloatInRange(minmax)`` -  Returns ​float between `min` and `max`. ​ The range is ``[min, max]``. Note that ``max`` ​is not included in the range. It's important ​to also note that the possible results are infinite as there are infinite real numbers between any two different values.\\+For examplewe can create an object at position ​that is slightly ​to the right of another (called "​EnemyBullet"​) by entering these expressions:​ 
 +{{ :​gdevelop5:​all-features:​expressions:​pasted:​20200510-120734.png?nolink |}}
-//Examples using the RandomFloatInRange() Expression://​ +## Finding functions that are useful for your game
-<​code>​RandomFloatInRange(-1.5,​ 2) //SOME of the many possible values ​are -1.5, -0.598156, 1.000874 and 1.99999</​code>​+
 +There are a lot of functions in GDevelop. You can discover them by clicking on the blue button on the right of field where you're entering an expression. This will open a menu organised by categories, containing a **list of all functions**:​
-``RandomWithStep(min,​ max, step)`` ​Returns a number between `min` and `max` in steps. The range is ``[min, min + N*step]`` with ``min + N*step ≤ max < min + (N+1)*step``. It means it can return any of the stepped values that are less than or equal than ``max``.\\+{{ :​gdevelop5:​all-features:​expressions:​pasted:​20200510-121635.png?nolink |}}
-//Examples using the RandomWithStep(minmax, step) Expression://+When you start to write a function or object nameGDevelop will show you a list of available functions. This is called a **smart auto-completion**:​ 
 +{{  :​gdevelop5:​all-features:​expressions:​pasted:20200510-121317.png?​nolink |}}
-<​code>​RandomWithStep(13, 0.6) // The generated numbers are ``1``, ``1.6``, ``2.2`` ​and ``2.8``</​code>​+Finallyyou can find a **[[gdevelop5:​all-features:​expressions-reference|list of all expressions ​and their descriptions here]]**.