AI Threat Mechanic

Hey, quick question :slight_smile:

I am trying to implement threat levels for my AI. Each ship has an array of target gameObjects, with their corresponding threats levels. And then the AI shall attack which ever target has the highest threat level.

I wanted to use 2D arrays e.g:
threats[threatLevel, gameObject]

When a ship attacks the AI, it adds itself to the array. And when it dies/threat=0, deletes from the list. Note that threatLevel shall increase with the damage dealt, and wil decrease over time(especially when out of sight).

Is there anyway I can have 2 different types, paired together in an array like this?

Or could you suggest a better idea for handling threats levels of multiple targets, like maybe dictionaries?

Thanks for you time,
Magnus :slight_smile:

1 Answer

1

You’re misunderstanding what a 2D array is. Think of multidimensional arrays as more like grids (or arrays of arrays if you’re using the notation: object). They aren’t quite the same thing as storing multiple parameters. For that, you need a different structure.

Now, for what you want, I would recommend a Dictionary. This is a very neat data structure that uses pairs of objects, one as a “key” and one as a “value”. These can both be any type you like. The catch is that there can only be one object in the Dictionary with a specific key. This works for you, because each GameObject has a value associated with it, so you can use the GameObject as the key, and the threat level as the value.

Now, another, perhaps neater, way of doing it (and definitely better if you plan on adding more parameters) is to use a struct, or a class. For most purposes, these are effectively the same (but structs are lighter weight, and act as value types, as opposed to classes, which act as reference types. It’s up to you to decide which is more appropriate for your needs). Instead of using a complex data structure, you can simply use an ordinary array of these structs or classes. The struct or class itself would contain 2 public fields, one of type GameObject, and one which specifies the threat level. This technique is useful when you need to store *related *data of multiple types, and is especially useful when dealing with collections such as arrays.

Ahh you cleared up a lot, thanks! I was about to use a dictionary, but it didn't look very efficient. But yeah that is what I am after, putting classes inside the array. As for structs, I have never used them. But they sound even better than classes for this kind of thing. Cheers :) Magnus

–

Also just to note, I think i will use a List instead of an array, as them seem even faster :P

–

Oh right sorry :/ 'As long as you don't need to insert or delete elements'. But I need to do that, a lot. Whenever a target dies, or its threats=0, it needs to be removed from the list. And whenever it attacks the AI, it gets added to the list. I just heard somewhere that list are faster for that sort of thing. Funny you should mention Big O, learning about that as this very moment :P Thanks for the rundown. Anyway so as you previously stated, you recommend me using dictionaries for this problem?

–