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

has_method() claims method available, but invalid on call() #87456

Open
AjaniBilby opened this issue Jan 22, 2024 · 6 comments
Open

has_method() claims method available, but invalid on call() #87456

AjaniBilby opened this issue Jan 22, 2024 · 6 comments

Comments

@AjaniBilby
Copy link

AjaniBilby commented Jan 22, 2024

Tested versions

  • Godot 4.2.1
  • Godot 4.0

System information

Microsoft Windows [Version 10.0.22621.3007]

Issue description

Using has_method on a scene instance can result in a true result when the .get_script()'s script has the function, however if you then run call() it will error

Steps to reproduce

manager.gd

@tool
extends Node3D

const Child = preload("res://child.tscn")

@export var pass_btn: bool = false:
	set(ticked):
		if not ticked: return
		pass_btn = false
		will_pass()

@export var error_btn: bool = false:
	set(ticked):
		if not ticked: return
		error_btn = false
		will_error()
	
func will_pass():
	var ref = Child.instantiate()
	var script = ref.get_script()
	print(script.has_method("test")) # print: true
	script.call("test")              # print: Hello From child
	script.test()                    # print: Hello From child

func will_error():
	var ref = Child.instantiate()
	print(ref.has_method("test")) # print: true
	ref.call("test")              # error: Invalid call. Nonexistent function 'test (via call)' in base 'Node3D (child.gd)'.
	ref.test()                    # error: Invalid call. Nonexistent function 'test' in base 'Node3D (child.gd)'

child.gd

extends Node3D

static func test():
	print("Hello From child")

Minimal reproduction project (MRP)

minimal-has-method-error.zip

@dalexeev
Copy link
Member

See #82767 and #86088, it should be fixed in 4.3 dev, please test.

@AjaniBilby
Copy link
Author

Built from master, but the issue still persists
image

@AjaniBilby
Copy link
Author

AjaniBilby commented Jan 22, 2024

If I make the test function non-static, then it fails in both:

  • The get_script version failing because the function is not static
  • The direct instance call failling for the same reason as with static (Nonexistent function)

This is not a change of behvaiour from 4.2 -> 4.3 - I'm just noting it's not just a static call issue

@Rubonnek
Copy link
Member

Built from master, but the issue still persists

I can confirm.

The MRP above works fine when updating child.gd to have @tool in it:

@tool
extends Node3D

static func test():
	print("Hello From child")

With that change, will_error() won't error out.

I personally think there should be an Editor-specific warning to let users know they are attempting to call functions from secondary scripts that the Editor expects to be in tool-mode as well but that are not, so that users know to update those scripts to be in tool mode. It seems reasonable to be able to run static functions from non-tool scripts however.

@dalexeev dalexeev added discussion and removed bug labels Jan 22, 2024
@tokengamedev
Copy link

I have a question related to autoload scripts.
Does autoload scripts have to be in @tool mode to be called. I tested; it is not working without @tool mode. Is it a bug or working as designed?

Personally, I feel, this is ridiculous to not able to create objects and call methods from a script without @tool mode specified.
Moreover, if it is not allowed it should throw error accordingly, but we have to waste hours on debugging why this method is not getting called.

If it is so much limitation why not put the @tool mode at the project level. That way we game developers don't have to bother about @tool mode at each scene level but about other crashes and issues due to @tool mode

@jinyangcruise
Copy link

I suggest the error message can add some extra hint like "you may have to add @tool to your script" if it detects the code running in tool mode.

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

5 participants