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

Typed Array constructor has no clear documentation about how it should be used #79066

Closed
Gnumaru opened this issue Jul 5, 2023 · 13 comments · Fixed by #79075 or #69451
Closed

Typed Array constructor has no clear documentation about how it should be used #79066

Gnumaru opened this issue Jul 5, 2023 · 13 comments · Fixed by #79075 or #69451

Comments

@Gnumaru
Copy link
Contributor

Gnumaru commented Jul 5, 2023

Godot version

v4.0.3.stable.official [5222a99]

System information

Windows 10

Issue description

There is one (and only one, at least that is exposed to gdscript) array constructor meant to creating typed arrays. This constructor takes as first parameter a base array for copying it's elements from. But the way the constructor should be used could be better documented with examples for creating typed arrays from native types and custom types, and these could have examples for scripts with and without class_name and also for inner classes.

Steps to reproduce

Create a script with the code bellow and run it. It will yield the same error in any of the attempts to use the typed array. constructor. The correct usage has been clarified in the comments bellow but my struggle to find how to do it is at least an example of the poor experience other users could avoid if there where a couple of examples in the docs.

extends Node
class MyClass: pass # inner class to create a type array of it
func _ready()->void:
	var a:Array = [MyClass.new()] # untyped array to copy into the typed one
	var b:Array[MyClass] # array typed with a class name

	# NONE of the methods bellow work. all of them will yield the runtime error "trying to assign an array of type "Array[MyClass]" to a variable of type "Array[MyClass]".
	# the stack trace is
#	E 0:00:03:0344   DELETEME.gd:12 @ _ready(): Attempted to assign an object of type 'RefCounted' into a TypedArray, which does not inherit from 'MyClass'.
#  <C++ Error>    Condition "!ClassDB::is_parent_class(object->get_class_name(), class_name)" is true. Returning: false
#  <C++ Source>   core/variant/container_type_validate.h:129 @ validate_object()
#  <Stack Trace>  DELETEME.gd:12 @ _ready()
	b = Array(a, TYPE_OBJECT, &'MyClass', null)
	b = Array(a, TYPE_OBJECT, &'RefCounted', null)
	b = Array(a, TYPE_OBJECT, &'Object', null)
	b = Array([]+a, TYPE_OBJECT, &'MyClass', null)
	b = Array(a.duplicate(), TYPE_OBJECT, &'MyClass', null)
	b = Array([]+a.duplicate(), TYPE_OBJECT, &'MyClass', null)
	b = Array(a.slice(0), TYPE_OBJECT, &'MyClass', null)
	b = Array(a.map(func(p):return p), TYPE_OBJECT, &'MyClass', null)
	b = Array(a.filter(func(p):return true), TYPE_OBJECT, &'MyClass', null)

	# this is the only method that works
	b = []
	for i in a: b.push_back(i)

Minimal reproduction project

N/A

@AThousandShips
Copy link
Member

You need to put the script as the fourth argument I think, see documentation

@Gnumaru
Copy link
Contributor Author

Gnumaru commented Jul 5, 2023

It doesn't work either. Regardless of using "self" or "self.MyClass"

image

@AThousandShips
Copy link
Member

AThousandShips commented Jul 5, 2023

That's not the script though, it's a class, try get_script

@Gnumaru
Copy link
Contributor Author

Gnumaru commented Jul 5, 2023

I'm sorry, I was too hasty. but it doesn't work either

image

@AThousandShips
Copy link
Member

Try with RefCounted as the third argument

@Gnumaru
Copy link
Contributor Author

Gnumaru commented Jul 5, 2023

nope

image

@Gnumaru
Copy link
Contributor Author

Gnumaru commented Jul 5, 2023

but it does work with MyClass which is the expected

image

@AThousandShips
Copy link
Member

Then no bug, though ambiguous as MyClass isn't a script

@Gnumaru
Copy link
Contributor Author

Gnumaru commented Jul 5, 2023

The docs are still not much clear about this. Should I close this and open a bug in godot-docs? or just leave this here?

@AThousandShips
Copy link
Member

Leave here, the class reference is handled here

@Gnumaru Gnumaru changed the title Unable to create a typed array copy from an untyped array of valid elements Typed Array constructor has no clear documentation about how it should be used Jul 5, 2023
@dalexeev
Copy link
Member

dalexeev commented Jul 5, 2023

Use

Array(a, TYPE_OBJECT, &"RefCounted", MyClass)

@AThousandShips
Copy link
Member

Already resolved above :)

@dalexeev
Copy link
Member

dalexeev commented Jul 5, 2023

If/when #71336 is merged, this should become less verbose and confusing:

Array[MyClass](a)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants