Best Way To Repeatedly Clean Arrays

I need to know what the most effecient way of achieving this goal:

RTS game with Towers that can be captured to increase income. Towers are captured by units within their colliders. Friendly units will prevent the tower from being captured if they are within range.

Currently the tower actor BP will add units that enter their collider to an array based on whether they are friendly or not. If an enemy enters the collider the tower will check the last index of the “friendly” array to determine whether to be captured. If a unit exits the collider it will attempt to removed it from both friendly and enemy arrays.

My issue is I need to clear these arrays regularly of any units that have been destroyed to get an accurate last index. As of now the most effecient way I can think to do this is to use a “for each” loop to remove the index of any array element found invalid. Furthermore, I also need the towers to check if the friendly index is empty when an enemy is present in the event the friendly unit is destroyed while both it and the enemy unit are within range as to initiate capturing the tower without requiring the enemy unit to reenter the collider.

I do not want to run a continuous loop constantly clearing and checking whether to be captured.

One idea I have is prior to being destroyed; every unit casts to the tower BP and attempts to be removed from both arrays. This would theoretically remove the need for the towers to clean the arrays of invalid units. Simultaneously, every tower can run a 0.5 second loop continuously checking the enemy array and if it’s not empty, it will check the friendly array, and start a capture event if no friendlies are present.

Thoughts?

You’re right, looping is a bad idea.

I think you’re set, because you have the collider.

Rather than using array, why not use maps? You don’t have to rumble through to find a unit, you can just look it up.

When a unit enters, put it in the map, on end overlap take it out.

How many towers and units, roughly? I need to know to tell you what to do when one is destroyed.

Towers 6-10.

Units, think Starcraft 2. 200-500 MAX depending on number of players and stage of the game.

Most of the time probably under 200 total units in the level.

Yup. Then the units can make a call to the towers before they destroy themselves. You can do it with a blueprint interface.

If you want to reduce it to one call, you can use a third BP, ‘tower manager’. The towers all bind to an event in this manager, and the units only have to call ‘i have been destroyed’ once, on the tower manager. The towers will automatically all get this because they are bound to the manager.

1 Like

Wouldn’t this remove a unit from every tower if there are any currently nearby? Can I pass that unique character’s element to them via dispatch?

I have not used maps before. I’ve only ever seen them used as a string/integer variable.

When the unit is destroyed, it does need to be removed from every tower, no?

You can have a map that looks up based on unit ID ( an integer ).

You can pass parameters through a dispatcher.

Ah OK.

I was assuming the towers would have a generic integer for “friendly” and “enemy” keys and when units enter and exit they would add or subtract from those integers.

Nope. They all have a unique ID. The towers have two maps. One for friendly, one for enemies.

I was thinking about this a bit more.

I don’t think you need dispatchers.

Each tower can manage it’s own array/map as the units enter and leave the collision volume. If a unit is destroyed between towers, it doesn’t matter. It’s only if a unit is destroyed when it’s overlapping a tower we need to consider.

You just need

image

and put the tower BP class in the filter. That way, you immediately know which tower to tell you’ve been destroyed.

I think sets, actually is the easiest way. This is the destroy code for a unit

And in the tower

and for start and end overlap

Technically, it’s not destroyed, but it’s the same code, so we might as well use that…

1 Like