0

Why when I run it to my app has been closed and write it - Nonexisten function apply_impulse in base Node2D. I see Godot's doc and I find it method and your parent is RigidBidy2D, but I do this code by the video and he use KinematicBody2D and it is alright

extends KinematicBody2D

var movespeed = 500 
var bulletspeed = 2000
var bullet = preload("res://Bullet.tscn") 

func _ready():
    pass 

func _physics_process(delta):
    var motion = Vector2()
    
    if Input.is_action_pressed('up'):
        motion.y -= 1
    if Input.is_action_pressed('down'):
        motion.y += 1
    if Input.is_action_pressed('right'):
        motion.x += 1
    if Input.is_action_pressed('left'):
        motion.x -= 1
    
    motion = motion.normalized() 
    motion = move_and_slide(motion * movespeed) 
    
    look_at(get_global_mouse_position())

    if Input.is_action_just_pressed('LMB'):
        fire()

func fire():
    var bullet_instance = bullet.instance()
    bullet_instance.position = get_global_position()
    bullet_instance.rotation_degrees = rotation_degrees
    bullet_instance.apply_impulse(Vector2(), Vector2(bulletspeed, 0).rotated(rotation))
    get_tree().get_root().call_deferred("add_child", bullet_instance)
Theraot
  • 31,890
  • 5
  • 57
  • 86

1 Answers1

0

As you have found apply_impulse exists in RigidBody (or RigidBody2D).

Your presented code uses apply_impulse, here:

bullet_instance.apply_impulse(Vector2(), Vector2(bulletspeed, 0).rotated(rotation))

If that works, it implies that bullet_instance must be a RigidBody (or RigidBody2D). Let us see where do you define bullet_instance:

var bullet_instance = bullet.instance()

This means that bullet_instance is an instance of the PackedScene you refer as bullet. Well, that scene must be a RigidBody (or RigidBody2D). Let us see where do you define bullet:

var bullet = preload("res://Bullet.tscn") 

Thus, "res://Bullet.tscn" must be a RigidBody (or RigidBody2D). More precisely, the root node of that scene must be a RigidBody (or RigidBody2D).


On the other hand, if this line (or some other similar line) does not work:

bullet_instance.apply_impulse(Vector2(), Vector2(bulletspeed, 0).rotated(rotation))

And you are getting the error "Nonexistent function 'apply_impulse' in base Node2D", then it means that your are calling apply_impulse on a Node2D but not RigidBody (or RigidBody2D).

In this case it would mean that bullet_instance is an instance of Node2D but not RigidBody (or RigidBody2D). And then the same process as before would lead us to the root node of "res://Bullet.tscn" being an instance of Node2D but not RigidBody (or RigidBody2D).


Thus, double check "res://Bullet.tscn". The type of the root node must be RigidBody (or RigidBody2D) for apply_impulse to work on it.


To be clear, if you call apply_impuse like this:

apply_impulse(Vector2(), Vector2(bulletspeed, 0).rotated(rotation))

Or like this:

self.apply_impulse(Vector2(), Vector2(bulletspeed, 0).rotated(rotation))

Then you are calling it on the node that has the script where that is written (which is a KinematicBody2D in this case, which does not have apply_impulse, and thus that would not work).


To reiterate, when you write object.function(...), you are calling function on that object. If you write self.function(...), then you are calling function on the object that has the script where you write that. If you write simply function(...), that is the same as self.function(...).

Just in case, I'll also mention that the above applies if you use $. For example $object.function(...) would be calling function on a child node called "object".

And for that code to work, the object on which you call the function must have it.

Theraot
  • 31,890
  • 5
  • 57
  • 86