0

I am trying to use the FireBullet behaviour using JavaScript in gdevelop5. I added the following code to implement that:

    var player = runtimeScene.getObjects("Player")
    let fireBullet = player[0].getBehavior('FireBullet')
    fireBullet.FireBullet()

The FireBullet function does not work on the behaviour. On debugging I found that the variable fireBullet is not undefined, and contains the behaviour as it’s value, but I don’t know which function to use to get the same result as the following image: image
image
1193×48 6.49 KB

The value of firebullet is found to be [object Object] in the debugger. I can't find a way to get the exact object, but it confirms that the behaviour is present.

Any help telling how to fire a bullet using the behaviour from js will be greatly appreciated. Thanks!

Prateek p
  • 117
  • 1
  • 11

1 Answers1

0

FireBullet() isn't a method associated with fireBullet object. getBehavior() returns an object of type RuntimeBehavior. activate() method can be used to activate a behavior associated with this class.

So a solution could be:

let player = runtimeScene.getObjects("Player")
let fireBullet = player[0].getBehavior('FireBullet')
fireBullet.activate(true)

Similarly, you can check out other methods of RuntimeBehavior and its child classes. This question has helpful answers on how to find what class the corresponding object belongs to.

Hope this helps.

Aastha Bist
  • 324
  • 2
  • 11
  • So fireBullet.activate activates the fire bullet method/function in the behaviour or just activates the behaviour ? If it only activates the behaviour, which it seems to because it does not fire a bullet, how do I actually run the fireBullet function in the behaviour and fire a bullet? The fireBullet behaviour has multiple functions, like fireBullet(), fireBulletTowardsPosition and so on, so how can I run those? Also is there any documentation for javascript in gdevelop? – Prateek p Aug 18 '22 at 04:06
  • The docs say it just activates the behavior. The closest thing I could find to js docs for gdevelop were these: https://wiki.gdevelop.io/gdevelop5/events/js-code, https://docs.gdevelop.io/GDJS%20Runtime%20Documentation/. You can check different method names associated with fireBullet, or go through RuntimeBehavior methods to figure out how to find the methods you are looking for. – Aastha Bist Aug 18 '22 at 17:20