It looks like you've shared a script from a Godot game project that defines a 2D player character using the Godot Engine scripting language, GDScript. However, the title of your message suggests you're facing an error:
code
Invalid get index 'global_position' (on base: 'null instance').
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
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@onreadyvargunsanimator:AnimationPlayer=ShootingAnimationPlayer
@onready var hit_animator : AnimationPlayer = HitAnimationPlayer@onreadyvarhand:NHand
@onready var pistol : Sprite2D = Hand/Pivot/Pistol@onreadyvaHand/Pivot/Pistol/PistolBulletMarker
@onready var attacking: bool = false
@export var camera: Camera2D
Load Scenes
@onready var muzzle_load: PackedScene = preload("res://Scenes/Particles/muzzle.tscn")
@onready var bullet_load: PackedScene = preload("res://Scenes/Props/bullet.tscn")
@onready var death_particle_load: PackedScene = preload("res://Scenes/Particles/player_death_particle.tscn")
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
code
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()
code
# Adjust the jump method to use jump_strength from PlayerBase
func _on_health_changed(new_health: int) -> void:
player_base.health = new_health # Make sure to update it when health changes
# Handle health change (e.g., update UI)
This error typically means that you're trying to access the global_position property of an object that doesn't exist (i.e., a null reference). The context where this error might occur isn't entirely clear from the script you've pasted.
However, there are a couple of lines in your script where you access global_position: