Cannot call non-static function

Godot Version

4.1

Question

im making a clone of a game but i got an error from a non-static function:

the function is pause_ghost() in await points_manager.pause_ghost(), and the error says Line 166:Cannot call non-static function “pause_ghost()” on the class “PointsManager” directly. Make an instance instead.

these are the relating pieces of code incase you need it to fix it:

the code where the error is in:

extends Area2D

class_name Ghost

enum GhostState {
	SCATTER,
	CHASE,
	RUN_AWAY,
	EATEN
}

signal direction_change(current_direction: String)

var current_scatter_index = 0
var direction = null
var current_state: GhostState
var is_blinking = false


@export var speed = 65
@export var movement_targets: Resource
@export var tile_map: MazeTileMap
@export var image: CompressedTexture2D
@export var chasing_target: Node2D
@export var points_manager = PointsManager
@onready var update_chasing_target_position_timer = $updateChasingTargetPositionTimer
@onready var body_sprite = $theghost as BodySprite
@onready var eyes_sprite = $ghosteyes as EyesSprite
@onready var navigation_area_2d = $NavigationAgent2D
@onready var scatter_timer = $scatterTimer
@onready var run_away_timer = $runAwayTimer
@onready var points_label = $pointsLabel

func _ready():
	navigation_area_2d.path_desired_distance = 4.0
	navigation_area_2d.target_desired_distance = 4.0
	navigation_area_2d.target_reached.connect(on_position_reached)
	
	call_deferred("setup")
 



 
func _process(delta):
	if !run_away_timer.is_stopped() && run_away_timer.time_left < run_away_timer.wait_time / 4 && !is_blinking:
		start_blinking()
	move_ghost(navigation_area_2d.get_next_path_position(), delta)
 

func move_ghost(next_position: Vector2, delta: float):
	var current_ghost_position = global_position
	var new_velocity = (next_position - current_ghost_position).normalized() * speed * delta
	
	caculate_direction(new_velocity)
	position += new_velocity

func caculate_direction(new_velocity: Vector2):
	var current_direction
	
	if new_velocity.x > 1:
		current_direction = "right"
	elif new_velocity.x < -1:
		current_direction = "left"
	elif new_velocity.y > 1:
		current_direction = "up"
	elif new_velocity.y < -1:
		current_direction = "down"
		
	if current_direction != direction:
	
		direction = current_direction
		direction_change.emit(direction)


func setup():
	print(tile_map.get_navigation_map(0))
	navigation_area_2d.set_navigation_map(tile_map.get_navigation_map(0))
	NavigationServer2D.agent_set_map(navigation_area_2d.get_rid(), tile_map.get_navigation_map(0))
	scatter()
 
 
func scatter():
	scatter_timer.start()
	current_state = GhostState.SCATTER
	navigation_area_2d.target_position = movement_targets.scatter_targets[current_scatter_index].position
 
func on_position_reached():
	if current_state == GhostState.SCATTER:
		scatter_position_reached()
		speed = 65
	elif current_state == GhostState.CHASE:
		chase_position_reached()
		
	elif current_state == GhostState.RUN_AWAY:
		run_away_from_pacman()


func chase_position_reached():
	print("kill pacman")


func scatter_position_reached():
	if current_scatter_index < 3:
		current_scatter_index += 1
	else:
		current_scatter_index = 0
	navigation_area_2d.target_position = movement_targets.scatter_targets[current_scatter_index].position
	
	








func _on_scatter_timer_timeout():
	starting_chasing_pacman()

func starting_chasing_pacman():
	if chasing_target == null:
		print("no chasing target, chasing wont work obviously lol")
	current_state = GhostState.CHASE
	update_chasing_target_position_timer.start()
	navigation_area_2d.target_position = chasing_target.position


func _on_update_chasing_target_position_timer_timeout():
	navigation_area_2d.target_position = chasing_target.position


func run_away_from_pacman():
	if run_away_timer.is_stopped():
		body_sprite.run_away()
		eyes_sprite.hide_eyes()
		run_away_timer.start()
		speed = 60
	current_state = GhostState.RUN_AWAY
	
	# stop timers
	update_chasing_target_position_timer.stop()
	scatter_timer.stop()
	
	var empty_cell_position = tile_map.get_random_empty_cell_position()
	navigation_area_2d.target_position = empty_cell_position

func start_blinking():
	body_sprite.start_blinking()




func _on_run_away_timer_timeout():
	is_blinking = false
	eyes_sprite.show_eyes()
	body_sprite.move()
	starting_chasing_pacman()
	speed = 75
	

func get_eaten():
	body_sprite.hide()
	points_label.show()
	await points_manager.pause_ghost()
	points_label.hide()
	run_away_timer.stop()
	current_state = GhostState.EATEN
	


func _on_body_entered(body):
	var player = body as Player
	if current_state == GhostState.RUN_AWAY:
		get_eaten()
	elif current_state == GhostState.CHASE || current_state == GhostState.SCATTER:
		update_chasing_target_position_timer.stop()
		player.die()
		scatter_timer.wait_time = 600
		scatter()

the code where pause_ghost() is from:

extends Node

class_name PointsManager

var ghost_points = 0
var points = 0

func pause_ghost():
	points += ghost_points
	get_tree().paused = true
	await get_tree().create_timer(1.13).timeout
	get_tree().paused = false

thank you!

You probably want to add points manager as a global inside the project settings instead of using class_name

i am using class_name to add points manager tho

class_name makes the type global, which doesn’t contain an actual node or values. A Autoload/Global from the project settings is global and contains an actual node with values.

If you have a PointsManager node in your scene, then you need to change this line to include the type instead of the type-as-a-value situation you have.

# is type PointsManager (error)
@export var points_manager = PointsManager

# is of type PointsManager (make sure to set a value in the inspector)
@export var points_manager: PointsManager

alright

its working

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.