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@onrShootingAnimationPlayer
@onready var hit_animator : AnimationPlayer = Hand
@onready var pistol : Sprite2D = Hand/Pivot/Pistol/PistolBulletMarker
@onready var attacking: bool = false
@export var camera: Camera2D
Assistant
It seems that you are encountering an issue where get_global_mouse_position() - global_position results in the error "get index 'global_position' (on base: 'null instance')". This error is typically raised in Godot when you attempt to access a property or method on an instance that has been freed from memory or not yet assigned (thus, "null instance").
The field does not exist in your class explicitly, but Godot's class (from which is derived and hence your class) provides a property called to get/set the global position of a Node2D in the scene.
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()
# 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)
global_position
Player25D
Node2D
Sprite2D
Player25D
global_position
To troubleshoot this issue, ensure the following points:
Make sure you are not accidentally freeing the Player25D instance anywhere before you try to access global_position. Objects can sometimes be freed prematurely without intention due to a variety of reasons, such as calling queue_free() or free().
Confirm that Player25D is properly added to the scene tree. The global_position property is only accessible when the node is part of the active scene tree.
Consider checking for null before accessing global_position to avoid the error. For example:
var mouse_position : Vector2
if is_instance_valid(self): # Make sure the instance isn't null
mouse_position = (get_global_mouse_position() - global_position).normalized()
# Proceed with mouse_position
It's possible that the error is being triggered from a different part of the code, so ensure that there are no other places where global_position is being accessed before the instance is ready or after it has been freed.
If the Player25D instance is supposed to be instantiated dynamically, make sure the instance is created with player25D_instance = Player25D.instance() and properly added to the scene with add_child(player25D_instance), before attempting to access global_position.
If the error persists after reviewing these points, consider isolating the issue by debugging smaller portions of the code or sharing more of the context where the error occurs, which might include how and where the Player25D class is instantiated and used within your project.