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

It should be possible to create Callables as const members of a class #11275

Open
blackears opened this issue Dec 2, 2024 · 1 comment
Open

Comments

@blackears
Copy link

Describe the project you are working on

A behavior tree library

Describe the problem or limitation you are having in your project

I'd like to create some const Callable members of a class. This seems to be impossible in GDScript at the moment.

So I'd like to be able to write

enum Status { SUCCESS, RUNNING, FAILURE }
const policy_run_until_success:Callable = func(status:Status): return status == Status.SUCCESS

At the moment this is causing the error
Assigned value for constant "policy_run_forever" isn't a constant

This does work:

enum Status { SUCCESS, RUNNING, FAILURE }
var policy_run_until_success:Callable = func(status:Status): return status == Status.SUCCESS

However, it is not a constant so it can be overwritten. The main point of this is so that I can use it as a default value in the class's constructor:

var policy:Callable

func _init(_policy:Callable = policy_run_until_success):
	policy = _policy

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

It allows you to create libraries of lambda functions that you can then use elsewhere.

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

Update GDScript so that lambda functions can be assigned to const fields.

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

No, since var fields can be overwritten and cannot be used as default parameter values.

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

Requires changing GDScript.

@dalexeev
Copy link
Member

dalexeev commented Dec 2, 2024

No, since var fields can be overwritten and cannot be used as default parameter values.

Note that function parameters support non-constant default values.

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

2 participants