How can I have a section of code be copied to another node?
Asked Answered
H

3

0

I ask because I am providing a function for a timer that is tied to another node than the one providing the function because after the timer is created (and still running) I may want to delete the node that provided the original function.
I'm creating a emulation of an old game engine that was scripted in Lua so passing functions was easy. Right now, anything that might still need that function has to call it directly from the source.

Hainan answered 14/3 at 3:30 Comment(0)
S
0

Hainan the obvious solution is copy+paste.
another solution could be to give a reference to the code you want to copy in the form of a Callable to another node through a signal or assigning it as a property or something, but that wouldn't be a real "copy". the callable will break the moment the node "hosting" the code that callable is referencing disappears from the scene tree.
i've heard myths of "singletons", but i don't believe in those. if someone knows what they are and think they could help here, feel free to speak up.

Seta answered 14/3 at 4:26 Comment(0)
S
0

Seta the callable will break the moment the node "hosting" the code that callable is referencing disappears from the scene tree

in other news, i just learned what a lambda is. i haven't experimented much, but it is in fact a standalone block of code you can toss between nodes, which sounds more like what you wanted.
what you can do with a lambda does seem limited, however.

here's a link to a site with a written and also video'd guide on lambdas.
https://shaggydev.com/2023/02/02/godot-4-lambda-functions/

Seta answered 14/3 at 5:29 Comment(0)
I
0

Hainan Use a static function if it doesn't have to be a method i.e. if the code is not doing anything with its object's data.

Insensible answered 14/3 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.