How to use a function from a custom module in creative device?

I’m not sure what the problem is in the way I’m using modules. I’ve researched for awhile and can’t find anything that really explains it.

I have created the following module for the purpose of cycling vending machines. ( all of this is done because you can’t turn vending machine’s cycle ability to off…) and I don’t want to clutter up my other code where I want to actually cycle these vending machines.

I don’t know if I created this module correctly and if it needs to have a class or not. I’m not sure how I’d edit the editables actually if it’s not a device that’s in the level. I might have to just make to creative devices now that I think about it. can modules have editables? Please review the code and answer my questions if you can.

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }

VendingModule<public> := module:
    vending_manager := class(creative_device):
        @editable
        CastleTransform : transform = transform{
            Translation := vector3{X := 100.0, Y := 200.0, Z := 0.0},
            Rotation := MakeRotationFromYawPitchRollDegrees(YawRightDegrees := 0.0, PitchUpDegrees := 0.0, RollClockwiseDegrees := 0.0),
            Scale := vector3{X := 1.0, Y := 1.0, Z := 1.0}
        }

        @editable
        IceHouseTransform : transform = transform{
            Translation := vector3{X := 300.0, Y := 200.0, Z := 0.0},
            Rotation := MakeRotationFromYawPitchRollDegrees(YawRightDegrees := 0.0, PitchUpDegrees := 0.0, RollClockwiseDegrees := 0.0),
            Scale := vector3{X := 1.0, Y := 1.0, Z := 1.0}
        }

        @editable
        IceChairTransform : transform = transform{
            Translation := vector3{X := 500.0, Y := 200.0, Z := 0.0},
            Rotation := MakeRotationFromYawPitchRollDegrees(YawRightDegrees := 0.0, PitchUpDegrees := 0.0, RollClockwiseDegrees := 0.0),
            Scale := vector3{X := 1.0, Y := 1.0, Z := 1.0}
        }

        @editable
        ArcticTransform : transform = transform{
            Translation := vector3{X := 700.0, Y := 200.0, Z := 0.0},
            Rotation := MakeRotationFromYawPitchRollDegrees(YawRightDegrees := 0.0, PitchUpDegrees := 0.0, RollClockwiseDegrees := 0.0),
            Scale := vector3{X := 1.0, Y := 1.0, Z := 1.0}
        }
        
        @editable
        var CastleVMs : []vending_machine_device = array{}
        @editable
        var IceHouseVMs : []vending_machine_device = array{}
        @editable
        var IceChairVMs : []vending_machine_device = array{}
        @editable
        var ArcticVMs : []vending_machine_device = array{}

        # Transform to send the vending machine to when 'hiding'
        @editable
        var HideTransform : transform = transform{
            Translation := vector3{X := 0.0, Y := 0.0, Z := -1000.0},
            Rotation := MakeRotationFromYawPitchRollDegrees(YawRightDegrees := 0.0, PitchUpDegrees := 0.0, RollClockwiseDegrees := 0.0),
            Scale := vector3{X := 1.0, Y := 1.0, Z := 1.0}
        }

        SpawnVendingMachines(Wave: int)<decides><transacts>: void =
            if (Wave < 4 and Wave > 0 and Wave < CastleVMs.Length):
                # Remove current vending machines
                CastleVMs[Wave -1].TeleportTo[HideTransform]
                IceHouseVMs[Wave-1].TeleportTo[HideTransform]
                IceChairVMs[Wave-1].TeleportTo[HideTransform]
                ArcticVMs[Wave-1].TeleportTo[HideTransform]
                
                # Bring in next round of vending machines
                CastleVMs[Wave].TeleportTo[HideTransform]
                IceHouseVMs[Wave].TeleportTo[HideTransform]
                IceChairVMs[Wave].TeleportTo[HideTransform]
                ArcticVMs[Wave].TeleportTo[HideTransform]```


I want to be able to call this 'SpawnVendingMachines()' function in another class.

I’ll try and answer, but I’m not sure I’m fully understanding the question.

I can see from this line of code that the verse file represent a device, not just a class - as you said it does.

    vending_manager := class(creative_device):

After compiling this code, did you get a new device representing it in the UEFN browser? They appear at the top level under your project name.

Did you drag and drop that device into the world - in the UEFN editor? So you’ll have something like this now for the device?

If so, at this point you’ve created an instance of your new device in the game - it will start when the game starts.

If you want some other Verse device to have access to this Verse device instance, you need to use an editable there as well.

Let’s say the second device is called another_device, it might look something like this.

another_device := class(creative_device):

    # This can be used for another_device to get access to the instance of vending_manager that you placed in the world.
    @editable IN_Vending_Manager : vending_manager = vending_manager{}

After compiling the Verse code again, you’d need to add the second device from the browser into the world (the another_device one). Then click on this new device instance in the world and you can set the IN_Vending_Manager editable to your first device.

Hope it helps.

1 Like

I understand that and I think I’ve done something like that before but at first I thought I didn’t need it.

How would I just use a function from a module is what I was trying to figure out but the way I’ve written this module, it is a creative_device with it’s own editables. So i’d have to go your method. But I’ll have to change it from being a module and just a creative device I think because I’m getting this error.

image

A standalone function can be used anywhere in the module as far as I know (a function not in a class or device).

A function that is part of a class, you can create an instance of that class, and then call the function anywhere.

myVariable : my_class = my_class{}

myVariable.SomeFunctionInMyClass()

But devices are different, they are made to be added to the game world, and the game creates and starts them (well, each instance added to the game world).

It really depends on what the function needs to do, and how it can/needs to fit in with the rest of the code.

1 Like

Making a public module isn’t enough to make everything public inside the module, you need to do the following :

VendingModule<public> := module:
    vending_manager<public> := class(creative_device):

Everything inside a module is internal by default, unless you specify otherwise
I believe by making the module public, you’re only making it “importable” from anywhere

1 Like

Ok but how to import it?

using { VendingModule }

1 Like

hmmmmmmm… thank you! So a few things. I was reading or misreading maybe that I had to import the path? But what you suggested finally doesn’t give errors. I thought I tried that. It works regardless of if the module is public or not. So could you clarify by importable from anywhere? Could you import it? Could I import it into another project? Or is it just different folder hierarchy within my project? Any clarification anyone could give would be great.

I see that if I want editables, I need to create a class and then an instance of that and get a reference to that device instance and call it’s properties in the way I have it set up. So is there any point to me having this in a module? It seems to work as just a class.

So I guess I have failed to use a module correctly because I wanted editables and if I remove the class line then I get errors about “This invocation calls the ‘reads’ effect…” so I’ll just use it as a class.

Edit: I’ve tinkered and found out that nothing needs to be public if you get an instance of that device(or probably just class too) and then call the functions on that instance. Which makes sense but I haven’t found a good use of <public> yet so I’m still a little confused on when to use it. You all have pointed out some interesting things about it though.

But I think I understand modules and how to use them a little bit better now. I probably could rewrite it to not have editables and only use gameplay tags but I need more granularity over which array element is in which slot, thus picking them from the scene/list is the better option here.

I’ll use a module soon! I’m determined. If anyone knows of any good snippets that would help me understand this I would appreciate it. @btwixer and @im_a_lama, you’ve both been a great help. I really appreciate it!

There’s no point in using modules, it’s just used for file structuring and managing property/method access more accurately

Would be hard to explain what I said earlier, you’d need to experiment with modules for that

1 Like

I’ve got several devices and a bunch of classes, I’ve ignored modules so far, likely UEFN created a module for me and all my code is in that module.

I only use devices when I need one or more editables - and almost all of those are in my ‘resource manager’ device, so I have one place for most things that need to be populated in the game world but used in Verse. My other classes then call a resource manager function to get ‘their’ editables.

I tried using gameplay tags shortly after they came out, but at point that they mostly worked but didn’t seem to be reliable enough, so for now I’m not using them. But if they are reliable they’re a great option in my view.

I have cases where I need editables to be paried - such as custom props and their respective prop managers. At this point I do that by hand, I have an editable array for each and I put them in the array in the same order (prop 1, prop 2, prop3 …) and (propMan 1, propMan 2, propMan 3 …).

Previously when I used gameplay tags I matched them up based on their location (transform), for each prop I checked all the unmatched propMan’s for the one that is within a small area of the prop. That worked well and was less tedious than my current approach. :slight_smile:

There’s tons of different ways to write and organize code … partly it’s for maintainability / clarity / efficiency … partly it’s just style / personal choice.

1 Like