Best way to program an Interaction system (performance)

Hey everyone!

I’m currently working on a side project and had a quick question about what the most efficient way (in terms of performance) to set up an interaction system is.

In the past I’ve used a lot of different collision objects. The one in the player looks for ones in the interactable object. The player checks every interactable object in range and determines which is the closest.

This approach has worked for several games I’ve made including most recently Second Son: Second Son [Project 2021-8] by IanGoGo

I have been trying to think of a better way to do it. I have come up with another way where I can have one script (a singleton) track all interactable objects in a level and determine the closest one to the player and if it’s in range. This is what I want to try in my test project.

My question is which would be more efficient in terms of performance. Is it better to have tons of different collision objects and only calculate if the player is within them or have the game loop through every object every frame (could also do on a timer and check a few times a second) to determine what the player is closest to.

Thanks!

Tons? Are we really talking about thousands here?

Are the objects moving or are they static? Either way, you could partition them into a coarse grid and only check those in the same grid cell as the player.

I think you will need to measure both solutions. It’s hard to say which will be the fastest.

No, not thousands. Probably a few dozen in the worst case scenario for the game.

I’m planning on using this system for a future project, specifically a mobile game. So I wanted to see if there was a measurable difference or a preferred way of doing it.

Objects will be static once they’ve spawned in the world.

I’ll do some testing and post my findings here!

That should not be a problem!

When in doubt, measure it yourself! :slight_smile: