Could not find type "Player" in the current scope

Hi, when I try to do the body is Player check, it gives me an error saying “Player” is not in scope. I’ve tried using a lower case player and it returns "player is a variable but does not contain a type.

My full code below:

extends Node2D

var player = null
@onready var startPos = $StartScene

@onready var exit = $Exit

func _ready():
	player = get_tree().get_first_node_in_group("player")
	var traps = get_tree().get_nodes_in_group("Traps")
	for trap in traps:
		trap.touched_player.connect(_on_trap_touched_player)
	exit.body_entered.connect(_on_exit_body_entered)

func _process(delta):
	if Input.is_action_just_pressed("quit"):
		get_tree().quit()
	elif Input.is_action_just_pressed("reset"):
		get_tree().reload_current_scene()
	


func _on_deathzone_body_entered(body):
	reset_player()
	
func _on_trap_touched_player():
	reset_player()
	
func reset_player():
	player.velocity = Vector2.ZERO
	player.global_position = startPos.get_spawn_pos()

func _on_exit_body_entered(body):
	if body is player:
		exit.animate()

You might be missing a class name definition of class_name Player at the top of your Player script (should go just below extends). The is keyword is looking for a class type, and your error suggests you either don’t have one defined in your Player script, or possibly that a spelling/case-sensitive error is causing it to check for the wrong definition.

Because this is a learning project and is very useful for validation checks like this one, I would definitely recommend you try to get it to work this way; however, if it doesn’t work and you just want to move on for now, you can replace if body is player with if body.is_in_group("player") and it should do the same thing in this case.

Hmm I worked around that because I get the following error when I try to add the class_name:

Parser Error: Class “Player” hides global script class

Full player script here:

extends CharacterBody2D
class_name Player
@onready var animation = $AnimatedSprite2D

@export var speed = 300
@export var fall = 300
@export var jump_power = 200

func _physics_process(delta):
	
	if is_on_floor() == false:
		velocity.y += fall*delta
	
	var direction = Input.get_axis("move_left", "move_right")
	
	velocity.x = direction*speed
	
	if direction != 0:
		animation.flip_h = direction == -1
	
	if Input.is_action_just_pressed("jump") && is_on_floor():
		jump(jump_power)
	
	anim_handler(direction)
	move_and_slide()

func jump(force):
	velocity.y = -force
	
func anim_handler(direction):
	if is_on_floor():
		if direction == 0:
			animation.play("idle")
		else:
			animation.play ("run")
	else:
		if velocity.y < 0:
			animation.play("jump")
		else:
			animation.play("fall")
		

Ah, I’ve seen that before. There are several things it can be. Have you renamed any files at any point in this project?

Potentially, though I can’t remember :sweat_smile:

if you have a quick look at the filesystem window in godot, do you have two scripts folders by any chance?

normally if it says that a class is hidden, its if its declared somewhere else as well. but if its only declared once, then having a look at this forum thread might help shed some light on it.

what you could try in the first instance, is to go into a script, then press Ctrl+Shift+F.
this will bring up the Find in Files box and you can search through all the scripts for any instances of the declaration for class_name for the Player and see if theres a duplicate.
image

it will show the results in the output window at the bottom that you can click on and take you directly to the script in question.

if that doesnt show any duplicates, then there is a thread that might be an alternative explanation.
it is a bit of a long thread, but im wondering if you may be experiencing the same.

Godot 4: Error at (2, 12) Class Player hides a global script class - Other Courses / Ask - GameDev.tv

2 Likes

Oh gosh, yeah, didn’t even think of that one anymore. I was about to write a short checklist of things to look at XD

Anyway, this problem is a little weird, because it usually has something to do with Godot caching a reference, and then that reference being broken in one way or another.

  • Renaming files with a class definition (especially in the OS instead of the Godot file explorer) is one way this can happen.
  • Moving / copying files also seems to have a small chance of doing this.
  • As @OboShape said, having multiple folders or definitions that might conflict in some way.

Things you can try in addition to Find In Files as @OboShape has shown:

  • Disconnect your player script from the scene, then reconnect it by dragging it back over the root node.
  • De-list your project from the Godot project list, then import it again
  • With your project closed, in your project folder, delete the .godot folder (this will regenerate when you open the project again). If you feel unsure, just cut and paste it into a temporary folder instead of deleting it.
  • Your case probably has nothing to do with Autoloads, but for the sake of completeness, make sure the player scene isn’t a global Autoload. Apparently that conflicts with class_name
2 Likes

Hi everyone,

I will give it a shot. It is a bit confusing because I am still new.

Thank you!

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

Privacy & Terms