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:events:js-code [2020/12/18 22:24]
tejucherry7515 [Read and change the value of a variable]
gdevelop5:events:js-code [2021/02/02 19:11] (current)
bouh [Use Javascript to get a value from parameters of a custom function or behavior]
Line 54: Line 54:
 </code> </code>
  
-See the documentation of [[https://docs.gdevelop-  +See the documentation of [[https://docs.gdevelop-app.com/GDJS%20Runtime%20Documentation/gdjs.Variable.html|gdjs.Variable]] and [[https://docs.gdevelop-app.com/GDJS%20Runtime%20Documentation/gdjs.VariablesContainer.html|gdjs.VariablesContainer]].
- app.com/GDJS%20Runtime%20Documentation/gdjs.Variable.html|gdjs.Variable]] and [[https://docs.gdevelop-app.com/GDJS%20Runtime%20Documentation/gdjs.VariablesContainer.html|gdjs.VariablesContainer]].+
  
 ### Move object at the position of another, check if the game is rendering the first frame ### Move object at the position of another, check if the game is rendering the first frame
Line 62: Line 61:
 const players = runtimeScene.getObjects("Player"); const players = runtimeScene.getObjects("Player");
 const playerHitBoxes = runtimeScene.getObjects("PlayerHitBox"); const playerHitBoxes = runtimeScene.getObjects("PlayerHitBox");
-//Check if one instance of objects exist, here if scene haven't one +// Stop early if for some reason the player or the hitbox object is not found.
-//of the two object on scene we avoid to compute rest of code.+
 if (playerHitBoxes.length === 0 || players.length === 0) return; if (playerHitBoxes.length === 0 || players.length === 0) return;
  
-//At the first frame+// At the first frame only (i.e: at the beginning of the scene).
 if (runtimeScene.getTimeManager().isFirstFrame()) { if (runtimeScene.getTimeManager().isFirstFrame()) {
-//Hide the first instance of playerHitBoxes+  // Hide the first instance of PlayerHitBoxes.
   playerHitBoxes[0].hide();   playerHitBoxes[0].hide();
 } }
  
-//Set the positions+// Set the position of the Player object.
 players[0].setX(playerHitBoxes[0].getX() - 12); players[0].setX(playerHitBoxes[0].getX() - 12);
 players[0].setY(playerHitBoxes[0].getY()); players[0].setY(playerHitBoxes[0].getY());
Line 113: Line 111:
 } }
  
-var LEFTKEY = 37; +const LEFTKEY = 37; 
-var RIGHTKEY = 39;+const RIGHTKEY = 39;
 if (runtimeScene.getGame().getInputManager().isKeyPressed(LEFTKEY)) { if (runtimeScene.getGame().getInputManager().isKeyPressed(LEFTKEY)) {
   player.flipX(true);   player.flipX(true);
Line 161: Line 159:
 The equivalent events would be: The equivalent events would be:
 {{ :gdevelop5:events:fade-out-and-behavior.png?nolink |}} {{ :gdevelop5:events:fade-out-and-behavior.png?nolink |}}
 +
 +### Use Javascript to get a value from parameters
 +
 +In Javascript, you can get the value from your function easily.
 +Here let see how to handle an object. Type is //Objects// as you can see //Objects// is plural, this means the values returned will be an array of instances of your object pass into parameters.
 +{{ :gdevelop5:events:function_rotate_parameters.png
 +?nolink |}}
 +
 +For get the array of instances we use this function:
 +
 +<code javascript>
 +eventsFunctionContext.getObjects("myParameter");
 +</code>
 +
 +Then in your case with a parameter named //RotateObjects// if we want rotate the object by 45 degrees we do
 +
 +
 +<code javascript>
 +const objects = eventsFunctionContext.getObjects("RotateObjects");
 +const firstInstance = objects[0];
 +firstInstance.setAngle(45);
 +</code>
 +
 +Let see now how to make the angle a parameter, angle is number typed.
 +
 +{{ :gdevelop5:events:degrees_angle_function.png
 +?nolink |}}
 +
 +<code javascript>
 +const angle = eventsFunctionContext.getArgument("degrees");
 +</code>
 +
 +The same code can be used for String type and others.
 +
 +If you see 
 +<code>
 +GetArgumentAsString()
 +GetArgumentAsNumber()
 +</code>
 +These are two expressions, these are not used in Javascript.
 +
 +
 +
 +
 +
 +
 +
 +