How to create and use a module in Verse?

Hi,

I’d love to use modules to structure and logically group my scripts/classes. But I can’t seem to understand how to reference modules.

According to the docs, I can create a module by:

  • Creating a folder.
  • Creating a module inside a script by using module() instead of class().

The problem comes when I want to use a module from scripts outside the module. I can’t seem to understand how to use the using { / } to reference custom modules. The autocomplete tells me that the path is /Localhost/.../MyModule but you can’t use Localhost in the path (for obvious reasons).

What am I missing here?

>>> EDIT <<<
The answer by @1-UPProtocol is also a solution to my question (but I can only select one). So check it out, too!

1 Like

Hello, I struggled with this myself, but I found this is how to import custom modules. For example, say I create a custom module math_module in a seperate verse file

# Custom Module I make with a Multiply function
math_module<public> := module:
    Multiply<public>(X : int , Y : int) : int =
        return X * Y

In my main Verse File, I would do

using {math_module}

OnBegin<override>()<suspends> : void =
    Product : int =  math_module.Multiply(3, 5)
    Print(" 3 x 5 = {Product}")

Note: You must give the module the public specifier, along with every method / class you want to reference in another verse file. Also, both files should be in the same workspace

3 Likes

If you have two verse files organized into MyProject/Content/First.verse and MyProject/Content/MyModule/Second.verse, you can access the contents of Second.verse by adding using { MyProject.MyModule } to the top of First.verse

6 Likes

@Tom.Noonan
Hi. I followed your instructions to build the folder, but it didn’t work. Did I do something wrong?

You have entered MyModuleub1 rather than MyModule

2 Likes

@Tom.Noonan
oh. very sorry. I was mistake. thank you

@Tom.Noonan

Excuse me. One more question.

Is this a specification? If so, why?

That seems to be somewhat a problematic setup as it forces you to use the project name. Can that be transparent? Last but not least, can you please comment on module exposure?! I found only a single buried reference to that, somewhere here on the forums during some private update announcement when modules were update.

My goal is to create a code structure like so, but I seem to fail somewhere, or the compiler isn’t able to understand it for some reason.

MyProject/Content/VerseSources/First.verse
MyProject/Content/VerseSources/Submodule1/A.verse
MyProject/Content/VerseSources/Submodule2/B.verse
  • I would like be able to possibly reference Submodule1 from Submodule2 if they depend on each other in some way.
  • I would like to reach SubmoduleN from First.verse.
  • I would like to not being forced to use MyProject as a prefix.

The idea is to outsource SubmoduleN as standalone git repositories which I can commit to GitHub. That setup would allow me to properly re-use the modules in development across multiple projects instead of copy pasting them without any proper version control (UE version control simply does not cut here).

What is the proper way to make SubmoduleN public? IIRC it’s MyProject/Content/VerseSources/Submodule1.verse with some module macro being explicitly marked as public.

Thank you in advance.

Hi. Here you are.

MyProject/Content/VerseSources/Submodule1/A.verse

MyProject/Content/VerseSources/Submodule2/B.verse

MyProject/Content/VerseSources/First.verse

Output
image

1 Like

How were you able to get the “Content” folder showing in your hierarchy there? Did you make one in addition to the one included with a project? e.g. Plugins/Project/Content/Content/VerseSources/ in windows explorer?

@AJ_Lakeman
Yes. " Content" is a folder with only the “VerseSoruces” folder.