- Home /
Copy From Array to List without reference [C#]
Been looking around for awhile for a solution to this problem and it doesn't sound like something which is simple. A lot of the answers I found online weren't working in unity, were for JS or didn't apply to my application easily. So, thought i'd ask here to see if others can help me. I'm not exactly the greatest coder, so hopefully it isn't a difficult problem.
My script is a summon monster script and the monster being summoned is added to a list of monsters that the player owns. So, I need to copy the monster from the original array into a list so that the player monster can be modified without affecting the parent monster. However, i'm struggling with finding a solution that will achieve this without the parent being modified. Here is the code I have below.
Monster is a class referenced in a previous script. MonsterArray is the full list of monsters and PlayerMonsters is the list of playermonsters. MonsterList is just a temporary list.
public static int randomNum;
public static int randomNum2;
public static Monster SummonedMonster;
public void randomizeMonster()
{
randomNum = Random.Range (1,100);
List<Monster> MonsterList = new List<Monster>();
if (randomNum <= 69)
{
for(int i=0;i<Main.control.MonsterArray.Length;i++)
{
if(Main.control.MonsterArray[i].Rarity == Rarity.Common)
{
MonsterList.Add (Main.control.MonsterArray[i]);
}
}
}
if (randomNum >= 70 && randomNum <= 94)
{
for(int i=0;i<Main.control.MonsterArray.Length;i++)
{
if(Main.control.MonsterArray[i].Rarity == Rarity.Uncommon)
{
MonsterList.Add (Main.control.MonsterArray[i]);
}
}
}
if (randomNum >= 95)
{
for(int i=0;i<Main.control.MonsterArray.Length;i++)
{
if(Main.control.MonsterArray[i].Rarity == Rarity.Rare)
{
MonsterList.Add (Main.control.MonsterArray[i]);
}
}
}
randomNum2 = Random.Range (0,MonsterList.Count);
SummonedMonster = MonsterList[randomNum2];
Main.control.PlayerMonsters.Add (SummonedMonster);
}
Thanks for the help!
Answer by WorpeX · Feb 23, 2015 at 12:47 PM
That worked perfectly! Thank you very much for your help!
I actually had a reason for them to be declared outside the function at one point, but I ended up splitting the script into 2 different scripts and never moved the declarations down. Have done that now, thanks again!
Answer by sniper43 · Feb 23, 2015 at 11:26 AM
You ddin't really specify your problem. I think you need to create a seperate monster using the "new" keyword. See 2.
why are
public static int randomNum; public static int randomNum2; public static Monster SummonedMonster;
static? Read up on the reference and decide if that's the desired behaviour. From what I can see you can easily just delcare them inside the randomizeMonster function, since all other relevant data should be in SummonedMonster
SummonedMonster = MonsterList[randomNum2]; Main.control.PlayerMonsters.Add (SummonedMonster);
Should be
SummonedMonster = new Monster(MonsterList[randomNum2]);
Main.control.PlayerMonsters.Add (SummonedMonster);
or
Main.control.PlayerMonsters.Add (new Monster(MonsterList[randomNum2]));
and you can omit declaring SummonedMonster.
The new keyword creates a new instance of a gameobject. Here's the relevant page: https://msdn.microsoft.com/en-us/library/k6sa6h87.aspx
You might have to modify the Monster script to support creating new objects. Example:
public class Monster
{
float paramA;
float paramB;
string paramC;
}
would turn into
public class Monster
{
public float paramA;
public float paramB;
public string paramC;
public Monster(Monster monster)
{
this.paramA = monster.paramA;
this.paramB = monster.paramB;
this.paramC = monster.paramC;
}
}