0

I tried to create a scene that resembles a bullet for a platformer shooter in Godot. I have created a scene with the root node being an Area2D, with a collision, and a visibility notifier. The code in that root node goes like this:

extends Area2D

export var bullet_speed = 400
var motion = Vector2()

func start():
    position = Vector2()
    pass

func _physics_process(delta):
    motion.x = bullet_speed * delta
    translate(motion)


func _on_VisibilityNotifier_screen_exited():
    queue_free()
    pass # Replace with function body.

Then I have a scene which has the player scene in it. In the player scene, the root node is a KinematicBody2D, A Collision Shape, and a Position2D with no name changes

The script of a player (cut short) has a physics process delta with the following commands:

const bulletlaunch = preload("res://Sprites/Gun Animation/Bullet.tscn") ##Declared the bullet scene

func _physics_process(delta):
    Engine.target_fps = 60
    #Playable when NOT dead
    if is_dead == false:
        var x_input = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")

        hud_score_display.text = String(GlobalScore.score)
        score_display.text = String(GlobalScore.score)
        heart_hud.value = health

        if health >= 75 && health <= 100:
            heart_hud.tint_progress = Color.green
        if health >= 50 && health <= 74:
            heart_hud.tint_progress = Color.orange
        if health >= 25 && health <= 49:
            heart_hud.tint_progress = Color.red

        if x_input != 0:
            if x_input == -1:
                if playing_health_damage == false:
                    character.flip_h = true
                    character.play("Idle")
            if x_input == 1:
                if playing_health_damage == false:
                    character.flip_h = false
                    character.play("Idle")
            motion.x += x_input * acceleration * delta
            motion.x = clamp(motion.x, -max_speed, max_speed)
        else:
            if playing_health_damage == false:
                motion.x = lerp(motion.x, 0, air_resistance)
                character.play("Idle")

        motion.y += gravity * delta
        if is_on_floor():
            if x_input == 0:
                motion.x = lerp(motion.x, 0, friction)
            if Input.is_action_just_pressed("ui_up"):
                motion.y = -jump_force
                small_jump_sound.play()

        if Input.is_action_just_pressed("ui_action"): ##The place where I have the error
            var bulletinstance = bulletlaunch.instance()
            get_parent().add_child(bulletinstance)
            bulletinstance.position = $Position2D.global_position

        motion = move_and_slide(motion, Vector2.UP)
        pass

However I when I press the ui_action key, I get this error

Invalid get index 'global_position' (on base: 'null instance').

Which I don't really understand..

What I've tried:

  1. Changing the global_position to position in both of them and doing all the combinations
  2. Find another Position2D like replacement but I can't find anything like that
  3. use the print(playershootpos.position) on the if ui_action statement but I seem to get the same error. Weird.

Other question:

Are there any replacements for the Position2D node? And what is the issue?

  • What happens if you change `const bulletlaunch = preload("res://Sprites/Gun Animation/Bullet.tscn")` to `onready var bulletlaunch = preload("res://Sprites/Gun Animation/Bullet.tscn")`? – Christopher Bennett Mar 27 '20 at 20:05
  • @ChristopherBennett No use, I get the same null instance error – simonknowsstuff Mar 28 '20 at 01:53
  • Hmm... Strange. Null instance means the engine can't find what you're telling it to look for, either because it hasn't been preloaded/added properly yet, or occasionally, due to a spelling mistake or path error.. My only other thought for the moment is that you might try changing `get_parent().add_child(bulletinstance)` to simply `add_child(bulletinstance)`, but I'm uncertain of your node setup so I can't be sure if that's right. – Christopher Bennett Mar 28 '20 at 11:11
  • The error from the code posted is caused by `$Position2D` being null. Most likely is that `$Position2D` is not the correct [path to that node](https://docs.godotengine.org/en/3.2/classes/class_nodepath.html#description). Quick tip: you can drag and drop a node from the scene tree into the script editor to place the node's path at the cursor. – hola Mar 28 '20 at 20:26
  • I don't remember how but I actually fixed it. I did everything from scratch with everything the same and it did work. Probably there MUST have been a mistake somewhere. – simonknowsstuff Mar 29 '20 at 12:38
  • If you're trying to instance a bullet in front of your character, you can replace `bulletinstance.position = $Position2D.global_position` with `bulletinstance.transform.origin = self.transform.origin`. Additionally, you can add a `Vector3()` object to `self.transform.origin` to have it shifted in relation to the character's position. – Breno Teodoro Mar 31 '20 at 14:06

0 Answers0