Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Class XXX hides a global script class" when trying to FileAccess.get_var a custom subclass of Resource #85369

Closed
moveonwasd opened this issue Nov 26, 2023 · 2 comments

Comments

@moveonwasd
Copy link

moveonwasd commented Nov 26, 2023

Godot version

4.1.2.stable

System information

Godot v4.1.2.stable - elementary OS 6.1 Jólnir - X11 - Vulkan (Mobile) - integrated AMD Radeon Vega 8 Graphics (RADV RAVEN) () - AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx (8 Threads)

Issue description

Getting "Parser Error: Class "WorkResource" hides a global script class" with stack frame "0 - gdscript://-9223371970299558721.gd:1 - at function:" when trying to FileAccess.get_var from "user://work_resource.save", which stores a WorkResource instance.
There is no other scripts nor autoloads of the same name.
No embedded scripts in .tscn files also.

data_saver_loader.gd (DataSaverLoader autoload):

extends Node


const USER_DIR: String = "user://"
const SAVE_EXT: String = ".save"


# Tested with this off - same results
func _ready():
	if OS.is_debug_build():
		for path in DirAccess.get_files_at(USER_DIR):
			DirAccess.remove_absolute(USER_DIR + path)


# This appears to be the problem
func load_var(name: String, value_if_null: Variant = null) -> Variant:
	var path: String = USER_DIR + name + SAVE_EXT
	var result: Variant = value_if_null

	if FileAccess.file_exists(path):
		result = FileAccess.open(path, FileAccess.READ).get_var(true)

	return result


# Works flawlessly (I guess?)
func save_var(name: String, value: Variant) -> void:
	var path: String = USER_DIR + name + SAVE_EXT

	FileAccess.open(path, FileAccess.WRITE).store_var(value, true)

work_resource.gd:

class_name WorkResource extends Resource


@export var background: Texture2D
@export var icon: Texture2D
@export var sound: AudioStream
@export var id: String
@export var name: String
@export var salary: int
@export var cost: int

work.gd (Where DataSaverLoader.load_var is called):

extends Control


@export var default_resource: WorkResource
@export var background: TextureRect
@export var icon: TextureRect

const SAVE_NAME: String = "work_resource"


func _ready():
	# I suppose this is where it crashes
	var resource: WorkResource = DataSaverLoader.load_var(SAVE_NAME)

	if resource == null:
		resource = default_resource
		DataSaverLoader.save_var(SAVE_NAME, resource)

	background.texture = resource.background
	icon.texture = resource.icon
	

Steps to reproduce

  1. Start a new Godot project
  2. Create a simple subclass of Resource
  3. Save instance of it via FileAccess.open("user://something.save, FileAccess.WRITE).store_var(instance_, true)
  4. Try to load it with FileAccess.open("user://something.save, FileAccess.READ).get_var(true)

Minimal reproduction project

N/A

@moveonwasd
Copy link
Author

Found this workaround from 2 years ago
So issue isn't new, yet couldn't find an existing ticket
Sorry in advance

@AThousandShips
Copy link
Member

AThousandShips commented Nov 26, 2023

Thank you for reporting, closing as a duplicate of:

@AThousandShips AThousandShips marked this as a duplicate of #68666 Nov 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants