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

Lambdas lose context when used with signals and call_deferred() #92033

Closed
HotNoggin opened this issue May 17, 2024 · 2 comments
Closed

Lambdas lose context when used with signals and call_deferred() #92033

HotNoggin opened this issue May 17, 2024 · 2 comments

Comments

@HotNoggin
Copy link

HotNoggin commented May 17, 2024

Tested versions

  • Reproducible in 4.2.2 Stable

System information

Godot v4.2.2.stable - Windows 10.0.22631 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3050 Ti Laptop GPU (NVIDIA; 31.0.15.5222) - AMD Ryzen 9 6900HS with Radeon Graphics (16 Threads)

Issue description

When working on my snake project, I had used this lambda in _ready():

func _ready() -> void:
    hurtbox.area_entered.connect(func(area: Area2D) -> void:
	    if area is Collectable:
		    var collectable: Collectable = area as Collectable
		    #CRITICAL This doesn't work without "self". Engine bug?
		    self.add_segment.call_deferred(false)
		    collectable.collect()
    )

When the self in self.add_segment.call_deferred(false) is removed, there is an error, which appears to say that the object calling this method doesn't exist! What the function does isn't too relevant, because a friend reproduced the error with minimal effort (as seen below in the steps to reproduce).

Steps to reproduce

Simply use this code in your project:

signal test

func _ready() -> void:
    test.connect(func(): do_thing.call_deferred())

func do_thing() -> void:
    print("hello")

func _process(delta: float) -> void:
    if Input.is_action_just_pressed("ui_accept"):
        test.emit()

You'll be met with this error when you press the key:
image
You'll also notice in the debugger that all of the object's properties are missing (this might not be a part of the bug, though), and only the properties from the lambda are shown.
Adding self before do_thing will also cause the issue to vanish.

Minimal reproduction project (MRP)

Lambda Reproduction Project.zip

@vnen
Copy link
Member

vnen commented May 17, 2024

I can reproduce on 4.2.2 but not on master. I assume this has already been fixed.

@vnen vnen closed this as completed May 17, 2024
@akien-mga akien-mga added this to the 4.3 milestone May 17, 2024
@dalexeev
Copy link
Member

I can reproduce on 4.2.2 but not on master. I assume this has already been fixed.

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