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

Unable to load Array of custom classes directly from load/preload to statically typed variable #95568

Open
Zain-A-Abbas opened this issue Aug 15, 2024 · 3 comments

Comments

@Zain-A-Abbas
Copy link

Zain-A-Abbas commented Aug 15, 2024

Tested versions

  • Reproducible in v4.3.stable.official [77dcf97]
  • Not present in v4.2.stable.official [46dc277]

System information

Godot v4.3.stable - Windows 10.0.19045 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3060 (NVIDIA; 31.0.15.3742) - AMD Ryzen 5 3600 6-Core Processor (12 Threads)

Issue description

In 4.2 and earlier, one could load arrays of custom object types directly from a loaded resource file in one line. E.g. var myvar: Array[SomeResource] = preload("somefile.tres").someVariable. Now, it gives the following error message: "Cannot assign a value of type Array[res://SomeResource.gd] to variable "myvar" with specified type Array[SomeResource]."

No error is brought up and code runs as usual if myvar is not statically typed. The error is also avoided if split up into two lines, e.g.

var myvar = preload("somefile.tres")
var yourvar: Array[SomeResource] = myvar.someVariable

Steps to reproduce

In the minimum reproduction project, the node.gd file will display the error. It can be reproduced by

  1. Creating a custom class
  2. Creating a resource with an array of that class as a variable
  3. Trying to load that data into a statically typed variable.

Minimal reproduction project (MRP)

Bug-Reporting.zip

Bugsquad edit: Fix formatting.

@goblinrockcandy
Copy link

I also encountered this problem. However, note that for me, it worked using load() would work, but preload() would not.

This is my reproduction:

# custom_resource.gd:
class_name CustomResource
extends Resource
# custom_resource_list.gd
class_name CustomResourceList
extends Resource

@export var array: Array[CustomResource]
# main.gd
extends Node

const SAVED_CUSTOM_RESOURCE_LIST = preload("res://saved_custom_resource_list.tres")

# This works!
func _loaded_array_resource() -> void:
	var loaded: CustomResourceList = load("res://saved_custom_resource_list.tres")
	for custom_resource: CustomResource in loaded.array:
		print(custom_resource)


# This does not work.
func _preloaded_array_resource() -> void:
	for custom_resource: CustomResource in SAVED_CUSTOM_RESOURCE_LIST.array:  # error here!
		print(custom_resource)

This is the error message:
Unable to iterate on value of type "Array[res://custom_resource.gd]" with variable of type "CustomResource".

@smedelyan
Copy link

smedelyan commented Aug 16, 2024

EDIT

Sorry, I haven't read the issue to the end. My reply is irrelevant


ORIGINAL REPLY

In 4.2.2 there were issues with typed arrays, e.g. you couldn't do things like this:

var typed_arr: Array[String] = []
var plain_arr: Array = ["test"]
typed_arr = plain_arr # this will fail
typed_arr.assign(plain_arr) # this will be ok

I think the issue is that the const / vars above are not typed and maybe treated as Variant-s. If this is the case, then it is valid because the types of entities don't match.

I recall that I had issues with definined typed consts when used with preload() in 4.2.2 (e.g. const MY_CONST: MyType = preload("...") didn't work even if casted as preload("...") as MyType). But maybe now it's working?

Can you try to use := assignment or specify the type explicitly?

@akien-mga akien-mga added this to the 4.4 milestone Aug 16, 2024
@matheusmdx
Copy link
Contributor

Bisecting points to #92544 as the culprit:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In progress / Assigned
Development

No branches or pull requests

6 participants