Raffle Rarity System - Is this method efficient?


I’ve programmed a raffle-style rarity system for my obstacle spawning and I want to run it by the community to see if the method I devised is efficient or not. Please have a look.

*~~~~~ ~~~~~ ~~~~~*

1) Create a data table drawing from a structure with a map of actors keyed to integers (commonalities).


**2) **Retrieve commonality map from data table using the name of the current level as the row name.

3) Loop through all the actors in the commonality map, adding up their commonality (as if they were raffle tickets), and storing them in a new map where each obstacle is paired with an Int32Range.

4) Store the total number of “tickets”.

5) Get a random integer between 1 and the total number of tickets counted.

6) Loop through all the actors in the new commonality map, checking if the random integer exists in any of their paired ranges.

7) Spawn the actor whose range includes the random integer.

*~~~~~ ~~~~~ ~~~~~*

Is this a good method for object rarity? I like that it allows me to tune the rarity in one place inside the data table.
I plan on executing SpawnObjects multiple times a second during play. Will looping through maps of actors and structs like this be inefficient?

Any advice would be appreciated.


I’d recommend using a soft reference to an actor rather than a hard one because hard references must be loaded to iterate on them. Use a soft reference to the path of the actor then if its ticket is chosen you can manually load it using the Asset Registry then spawn it. Otherwise your memory usage might skyrocket.

Your math is good and lean, I don’t see any issues with running this multiple times per second as long as you aren’t running it every tick or something crazy :slight_smile:

Thanks for your response

I’ve only read about soft references. How do you mean I should use them in this case? Should the map inside the data table be to only soft references? Or do I convert them to soft references after retrieving the row?

Do soft references make a large difference in performance that I should revise my game and make this change wherever I can?

Thanks for your time.

They should be soft reference sin the data table and in the struct. If you use soft references, you’re just storing string data + meta and those don’t become ‘real’ objects until you load them manually. If you have all hard references then you have them all stored in live memory the whole time.

It’s a trade off. With soft references you have to manually load which has a tiny performance cost during the game; if you use hard references everything is loaded at the beginning but that means taking up valuable memory. In the prototype stage that’s fine but in a completed game you’re loading every reference in every loaded object as well including things like textures. It depends on where you want to go with this in the end.