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

Passing null for RefCounted argument from GDScript prints error (though the function runs without problems) #1615

Open
Maximilian-Seitz opened this issue Oct 6, 2024 · 1 comment · May be fixed by #1616
Labels
bug This has been identified as a bug

Comments

@Maximilian-Seitz
Copy link

Godot version

v4.4.dev3.official (f4af820)

godot-cpp version

4.4.dev (6facde3)

System information

Ubuntu 22.04.5 LTS 22.04 - X11 - Vulkan (Forward+)

Issue description

A GDExtension function with a RefCounted parameter (using Ref), which may be null, works fine from within GDExtension, but sometimes prints an error when passing null from GDScript.

The functionality remains in all cases, except for the printed error in one corner-case.

The error is:
Parameter "p_ptr" is null. from godot-cpp/include/godot_cpp/classes/ref.hpp:233 @ convert()

Steps to reproduce

The exact problem occurs when a variable typed as some RefCounted type is passed and this variable holds null. When the variable is un-typed, null is passed directly, or a valid object is passed, no error is printed.

GDScript example:

extends Node

enum Mode {
	TYPED_INPUT,
	UNTYPED_INPUT,
	LITERAL_INPUT,
	OBJECT_INPUT
}

@export var mode: Mode = Mode.TYPED_INPUT

func _ready() -> void:
	var typed_variable: Texture2D = null
	
	match mode:
		Mode.TYPED_INPUT:
			# Prints error, then 'true'
			print(Test.is_null(typed_variable))
		
		Mode.UNTYPED_INPUT:
			# Prints 'true'
			var untyped_variable = typed_variable
			print(Test.is_null(untyped_variable))
		
		Mode.LITERAL_INPUT:
			# Prints 'true'
			print(Test.is_null(null))
		
		Mode.OBJECT_INPUT:
			# Prints 'false'
			typed_variable = ImageTexture.new()
			print(Test.is_null(typed_variable))

GDExtension example:

#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/classes/ref_counted.hpp>
#include <godot_cpp/classes/texture2d.hpp>

namespace godot {

class Test : public RefCounted {
	GDCLASS(Test, RefCounted)

	protected:

	static void _bind_methods() {
		ClassDB::bind_static_method("Test", D_METHOD("is_null", "value"), &Test::is_null);
	}

	public:

	Test() = default;

	static bool is_null(Ref<Texture2D> value) {
		return value.is_null();
	}

};

}

Minimal reproduction project

Note: godot-cpp folder is empty in minimal reproduction project, and needs to be fetched (as to not upload too much junk)

Project.zip

@dsnopek dsnopek added the bug This has been identified as a bug label Oct 7, 2024
@dsnopek dsnopek linked a pull request Oct 7, 2024 that will close this issue
@dsnopek
Copy link
Collaborator

dsnopek commented Oct 7, 2024

Hm, yeah, I agree we shouldn't print an error message for this - it's a valid case.

I just pushed PR #1616 which I think should fix this (I haven't had a chance to test it yet).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This has been identified as a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants