Home Unity

Storing Enemy Data in an Array (C#)?

Hi all,

I'm working on my Game Development Capstone, which is a turn-based RPG in Unity.

Right now I'm prototyping; and the current stage involves generating enemy encounters.

What I'm not sure about, and where things get a little complicated, is storing each generated enemy's stats in an array.

Enemy encounters generate between 1 to 3 enemies, and for each enemy, I want to generate stats: HP, Attack, Defense, and Speed.

Arrays are confusing to me. I'm trying to wrap my head around them on the Unity documentation and other resources; but other help would be greatly appreciated.

Replies

  • Parkar
    Offline / Send Message
    Parkar polycounter lvl 18
    Unless you are just looking for generic information about object oriented design and data structures, I think you need to ask a more specific question. It's a bit difficult to understand what you are asking right now.

    A shot in the dark though is that you have code running that creates a random number of enemies which are all the same prefab. If this is the case you don't really need any additional arrays just add member variables for each stat to a script attached to one of the members of the prefab. Then you can just generate values for them when you create them either in the script for each enemy or in the script that creates them.
  • CowKing
    What I would do is create a base class that held those stats and whatever other variables that they all share in common, and make a List of that base class that will store a reference to each enemy.
    public class Enemy : MonoBehaviour {[INDENT]public float health;
    public float speed;
    public float attack;
    public float defense;
    
    //whatever else you need
    [/INDENT]}
    
    public class SwordDude : Enemy {[INDENT]//any variables or functions that are special to this class
    [/INDENT]}
    
    using System.Collections.Generic;
    
    public class PlayerOrWhatever : MonoBehaviour {[INDENT]List<Enemy> enemyList;
    
    //cast the enemy list to see what the type is if you need to do
    //specific stuff
    
    void CastStuff() {[INDENT]Enemy tempEnemy = null;
    
    tempEnemy = enemyList[0] as SwordDude;
    
    if (tempEnemy != null) DoStuff();
    [/INDENT]}
    [/INDENT]}
    
    look at the List class in MSDN: http://msdn.microsoft.com/en-US/library/6sh2ey19%28v=vs.80%29.ASPX
  • MikeSeguin
    Ah, okay. This clears things up quite a bit.

    Thank you very much.
  • Lamont
    Offline / Send Message
    Lamont polycounter lvl 16
    I would make an object and use Get/Set.

    EnemyObject Orc = new EnemyObject();
    Orc.name = "Billy";
    Orc.Health = 100;
    Orc.Speed = 10.5f;

    This creates an object that everywhere has access to.

    You can store these enemies in a list and access them by name. Create a random enemy generator... so many things...

    Just an idea.
  • vaynenick
    Btter option is to use C# List

    Nick
Sign In or Register to comment.