diff --git a/docs/gdevelop5/behaviors/events-based-behaviors/generate-property-accessors.png b/docs/gdevelop5/behaviors/events-based-behaviors/generate-property-accessors.png new file mode 100644 index 0000000000..d26dbdab06 Binary files /dev/null and b/docs/gdevelop5/behaviors/events-based-behaviors/generate-property-accessors.png differ diff --git a/docs/gdevelop5/behaviors/events-based-behaviors/index.md b/docs/gdevelop5/behaviors/events-based-behaviors/index.md index 5ba179f8f2..6976b0f1fc 100644 --- a/docs/gdevelop5/behaviors/events-based-behaviors/index.md +++ b/docs/gdevelop5/behaviors/events-based-behaviors/index.md @@ -31,9 +31,9 @@ Behaviors are grouped into [extensions](/gdevelop5/extensions). They are the sam Click on **Create or search for new extensions** at the bottom. Then, select **Create a new extension** to [create a new extension](/gdevelop5/extensions/create). -By default, extensions don't have any behavior. Add one by clicking on **Add a new behavior** on the left. +By default, extensions don't have any behavior. Add one by clicking on the "+" button on the left next to **Behaviors**. -![](pasted/20210906-234614.png) +![](/gdevelop5/events/functions/empty-extension.png) You can choose the description and the name to display to users when they will add your behavior to an object. If you need to use a specific object (sprites, texts...), you can choose it with the drop-down list at the bottom. @@ -142,14 +142,11 @@ Properties can also be used to required behaviors as described in a following se ### Use actions and conditions to manipulate the properties -Once you have created some properties on your behavior, conditions and actions will be automatically added in the events sheet. For each property, there will be a condition to compare its value, and an action for changing it. -Strings and numbers will also have an expression to get their values. +Number, string and boolean parameters can be used with the same actions and conditions as global and scene [variables](/gdevelop5/all-features/variables). Properties can also be used directly in expressions by writing their name. For instance, a property called "ScoreToMultiply" can be used the following: `2 * ScoreToMultiply`. -!!! note - - If you rename your properties, the actions/conditions/expressions will be updated automatically. +Properties won't be usable from outside of the behavior. Properties are said to be "private", they can only be manipulated by the behavior. If you want to let extension users modifying them from the scene events, you can generate an action and a condition from the drop-down menu of the property. -These actions/conditions/expressions won't be usable from outside of the behavior. Properties are said to be "private", they can only be manipulated by the behavior. If you want to let extension user modifying them from the scene events, you can generate an action and a condition from the drop-down menu of the property. +![](generate-property-accessors.png) ## Behaviors using other behaviors as properties @@ -164,6 +161,18 @@ If you create a behavior and want to use this, just go to the properties of this To use a behavior based on another, you don't need to do anything special! Just add it to your object as usual: any missing behavior will be added to your object, so you can start using it immediately. +## Write behaviors dedicated to your project + +You probably used extensions from the community. These extensions aim to be usable in many projects, but extensions can also be created with only one project in mind. + +The [platformer template](https://gdevelop.io/game-example/free/platformer) has a `Enemy` extension with a `MonsterEnemy` behavior. This behavior toggle between +- a **Fire** state where it hurts the player +- a **NoFire** state where the player can jump on it + +![](platformer-enemy-extension.png) + +The [Bomberman-like template](https://gdevelop.io/game-example/free/3d-bomber-bunny) shows how a game can be organized into extensions. The players, bombs and bonuses have their own custom behavior. It allows to gather the logic of each object in one place: their extension. The main events can then focus on how these objects interact with each other. + ## A word about the advantages of behaviors Creating your own behaviors has multiple advantages: @@ -178,6 +187,11 @@ Creating your own behaviors has multiple advantages: ## Examples/ideas for custom behaviors +Installing and opening existing extensions can be a good way to see how behaviors work. The following extensions are simple enough not to feel lost when looking at their events: + +- [Animated back and forth movement](https://wiki.gdevelop.io/gdevelop5/extensions/animated-back-and-forth-movement/) +- [Flash object](https://wiki.gdevelop.io/gdevelop5/extensions/flash/) + You can imagine tons of different behaviors. Here are a few examples: * A behavior to **manage the health of an enemy or a boss**. diff --git a/docs/gdevelop5/behaviors/events-based-behaviors/platformer-enemy-extension.png b/docs/gdevelop5/behaviors/events-based-behaviors/platformer-enemy-extension.png new file mode 100644 index 0000000000..6522229ffa Binary files /dev/null and b/docs/gdevelop5/behaviors/events-based-behaviors/platformer-enemy-extension.png differ diff --git a/docs/gdevelop5/events/functions/empty-extension.png b/docs/gdevelop5/events/functions/empty-extension.png new file mode 100644 index 0000000000..f0eb20596a Binary files /dev/null and b/docs/gdevelop5/events/functions/empty-extension.png differ diff --git a/docs/gdevelop5/events/functions/index.md b/docs/gdevelop5/events/functions/index.md index c774d97480..4d45c15a3c 100644 --- a/docs/gdevelop5/events/functions/index.md +++ b/docs/gdevelop5/events/functions/index.md @@ -25,9 +25,9 @@ Functions are grouped into [extensions](/gdevelop5/extensions). They are the sam Click on **Create or search for new extensions** at the bottom. Then, select **Create a new extension** to [create a new extension](/gdevelop5/extensions/create). -By default, extensions don't have any function. Add one by clicking on **Add a new function** on the left. +By default, extensions don't have any function. Add one by clicking on the "+" button on the left next to **Functions**. -![](pasted/20221118-093435.png) +![](empty-extension.png) A new function is added, it's time to give it a name. By convention: @@ -79,9 +79,11 @@ Conditions and actions from behaviors can also be used in functions events. For #### Use parameter values -A number, string and boolean parameter values can be compared with conditions. +Number, string and boolean parameters can be compared with the same conditions as global and scene [variables](/gdevelop5/all-features/variables). -![](functionn-parameter-condition.png) +!!! note + + Parameter values can't be changed with actions. If you want to give back a value to scene events, you have to create a [custom expression](#return-a-value-from-a-function). Parameters can also be used directly in expressions by writing their name. For instance, a parameter called "ScoreToMultiply" can be used the following: `2 * ScoreToMultiply`. @@ -109,12 +111,11 @@ Function that are conditions or expressions must return a value. The returned va ### Use variables from function events -Variable can be useful within functions for intermediary results or to keep a state in the extension. -From function events, expressions must be used to access variable values: +Variables can be useful within functions for intermediary results or to keep a state in the extension. You can declare new [variables](/gdevelop5/all-features/variables) that are accessible only inside the extension by clicking on **Extension global variables** or **Extension scene variables**. -- `GlobalVariable(MyVariable)` for global variables -- `Variable(MyVariable)` for scene variables -- `MyObject.Variable(MyVariable)` for object variables +!!! tip + + If you feel the need to modify object variables, there is a good chance that you should rather make a [custom behavior](/gdevelop5/behaviors/events-based-behaviors/) and use properties. ## Use functions in events @@ -122,6 +123,18 @@ Extension functions can be found in conditions and actions lists like any other ![](pasted/20221118-094110.png) +## Write functions dedicated to your project + +You probably used extensions from the community. These extensions aim to be usable in many projects, but extensions can also be created with only one project in mind. + +The [platformer template](https://gdevelop.io/game-example/free/platformer) has a `Player` extension with a few functions. For instance, the function `AnimateFallingIntoPortal` uses a few actions to make an animation that is played at the end of the level. + +![](platformer-player-extension.png) + +Visual and sound effects can quickly take a lot of space. Making small functions like this allow to keep the scene events easy to follow. + +![](platformer-player-extension-usage.png) + ## Advanced usages This page gave a basic overview of what functions are. They are one of the more powerful features of GDevelop, as you can extend the events by using them, enabling to create very readable and concise events sheets. By using them, you can reduce the amounts of events that you write for your game, avoid copy-pasting them and even reduce bugs by ensuring that functions are always used for common tasks on your objects. diff --git a/docs/gdevelop5/events/functions/platformer-player-extension-usage.png b/docs/gdevelop5/events/functions/platformer-player-extension-usage.png new file mode 100644 index 0000000000..0e27960482 Binary files /dev/null and b/docs/gdevelop5/events/functions/platformer-player-extension-usage.png differ diff --git a/docs/gdevelop5/events/functions/platformer-player-extension.png b/docs/gdevelop5/events/functions/platformer-player-extension.png new file mode 100644 index 0000000000..451f6ac33d Binary files /dev/null and b/docs/gdevelop5/events/functions/platformer-player-extension.png differ diff --git a/docs/gdevelop5/events/functions/pasted/20221118-093435.png b/docs/gdevelop5/events/functions/unused/20221118-093435.png similarity index 100% rename from docs/gdevelop5/events/functions/pasted/20221118-093435.png rename to docs/gdevelop5/events/functions/unused/20221118-093435.png diff --git a/docs/gdevelop5/events/functions/functionn-parameter-condition.png b/docs/gdevelop5/events/functions/unused/functionn-parameter-condition.png similarity index 100% rename from docs/gdevelop5/events/functions/functionn-parameter-condition.png rename to docs/gdevelop5/events/functions/unused/functionn-parameter-condition.png