Verse design help: how to increment a counter in a main class if we call functions in another class?

Hola,

I’m trying to clarify my own algorithm in my head about how to design an aspect of my custom level.

For simplicity, let’s say I have the main verse class “coin manager” and then I have another class “coinItem” that has functions “OnCollected” and “Spawn_This.” They way I’m “spawning” the objects are that they are already created in the level and I’m just teleporting into the active play area in certain locations.

So what I want to achieve is that the coin manager class has a total coin count for the spawned coinItem objects in the stage, so no more than a declared maximum can “spawn” in the game. But my coin manager class’s counter variable needs to be accessed and updated on the side of the coinItem class functions no? Like I have a Subscribe function for when the coin object device (contained in coinItem) is collected, but how do I update the counter from the main coin manager class?

I can’t pass a function parameter by reference (with handler functions) to be able to change the total coin count via the coinItem class functions “OnCollected” and “Spawn_This” is my understanding, is that correct? Is there some best practice how to do this?

Maybe you can have an Init() function (or named whatever you want) in “coinItem” which you call at the start of the game and pass the “coin manager” instance to it. The “CoinItem” then calls a “Coin manager” function when a spawn occurs, or anything else the “coin manager” needs to know about.

1 Like

Hmm interesting idea, do you also know if this method might need to use the self keyword? Note, I plan to use multiple “coin manager” devices one per active stage area and there are multiple stages on my map. I’m still a little new to Verse and I saw there might be a keyword to reference the particular instance being used :thinking:
Like in the Handler function example:

Button.InteractedWithEvent.Subscribe(Handler1{Device:=Self, Parameter1:=Value}.HandlerFunction)

I think the ultimate thing is I need to make sure I’m changing or updating the original count value somehow and not a copy passed somewhere.

Maybe there’s another way to think about this than how I approached it. When I subscribed to the OnCollected event, I made it use the coinItem’s function directly. Instead of that, maybe first I can send the subscribed event to a function within the coin manager to update the counter, and then somehow trigger the specific coinItem object’s OnCollected function from there. I’ll have to think about it more when I can open my project again.

There are a lot of ways to organize code/classes/etc. :slight_smile:

I don’t use Self very often, so far, in Verse - it can be used to provide a reference to itself to something else - for example. if Coin Manager called a function in Coin Item and wanted to give a reference to itself to Coin Item, it would pass Self and Coin Item would received it as a Coin Manager.

1 Like

Thanks for adding that, it’s good to consider for sure!

Right now I’m seeing a bunch of errors in verse files (even the official ones) after the update, so guess that will have to get sorted out too before I try anything. :open_mouth: And yes, I love how there can be multiple approaches to a problem and sometimes we can find creative solutions!

EUFN is powerful, and for me a lot of fun, but it can be buggy and frustrating too.

The previous update all my and UEFN Verse code showed up red, but a recompile fixed it (so it didn’t slow me down).

This update the UEFN Verse code shows red (but not mine) but everything is working fine.

But I’ve seen multiple posts on Verse errors this update for others.

Yeah, I got a lot of errors because of a certain thing I put in with my editable variables. I hope they can fix it soon, otherwise I’ll have to do the suggestion from the other forum post and move a lot of lines of code for the errors to go away.