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:
- Changing the global_position to position in both of them and doing all the combinations
- Find another Position2D like replacement but I can't find anything like that
- 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?