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

Create Signal type for better autocomplete and compiler errors #2731

Closed
AaronRecord opened this issue May 17, 2021 · 5 comments
Closed

Create Signal type for better autocomplete and compiler errors #2731

AaronRecord opened this issue May 17, 2021 · 5 comments

Comments

@AaronRecord
Copy link

AaronRecord commented May 17, 2021

Describe the project you are working on

Godot

Describe the problem or limitation you are having in your project

Using strings for signals results in worse compiler errors/autocomplete/refactoring tools

Describe the feature / enhancement and how it helps to overcome the problem or limitation

I think there should be a Signal type for signals that (maybe) inherits Callable (kind of like C# events).
Object would no longer need connect(), disconnect(), emit_signal()

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Here's some of my ideas for how it could look:

GDScript:

# player.gd
signal on_death(score: int)

func die():
	on_death.call(score)
	# or if callables ever support this
	on_death(score)
	# or maybe if using callables wouldn't make sense, something like
	on_death.emit(score)


# death_screen.gd
player.on_death.connect(show)

func show(score: int):
	# do stuff

C++:

// base_button.h
public:
	Signal pressed; // something like this

// my_class.cpp
my_button->pressed.connect(callable_mp(this, &MyClass::do_stuff));
// or maybe like C#
my_button->pressed += callable_mp(this, &MyClass::do_stuff);

If this enhancement will not be used often, can it be worked around with a few lines of script?

No

Is there a reason why this should be core and not an add-on in the asset library?

Signals are core


See also: #2557
@AaronRecord AaronRecord changed the title Create Signal type that (maybe) inherits Callable Create Signal type for better autocomplete and compiler errors May 17, 2021
@dalexeev
Copy link
Member

@KoBeWi
Copy link
Member

KoBeWi commented May 17, 2021

Yep, closing.

@AaronRecord
Copy link
Author

I guess I got confused because most places in the c++ codebase still use strings for the signal name, is this just a gdscript thing?

@Calinou
Copy link
Member

Calinou commented May 17, 2021

I guess I got confused because most places in the c++ codebase still use strings for the signal name, is this just a gdscript thing?

Indeed, it's only used in GDScript for now. I'm not sure if there are plans to use the Signal type in C++. See also #2152.

@YuriSizov
Copy link
Contributor

YuriSizov commented May 17, 2021

Yes, engine uses strings for that. To make the process a bit more controlled you can use the CoreStringNames singleton.

_curve->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &CurveTexture::_update));
emit_signal(CoreStringNames::get_singleton()->changed);

Edit: Though Calinou's proposal would be better.

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