how to use arrays and what type to use

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ArthurER

I would like to learn about arrays to know that I am using the right kind for what I am putting in them. I am pretty new to Godot and scripting so even though I go through documentation there is a lot that I don’t understand and I do a lot of learning by trial and error to get things to work.

the way I know to declare an array is simply

var target = []

I am using arrays to store different types of things like destination coordinates or for turret targeting where it is used to store the value of “body” from an on_body_entered function

then I have one that is storing missions for the enemy ai that has the unit reference , a string like “scout” and the mission destination.
the arrays are all under control , none are filling with garbage or anything but from what I read they can be “expensive” and it gets me to wondering about using them correctly.

right now I am only just testing and have few units in the map scene but I am imagining ahead where 20 units are shooting at each other and something like the targeting arrays are starting to get bigger and each unit in the battle are keeping track of each other… say something with easier math like five units battling five units each would be storing reference to five units that would be 50 indexes spread across 10 arrays. that added up pretty quick and makes me think of Red Alert slowing down for bigger battles.

makes me wonder if I am using the right types of arrays to store things like that.

:bust_in_silhouette: Reply From: IHate

I don’t know anything about your project but you are using a lot of arrays for what seem like simple tasks to me. You don’t need to save every piece of data, you can get it at runtime as you need with functions or signals.

The only reason I can think of for a simple turret to store 5 different enemies is if it is able to shoot 5 enemies at the same time. Otherwise you can just create a function on the turret that targets the closest enemy or the enemy with the most health,etc, making a behaviour that store the current target in case you want to keep the first target under fire even if it is not the closest enemy or the healthiest that way you save some memory.

If you can shoot 5 targets with a single turret this function would add them to the array as they get in range and then another would erase them as they leave. You can use signals to tell the function what enemy is in range with area entered or something like that.

You don’t need to store the enemies until they are in range unless you want to track for a very specific reason.

Make your turrets a scene so they can pick their targets on their own store their targets and ignore anything they don’t care about.

Is hard to tell without knowing more about what you exactly need but either way arrays must be huge to feel a significant slowdown. I’ve read some people storing the entirety of their game’s dialogue in a single array (I would parse it as I need it tho). Rendering things can take a bigger toll on your game’s performance than an array.

thanks for the feedback , my project is a top down tank RTS effort, and is coming along pretty good. my turrets are scenes of their own and their targeting array only stores the information of opposing units in their detection radius, I don’t see that there is another way to make that work, all turrets do target for themselves , I can select a tank and change its target focus to another unit but only within its detection radius. when I mention targeting array I am considering down the road when I add in other units particularly foot soldiers, it is not the size of one targeting array I was talking about , it is the impact of having a battle with a lot of forces involved.

I don’t think I indicated I am trying to store superfluous data but to get enemies doing something more than running around on predetermined paths storing data is required and there is no two ways about that.

I am really just curious about types of arrays for different types of data or if the arrays I am using are fine for the types of data, I doubt I am in danger of much memory impact at this point but figure I should have scaling in mind.

ArthurER | 2020-10-09 02:34

If you are storing all the units in range but you can shoot them all at the same time the information is irrelevant.

How are you updating the targets array? if its an RTS you are doing it every frame I imagine.
So why store all these enemies you don’t care about right now.

If you want to target another unit in range make a function that enables a “targeting mode” during this mode the turret displays its range and the units inside so you can swap targets once a target is selected end the “targeting mode” and stick to 1 target so it only needs to track that. If the target dies or runs away you can do an auto targeting and picks the closest enemy.

I’m not a very experienced programmer but I just don’t see the point storing all this data if you store it means you need it every time on every function, and I don’t think your turret is using all that info at all times. It will escalate quickly and it will be redundant most of the frames.

a bunch of arrays but small shouldn’t be a problem if you are using arrays that contain only 1 type of information use a poolarray there’s 7 types of pools in godot: Array — Godot Engine (stable) documentation in English

Optimized for memory usage, does not fragment the memory.

IHate | 2020-10-09 12:49

the target array is updated @ on_body_entered and @ on_body_exited because that is when the reference to the body is passed to the area and that does not happen every frame that a body is in the area it happens one time when the body enters or exits the area , if it is not accepted then then the turret would have to do a more frame by frame check for overlapping bodies, and that would break more than it would fix the turrets _process(delta) begins with “if target” that script does nothing until the detection radius pushes a target into the array @on_body_entered when targets have exited the array is empty and the turret returns home the script becomes inactive , with empty arrays.

some of what you are saying here just does not make sense for the action of autonomous units , if five units of an opposing force are in the detection radius of a unit, the purpose for that information is not limited to targeting , it also at the same time means that five units are targeting it , I don’t see how that information is irrelevant if I stand any chance to make some AI for units it is not going to be by discarding information like that.

perhaps you are imagining a player script driven by W, S, A, D keys and an enemy script , but that is not what I have I have a vehicle script and a turret script, that all units share , the player can move units to locations and change target focus , the enemy AI can so far scout and I just got it to select a scouted target and launch its first attack, it would not be able to do that discarding the information you are saying is not relevant.

ArthurER | 2020-10-09 15:24