How can I store different number of values using lists and arrays?

Hi, sorry for the badly worded question, wasn’t too sure how to phrase it. (noob alert)

So this is just a learning example. I have four monsters in this example, each with an image, a name and an attack value and this works using arrayindex number (initialized at 0) to cycle through them:

private string enemynameslist = { “Alpha”, “Beta”, “Charlie”, “Delta”};
private int enemyattack = { 2, 4, 6, 8 };
private int arrayindexnumber;

What I’m struggling with now is how I could do things like the following:

  1. Make it so the Alpha monster attacks for a random range value (I’ve declared the array as interger of course) - I did consider making it a string array and then converting later.
  2. Make it so the Alpha monster has two attack values, maybe 1 then 2, but really would like to be able to support “as many variations” as I like - I did consider having multiple arrays (attack1, attack2, attack3) etc, and I could probably cobble something together that works but it seems inelegant. Also what if Alpha has two attacks, but Beta has four, or Charlie has 100.

Any advice or reading would be welcome. I’m still in the early stages and sometimes finding the right information is difficult when I don’t really know what to look for.

I don’t really understand exactly what your question is, but I’ll give it a shot. I’m assuming you want to give your enemies the ability to have different attack variations while being randomized.


So in your attack functionality just pick a random attack value where the returned int will be one of the enemyAttacks in the enemyAttacks array which I’m assuming the values in the enemyAttacks array are referring to damage to be served:

int[] enemyAttacks = { 2, 4, 6, 8, 10 };

void Attack()
{
    //get a random attack value from enemy attack array
    int damage = enemyAttacks[Random.Range(0,enemyAttacks.Length)];

   //deal damage if player is hit.. you’ll have to figure this out
}

Hi thanks for the reply, I guess I didn’t word it very well. In my example Alpha deals 2 damage (arrayindex 0 applied to both), Beta deals 4 damage (array index 1 applied to both). So the values for each monster is not random but determined by the matching index is the enemydamage variables. (in this example the four monsters are progressively stronger, dealing 2,4,6 and 8 damage respectively.

But what if I want Alpha to deal 2 damage the first time, then 3 the second and still store this information in an easy adjustable way. I appreciate the method I’ve done above won’t work, looking for suggestions on what to try. Have had a look at lists, arrays, hashtables and none of them seem appropriate.

Perhaps something like an array of lists might do it (if thats possible) so its something like:

private string enemynameslist = { “Alpha”, “Beta”, “Charlie”, “Delta”};
“monsterattackarray”={List1, List2, List3, List4}

“List1”=2,3 (alpha’s attack list)

“List2”=4 (betas attack list)

“List3”=6,0,6,0 (charlies attack list)

“List4”= 8 (deltas attack list)

But I’m not entirely sure if this is possible, advised, or if theres a better way of doing it, plus how the code would work. Hope that explains it a bit better and seriously thanks for replying, I’m still pretty new and still at the stage where I like simple examples that I can adapt and then expand upon.

This is for a card game im writing which is a highly simplified version of slay the spire. I have the deck side sorted out, but presently all the monsters attack for a fixed amount on the enemy turn. I can make this work by using conditional logic in the “monster attack” phase of the code, checking the name and having specific attack logic for each type, but it produces heaps of code. I am looking for a more efficient way of declaring how the monster attacks, instead of (not actual code of course):
attacknumber=0;

if (monster=“alpha”)

{
if attacknumber=0

attack=1

if attacknumber=1

attack=2

if attacknumber=2

attack=random range (0,4)

“Do the Attack for ‘attack amount’ '”

attacknumber++

if attacknumber=3 then attacknumber=0

}

(Scales fall from eyes) Ah I see, sorry I was thrown by the vector 3 related stuff and couldn’t see how that applied. Went code blind I think. Thats all really helpful. I’ll admit that the whole classes thing is something I definately need to find out more on, but I get the core idea now I’ve read your code example properly and your last comment was incredibly helpful. Thank you very much, I know what I need to read up on (I’ve been learning Unity fairly randomly which has taken me so far, but I think its time to go a bit more back to “basics”).

for (var i = 0; i < 3; i++)

{

Debug.Log(“Thank you highpockets”)

}