Differences

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:behaviors:events-based-behaviors [2020/12/26 13:56]
127.0.0.1 external edit
gdevelop5:behaviors:events-based-behaviors [2021/09/07 00:18] (current)
4ian [A word about the advantages of behaviors]
Line 11: Line 11:
 Behaviors are grouped into "extensions". They work similarly to [[gdevelop5:extending-gdevelop|extensions that you can write]] for GDevelop, except that they are entirely composed of functions or behaviors powered by events. You can see the list of all the extensions, the game has in the [[gdevelop5:interface:project-manager|Project Manager]], as well as add a new extension: Behaviors are grouped into "extensions". They work similarly to [[gdevelop5:extending-gdevelop|extensions that you can write]] for GDevelop, except that they are entirely composed of functions or behaviors powered by events. You can see the list of all the extensions, the game has in the [[gdevelop5:interface:project-manager|Project Manager]], as well as add a new extension:
  
-{{ :gdevelop5:events:project-manager-functions-extensions.png?nolink |}}+{{ :gdevelop5:behaviors:events-based-behaviors:pasted:20210906-234510.png?nolink&400 }}
  
-Click on the "+button at the bottom to create a new extension. You can right click on it to rename it. It's a good idea to have functions and behaviors related to the same thing in a single extension.+Click on the **+** button at the bottom to create a new extension. You can right click on it to rename it. It's a good idea to have functions and behaviors related to the same thing in a single extension.
  
-Click on the extension in the list to open its editor. By default, there are no functions in the extensions. Add one by clicking on "Add a new behavior", on the left:+Click on the extension in the list to open its editor. By default, there are no functions in the extensions. Add one by clicking on **Add a new behavior**, on the left:
  
-{{ :gdevelop5:behaviors:add-events-based-behavior.png?nolink |}}+{{ :gdevelop5:behaviors:events-based-behaviors:pasted:20210906-234614.png?nolink }}
  
 The new behavior is added. You should rename it to give it a name according to what you want it to do. For example, if your behavior will be used to have an enemy move left and right on a platform, you can call it `HorizontalMovement` (only alphanumeric characters and underscores are allowed in names). If your behavior is automatically tracking damage and deletes objects that are too damaged, you could call it `Destructible`. The new behavior is added. You should rename it to give it a name according to what you want it to do. For example, if your behavior will be used to have an enemy move left and right on a platform, you can call it `HorizontalMovement` (only alphanumeric characters and underscores are allowed in names). If your behavior is automatically tracking damage and deletes objects that are too damaged, you could call it `Destructible`.
Line 23: Line 23:
 You should also edit the configuration of the behavior by doing a right click on it and choosing "Properties". You can enter the description, the name displayed in the editor and specify if it should be restricted to some kind of objects: You should also edit the configuration of the behavior by doing a right click on it and choosing "Properties". You can enter the description, the name displayed in the editor and specify if it should be restricted to some kind of objects:
  
-{{ :gdevelop5:behaviors:setup-destructible-behavior.png?nolink |}}+{{ :gdevelop5:behaviors:events-based-behaviors:pasted:20210906-234834.png?nolink }}
  
 Behaviors can also embed properties, which are number, strings or booleans that are stored inside the behavior. Check out the section below about how to add and use properties. Behaviors can also embed properties, which are number, strings or booleans that are stored inside the behavior. Check out the section below about how to add and use properties.
Line 31: Line 31:
 Click on the behavior in the list to see the list of functions composing the behavior. It will appear on the right side panel. Click on the behavior in the list to see the list of functions composing the behavior. It will appear on the right side panel.
  
-{{ :gdevelop5:behaviors:empty-behavior-functions-list.png?nolink |}}+{{ :gdevelop5:behaviors:events-based-behaviors:pasted:20210906-235104.png?nolink&300 }}
  
-By default, a behavior is empty and **does nothing**. To add interactivity, you can add a new function (also called "method"). You'll be given the choice between some predefined functions, called lifecycle methods, or a custom function. Lifecycle methods will be called by the game engine automatically at some key moment during the game. Custom functions will be available as conditions or actions (like [[gdevelop5:events:functions|usual functions]]).+By default, a behavior is empty and **does nothing**. To add interactivity, you can add a new function (also called "method"). You'll be given the choice between some predefined functions, called *lifecycle methods*, or a custom function. Lifecycle methods will be called by the game engine automatically at some key moment during the game. Custom functions will be available as conditionsactions or expressions (like [[gdevelop5:events:functions|usual functions outside behaviors]]).
  
