Invalid get index 'global_position' (on base: 'null instance').
extends Sprite2D
class_name Player25D
enum State { IDLE, RUN, JUMP, ATTACK }
var state = State.IDLE
var velocity: Vector2 = Vector2.ZERO # Declare velocity at the start of the class
var bullets_amount: int = 30
var attack_animation: String = "Attack1"
var player_base = PlayerBase.new()# Use PlayerBase’s set_health method¨
@export var movement_data : MovementData
@export var stats : Stats
References
@onready var animator : AnimatedSprite2D = AnimatedSprite2D@onShootingAnimationPlayer
@onready var hit_animator : AnimationPlayer = Hand
@onready var pistol : Sprite2D = Hand/Pivot/Pistol/PistolBulletMarker
@onready var attacking: bool = false
@export var camera: Camera2D
Assistant
The error message Invalid get index 'global_position' (on base: 'null instance') that you've mentioned indicates that you are attempting to access the global_position property of an object (most likely a Node2D or any of its derivatives) that is null (nonexistent or has been freed).
This can happen when the object you're trying to access has not been initialized, has been deleted, or if you're trying to access it before it's ready in the scene tree. Here are some places in your code where this kind of error might occur:
func _physics_process(delta):
var input_vector = Input.get_axis("move_left", "move_right")
process_movement_input(delta)
process_actions()
update_animation_state() # This function was incorrectly named in the previous version.
func apply_acceleration(input_vector, delta):
velocity.x = move_toward(velocity.x, movement_data.max_speed * input_vector, movement_data.acceleration * delta)
#$Hand/AttackArea/CollisionShape2D.disabled = true;
state = State.IDLE
func update_animation_state():
if state == State.ATTACK:
return
if velocity.x != 0:
animator.play("Run")
else:
animator.play("Idle")
func _on_hurtbox_area_entered(_area):
if not _area.is_in_group("Bullet"):
hit_animator.play("Hit")
take_damage(1) # If damage is to be variable, extract value from _area
EventManager.frame_freeze.emit()
func spawn_death_particle():
var death_particle = death_particle_load.instantiate()
death_particle.global_position = global_position
get_tree().current_scene.add_child(death_particle)
Override PlayerBase method
func take_damage(damage: int) -> void:
player_base.health -= damage # Use the health property from PlayerBase
if player_base.health <= 0:
die()
else:
AudioManager.play_sound(AudioManager.HURT)
func _on_swapPlane():
PlayerState.set_last_2d_position(global_position);
SceneChanger.change_to_2d(global_position) # Calls the singleton to change to 3D scene
Implement the move method required by PlayerBase, the actual movement
logic is in the _physics_process function above.
func move(input_dir: Vector2) -> void:
# For Player2D, the move function is handled by process_movement_input,
# so no additional logic is needed here.
pass
Implement the attack method required by PlayerBase. This is a placeholder
and the actual logic for starting an attack is in the perform_attack function.
func attack() -> void:
perform_attack()
Ensure that the scenes have been loaded correctly.
When you instantiate muzzle_load and bullet_load, make sure that the paths are correct and that the scenes exist at those locations.
Ensure that you're not trying to instantiate or use them before the _ready() function has been called.
Check for accessing global_position before the node is ready.
Make sure all the nodes referenced by get_global_mouse_position() and global_position are initialized and in the scene tree before you try to access their properties.
Here's how to troubleshoot and fix this issue:
Place a breakpoint or print statement in the shoot() function where you are using global_position to confirm that the instance exists at the time it's being accessed.
If self is null, it means that the script's owner has been freed or is not yet ready in the scene tree.
Double-check your scene tree to ensure that the node the script is attached to is properly instantiated before you try to access its global_position.
Make sure that the die() function isn't being called prematurely, resulting in the node being freed before you're done with it.
Use safe navigation to ensure that the object is not null before accessing its properties:
if not is_instance_valid(bullet):
print("Bullet instance is invalid or has been freed.")
Ensure that the instance you're working with is valid before accessing its properties or methods.
Remember that if you asynchronously free a node (e.g., with queue_free()) and then attempt to access it afterward, you'll also run into this issue. Make sure to manage the lifecycle of your nodes correctly and avoid accessing them after they've been freed.