- Home /
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:
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.
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.
(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")
}
Answer by highpockets · Mar 10, 2021 at 04:01 PM
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 deter$$anonymous$$ed 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.
If I was to do this, I would define my Monsters in a Monster class, put that class on each monster object, define the values in the editor and then have a monsters array in the monster controller class. You don't have a very complicated setup. (scriptable object for each monster would be good to, but lets keep things simple):
//Add this to each monster in the editor and assign values accordingly
public class Monster : MonoBehaviour
{
public string name;
public DamageAmounts[] damageAmounts;
public void Attack()
{
int damage = damageAmounts[Random.Range(0,damageAmounts.Length)];
//deal damage, animate, etc..
}
}
public class MonstersController : MonoBehaviour
{
//Put each monster in the array in the editor
public Mosters[] monsters;
//Attach player transform to this, just an example
public Transform player;
private float strikingDist;
void Update()
{
for(int i = 0; i < monsters.Length; i++)
{
if(Vector3.Distance(player.position, monsters[i].transform.position) <= strikingDist)
{
monsters[i].Attack();
}
}
}
}
You will obviously want to separate the attacks, but this is just a quick and dirty example
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
}
The "monster" is just a static image with a sprite. It's all being done using the UI layer. I have a variable which deter$$anonymous$$es the monster damage. Currently its just a static hard coded value. As in my initial question I can load the monster image, name and damage from three arrays and update the display and the attack value,,, but I can only attack for one specific amount per monster. Thanks for helping.
did you check my code above.. If you need the image or anything else to be related to the monster, just add those properties to the monster class and define it in the editor or make a scriptable object with each one. My code is just an example to get across the idea that you should have each monster defined as an instance of a class with damage amounts, images, names, etc.. then every time you access each monster, you will have access to all of that monster's properties that you defined specifically for that monster
(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")
}
@Nickshepp I keep converting your answers to comments. Just comment when you are not answering the question and it is standard when you mark the answer as "correct" if you feel it answered your question
Yeah, sorry about that, I did convert my last "answer" to a comment after I posted it. This was my first post, and I don't find the interface that intuitive, but like my Unity skills I will try to improve. Thanks again and I will attempt to accept your answer...
Your answer
Follow this Question
Related Questions
Null Ref Exception on a script that should have a reference in it? 2 Answers
How Do I Convert String to String Array In List.Add? 1 Answer
Using similar to GameObject.FindObjectsWithTag but for tag.contains. 0 Answers
How do I create and loop through a completely generic list in javascript? 1 Answer
How to add to list if not in list 3 Answers