I want to be able to call a function in a separate script or have a variable shared between two different scripts, but I cannot figure out how to get the two different scripts to be aware the other exists. Are there any examples of how to do this anywhere?
Hi @DolphimDom:
The team has got a bit of work to do on documenting how Verse scoping works, but to answer your question directly, if you had the following Verse scripts within your project:
In hello_world_device.verse
:
Foo():void=
return
And another function in hello_world_device2.verse
:
Bar():void=
Foo()
This should already be compilable. If you hover over the Foo
symbol in VS Code, the LSP should also show you the full Verse path of that function symbol:
(/localhost/_MyProjectC:)Foo():void
Behind the scenes, the Foo
and Bar
symbols are defined under your project’s Verse path, which helps avoid namespace collisions. You can wrap these functions into modules/classes to help encapsulate them further:
my_module := module:
Foo<public>():void=
return
In your other script file, you’d then need to first import your new module:
using { my_module }
And then you can again use the function as-is in Bar
:
Bar():void=
Foo()
This isn’t the only way to do things in Verse and is far from the full set of rules surrounding scoping, but hopefully that gives you a clearer idea of where to start. Please feel free to provide feedback on the documentation located at: https://www.epicgames.com/fortnite/en-US/creative/docs/uefn/Verse/modules-and-paths-in-verse regarding this subject.
If I have 2 scripts but want to use := class(creative_device): is there a way to associate one with the other? My main desire is to use @editable in both which needs to be a class with a creative_device.
game_manager.verse
game_manager:= class(creative_device):
OnBegin<override>()<suspends>:void=
Foo()
hello_world_device.verse
hello_world_device := class(creative_device):
Foo():void=
Print("Hi")
Ended up figuring out from another forum post: How do I call a method on another verse device?
Basically for my example @editable then reference the other script, and then it can be called.
game_manager:= class(creative_device):
@editable
hello_world_device_ref: hello_world_device= hello_world_device{}
OnBegin<override>()<suspends>:void=
hello_world_device_ref.Foo()
Another solution to this problem would be using a global singleton reference to the device, I have an example of that here: I came up with a way to make singletons in Verse