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 [2019/05/11 14:23]
ddabrahim Added Collaboration to the list of advantages and improved(?) explanation of the other two..
gdevelop5:behaviors:events-based-behaviors [2019/06/25 00:04] (current)
4ian [Use actions and conditions to manipulate the properties]
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&​400 ​|}}+{{ :​gdevelop5:​events:​project-manager-functions-extensions.png?​nolink |}}
  
 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.
Line 19: Line 19:
 {{ :​gdevelop5:​behaviors:​add-events-based-behavior.png?​nolink |}} {{ :​gdevelop5:​behaviors:​add-events-based-behavior.png?​nolink |}}
  
-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`.
  
-You should also edit the properties ​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:​setup-destructible-behavior.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.
  
 ### Add functions to your behavior ### Add functions to your behavior
  
-Click on the behavior in the list to see the list of functions composing the behavior. It will appear ​in 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:​empty-behavior-functions-list.png?​nolink |}}
Line 40: Line 42:
  
 * **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. Typicallyin most games this is done 60 times per second. This is called for **every single object** having the behaviorif 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 gamesthis 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. * **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.
 * **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.
-* **onStepPostEvents**:​ events that will be run for every single object having the behavior, **after** the "​traditional"​ events of the scene, ​ if the behavior is not deactivated.. Typicallyin most games this is done 60 times per second. As possible, we recommend to prefer onStepPreEvents,​ to run your logic before events and give the events in the scene a chance to react to changes that happen on the objects.+* **onStepPostEvents**:​ events that will be run for every single object having the behavior, **after** the "​traditional"​ events of the scene, ​ if the behavior is not deactivated. Typically in most gamesthis is done 60 times per second. As possible, we recommend to prefer onStepPreEvents,​ to run your logic before events and give the events in the scene a chance to react to changes that happen on the objects.
  
-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:​behavior-lifecycle-method-example.png?​nolink |}}
Line 82: Line 84:
 ### 2) Use actions/​conditions/​expressions ### 2) Use actions/​conditions/​expressions
  
-Like any other "builtin ​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:​custom-behavior-action.png?​nolink |}}
Line 89: Line 91:
 {{ :​gdevelop5:​behaviors:​custom-behavior-action-events-sheet.png?​nolink |}} {{ :​gdevelop5:​behaviors:​custom-behavior-action-events-sheet.png?​nolink |}}
  
-As you can see, this allows ​to create ​very expressive and easy-to-read events. You can then make the behavior more complex or reuse it on other objects.+As you can see, this allows ​creating ​very expressive and easy-to-read events. You can then make the behavior more complex or reuse it on other objects. 
 + 
 +## Add and use properties in a behavior 
 + 
 +What is interesting with behaviors is that they can hold information inside of them, much like variables of an object. These information are called properties. They can be a number, a string (text) or a boolean (which is like a virtual checkbox, either checked or unchecked).  
 + 
 +You can use these properties to make your behavior customizable (for example, you can have the speed, an amount of damage, the life points, the power points, the mana, etc... as properties of your behavior - depending on what your behavior is doing).  
 + 
 +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 |}} 
 + 
 +Properties can be: 
 + 
 +* Number, string or booleans (will be displayed as a checkbox) 
 +* 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. 
 +* Finally, they have a label that will be displayed in the editor. 
 + 
 +### 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. 
 + 
 +<​note>​If you rename your properties, the actions/​conditions/​expressions will be updated automatically.</​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>​
  
 ## A word about the advantages of behaviors ## A word about the advantages of behaviors
Line 95: Line 123:
 Creating your own behaviors has multiple advantages: Creating your own behaviors has multiple advantages:
  
-* *Encapsulation*:​ by adding logic inside a custom behavior, you can keep a clear events sheet, with less events so it's easier to read and maintain. Behaviors can have actions/​conditions and expressions that run logic, so you don't have to repeat long instructions in the events sheet. You can just use the action/​condition the behavior providing ​like you would normally do with any other behavior. +* *Encapsulation*:​ by adding logic inside a custom behavior, you can keep a clear events sheet, with fewer events so it's easier to read and maintain. Behaviors can have actions/​conditions and expressions that run logic, so you don't have to repeat long instructions in the events sheet. You can just use the action/​condition the behavior providing ​as you would normally do with any other behavior. 
-* *Reusability*:​ let's say that you created a behavior to have an enemy move and lose life if hit by the player. If you want to create a slightly different enemy, but based on the same principles, you can create a new object, then add the same behavior with different properties. In a few clicks, you can have a new enemy in your game. +* *Reusability*:​ let's say that you created a behavior to have an enemy move and lose life if hit by the player. If you want to create a slightly different enemy, but based on the same principles, you can create a new object, then add the same behavior with different properties. In a few clicks, you can have a new enemy in your game. 
-* *Collaboration*:​ let's say you are working in a team or would like to make your project open-source and invite people to contribute with new features and gameplay. Custom behavior ​make it possible for anyone to design and create self contained features and game logic that we can easily import ​in to a project and used by anyone without prior knowledge required including level and game designers who don't normally work on game logic or at least prefer not to.+* *Collaboration*:​ let's say you are working in a team or would like to make your project open-source and invite people to contribute with new features and gameplay. Custom behavior ​makes it possible for anyone to design and create self-contained features and game logic that we can easily import ​into a project and used by anyone without prior knowledge required including level and game designers who don't normally work on game logic or at least prefer not to.
  
 <​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>​