-{{ :gdevelop5:behaviors:behavior-choose-method.png?nolink |}}+{{ :gdevelop5:behaviors:events-based-behaviors:pasted:20210906-235126.png?nolink }}
  
 ### Lifecycle methods ### Lifecycle methods
Line 43: Line 43:
 * **onCreated**: events inside the function will be run when **a new instance of an object** with the behavior is created. * **onCreated**: events inside the function will be run when **a new instance of an object** with the behavior is created.
 * **onStepPreEvents**: will be run every time a frame is rendering on the screen **before the "traditional" events** of the scene. Typically in most games, this is done 60 times per second. This is called for **every single object** having the behavior if the behavior is not deactivated. * **onStepPreEvents**: will be run every time a frame is rendering on the screen **before the "traditional" events** of the scene. Typically in most games, this is done 60 times per second. This is called for **every single object** having the behavior if the behavior is not deactivated.
-* **onOwnerRemovedFromScene**: events that will be run when an instance of the object having the behavior is **deleted** (if multiple objects are removed at the same time, events will be run for each object). This is done even if the behavior is deactivated.+* **onDestroy**: events that will be run when an instance of the object having the behavior is **deleted** (if multiple objects are removed at the same time, events will be run for each object). This is done even if the behavior is deactivated.
 * **onDeActivate**: events that will be run once, after the behavior is deactivated on an object. * **onDeActivate**: events that will be run once, after the behavior is deactivated on an object.
 * **onActivate**: events that will be run once, after the behavior is activated again on an object. * **onActivate**: events that will be run once, after the behavior is activated again on an object.
Line 50: Line 50:
 This is an example of some events inside onStepPreEvents in the case of behavior that automatically deletes objects that are too damaged: This is an example of some events inside onStepPreEvents in the case of behavior that automatically deletes objects that are too damaged:
  
-{{ :gdevelop5:behaviors:behavior-lifecycle-method-example.png?nolink |}}+{{ :gdevelop5:behaviors:events-based-behaviors:pasted:20210906-235321.png?nolink }}
  
 ### Custom action, condition or expression ### Custom action, condition or expression
Line 58: Line 58:
 For example, this creates a new action that can be used to add damage to the object: For example, this creates a new action that can be used to add damage to the object:
  
-{{ :gdevelop5:behaviors:behavior-custom-function-example.png?nolink |}}+{{ :gdevelop5:behaviors:events-based-behaviors:pasted:20210906-235538.png?nolink }}
  
  
Line 70: Line 70:
 Here, you can see the "Destructible Object" behavior for example (that was created in the previous screenshots): Here, you can see the "Destructible Object" behavior for example (that was created in the previous screenshots):
  
-{{ :gdevelop5:behaviors:choose-custom-behavior-in-list.png?nolink |}}+{{ :gdevelop5:behaviors:events-based-behaviors:pasted:20210907-000055.png?nolink }}
  
 The custom behavior that you created is then displayed in the list of the behaviors attached to the object: The custom behavior that you created is then displayed in the list of the behaviors attached to the object:
  
-{{ :gdevelop5:behaviors:screenshot_2019-05-07_23.48.04.png?nolink |}}+{{:gdevelop5:behaviors:events-based-behaviors:pasted:20210907-000202.png}}
  
 You can run the game and see that the events in "doStepPreEvents" function (or "doStepPostEvents") will be executed. In the example of the Destructible behavior that was created, if the "Damage" variable is set to a number greater or equal to 100, the object will be destroyed automatically.  You can run the game and see that the events in "doStepPreEvents" function (or "doStepPostEvents") will be executed. In the example of the Destructible behavior that was created, if the "Damage" variable is set to a number greater or equal to 100, the object will be destroyed automatically. 
Line 86: Line 86:
 Like any other "built-in behavior", you can also use actions/conditions (or expressions) if you have declared functions as such in the behavior. For example, in the "Destructible" behavior, we created an action to cause some damage to the object. We can find it and use it in the events sheet: Like any other "built-in behavior", you can also use actions/conditions (or expressions) if you have declared functions as such in the behavior. For example, in the "Destructible" behavior, we created an action to cause some damage to the object. We can find it and use it in the events sheet:
  
-{{ :gdevelop5:behaviors:custom-behavior-action.png?nolink |}}+{{ :gdevelop5:behaviors:events-based-behaviors:pasted:20210907-000606.png?nolink }}
  
 It is displayed in the events sheet: It is displayed in the events sheet:
Line 101: Line 101:
 To show and add properties to your behavior, edit it and click on the "Properties" tab. In this example, you can see a behavior called "Health", that has properties to store the health of the object and the minimum time between two hits on the object: To show and add properties to your behavior, edit it and click on the "Properties" tab. In this example, you can see a behavior called "Health", that has properties to store the health of the object and the minimum time between two hits on the object:
  
-{{ :gdevelop5:behaviors:health-behavior-properties.png?nolink |}}+{{ :gdevelop5:behaviors:events-based-behaviors:pasted:20210907-001003.png?nolink }}
  
 Properties can be: Properties can be:
  
-* Number, string or booleans (will be displayed as a checkbox)+* Number, string (including a color or a choice from a list of options) or booleans (will be displayed as a checkbox).  
 +* They can also be another required behavior - in which case the behavior you're editing will ensure the other specified behavior is present on objects using it. There is a dedicated section later on this page about this.
 * They have a default value, that will be used if the value is not changed when adding the behavior to an object. * They have a default value, that will be used if the value is not changed when adding the behavior to an object.
 * They can be visible in the editor, when added to an object, or invisible. * They can be visible in the editor, when added to an object, or invisible.
Line 118: Line 119:
  
 <note tip>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 have them modified by the scene events, create new actions/conditions for this in your behavior.</note> <note tip>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 have them modified by the scene events, create new actions/conditions for this in your behavior.</note>
 +## Behaviors using other behaviors as properties
 +
 +It is possible for behaviors to use other "required behaviors" as properties. When this is the case, GDevelop will ensure that any object using your behavior will also use the other one.
 +You can then use the behavior entered as a property inside the actions, conditions, expressions and the lifecycle functions of your own behavior.
 +
 +<note tip>This is an advanced feature that is helpful to create behaviors that are based on other. For example, you could make a behavior called "Platformer enemy" that is using the "Platformer object" behavior and adding specific actions, conditions and logic to make an enemy chase the player.</note>
 +
 +If you create a behavior and want to use this, just go to the properties of this behavior and add a new property. Choose the type "Required Behavior" for this property. You can then use this new behavior in the events of the behavior you just edited.
 +
 +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.
  
 ## A word about the advantages of behaviors ## A word about the advantages of behaviors
Line 128: Line 139:
  
 <note>Encapsulation and reusability are core concepts in programming. In other game engines, like Unity, objects and behaviors might be named entities and components.</note> <note>Encapsulation and reusability are core concepts in programming. In other game engines, like Unity, objects and behaviors might be named entities and components.</note>
- 
-In the future, updates will also ease the *composability* of behaviors. 
  
 ## Examples/ideas for custom behaviors ## Examples/ideas for custom behaviors
Line 149: Line 158:
   * You can create a behavior that increases the "Score" variable when the object is destroyed. You can then create items, collectibles or enemies that have this behavior, and when they are collected or killed, the score will automatically increase.   * You can create a behavior that increases the "Score" variable when the object is destroyed. You can then create items, collectibles or enemies that have this behavior, and when they are collected or killed, the score will automatically increase.
  
-<note tip>If you want to see an example of how behaviors can be used in your gameread [[gdevelop5:tutorials:how-to-make-behavior|How to make a behavior for an object]]. It's a step by step explanation and a good guide about architecturing your game with behaviors.</note>+<note tip> [[gdevelop5:extensions|New extensions]] are being created by the community all the time!  If you are interested in helping with thisstart by looking at the [[https://trello.com/b/AftjL2v1/gdevelop-extensions|GDevelop Extension Team]] (on Trello) You can also start discussing your ideas for extensions on the [[https://discordapp.com/invite/JWcfHEB|GDevelop Discord]] channel named #extension.</note>