I am making a lot of rotating objects for my game. For optimization, I want to enable their movement when player is nearby. In order to do that I need a way to detect if player is near then bind event to that. Anyone has any idea how to implement this?
Try adding large collision spheres to you actor class, set the collision type to trigger, and tick the enable overlap events box. Then in event graph, make a On Begin Overlap event for the component, check if the other actor pin from the event = the player char, and call the function which enables the movement.
Could put the huge sphere on player and when it overlaps send an interface message to the other actor telling it to wake up. If you add a tag to actors that can wake up this way, you can filter out other overlaps by checking if overlapped actor has the tag.
Here I added a 1000 unit sphere to player char with overlapalldynamic collision and do this:
Turret actor also has a tag “sleepy” which the overlap event on player looks for. Whenever the 10 meter sphere on the player overlaps one, it starts rotating to look at me and stops after moving away..
Probably less overhead this way with only player char testing for overlaps.
Every component you add to a character gets location & rotation updated per tick. Set Location and Rotation is a heavy expense to add if there’s another feasible solution.
A simple manager class with a large sphere collision that fires off dispatchers when overlapped is probably the best option.
All the moving actors will get a reference to the manager in order to bind. One to Many approach.
Collision would ignore all, overlap Pawn. No other filtering is needed.
For multiplayer you’d simply add actor references to an array on Begin Overlap, Remove on End overlap, then check array length.
you could do the rotation in a material using world position offset, depending on what it’s used for. this would take some creative asset/shader work but would be very efficient.
I guess the million dollar question is: What are the rotating actors doing, and how urgently do the rotations need to be (de)activated? Is the rotation critical to gameplay, like a turret or spinning blade trap? If so, some sort of overlap test or some external “security camera” actor to control them might be needed. If the spinning is mostly cosmetic, like spinning item pickups or some sort of exaust fan/windmill props, you could just slap a looping timer event in begin play to check for nearby player once or twice a second.
Other thing that hasn’t been mentioned is TriggerVolume. When player enters / exit it can enable/disable things.
Preferably tie an entire room or group of objects to a volume, rather than a volume per rotating thing.
You probably also want to make this a generic functionality of your game objects, so any object with movement or calculations can be turned on/off when the player is not near.
I would do an ononverlap off of an event tick, that would likely be the cleanest way to do it with collision. If not collision then you could do it with math and in range float. Likey then I would make an actor bp for the rotaters and then have the logic for each of them to rotate whilst the player is in the vicinity.
Careful if you go the ‘overlap’ approach. Every object that is testing for overlaps has a cost, it can add up to quite a bit with a bunch of objects. If you go this route put some careful thought on setting up your collision.