Reputation: 21
I am currently trying to copy sections of one list to another list and I am having a hard time.
I am very new to using lists.
I have created a constructor.
Then I have created a list that contains all of the items that I need. I want to copy a random piece from my first list into my second list. I then want to remove the piece that I copied from my the original list.
This is the current code I have.
My Constructor Code
using UnityEngine;
using System.Collections;
public class Character_Setup {
// Declare Attributes
public int Agility = 1;
public int Stamina = 1;
public int Strength = 1;
public int Critical_Hit = 1;
public string Weapon ="";
// Declare Stats
public int Speed = 1;
public int Health= 2;
public int Damage = 3;
public int Acuity = 4;
public Character_Setup(string _name)
{
Weapon = _name;
Speed = Agility * Random.Range (1,10);
Health = Stamina * Random.Range (1,10);
Damage = Strength * Random.Range (1,10);
Critical_Hit = Acuity * Random.Range (1,10);
}
}
This is my current attempt at the code to copy from one group to another.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Game_Play : MonoBehaviour {
public int Num_Players = 11;
public int Players = 1;
// Use this for initialization
void Start () {
//Create a list
// Using Character_Setup
//add 10 characters to list
List <Character_Setup> Characters = new List<Character_Setup> ();
Characters.Add (new Character_Setup ("Flamethrower"));
Characters.Add (new Character_Setup ("Shotgun"));
Characters.Add (new Character_Setup ("Rifle"));
Characters.Add (new Character_Setup ("Pistol"));
Characters.Add (new Character_Setup ("Machine Gun"));
Characters.Add (new Character_Setup ("Grenade Launcher"));
Characters.Add (new Character_Setup ("Knife"));
Characters.Add (new Character_Setup ("Rocket Launcher"));
Characters.Add (new Character_Setup ("Throwing Star"));
Characters.Add (new Character_Setup ("Sling Shot"));
/*foreach (Character_Setup character in Characters) {
print (character.Weapon);
}*/
// Create 2 teams Off 3 Players
// Team 1
// Create Blue Team
List<Character_Setup> Blue_Team = new List<Character_Setup> ();
Blue_Team.AddRange (Characters);
foreach (Character_Setup character in Blue_Team)
{
print (character.Weapon);
}
// Team 2
//Create Red Team
//List<Character_Setup> Red_Team = new List<Character_Setup> ();
//Red_Team.AddRange (Characters);
}
// Update is called once per frame
void Update () {
}
}
This code currently copies everything over. I want to be able to select something within Characters and copy it to Blue_Team.
I have been through the internet and I am a bit lost at the moment.
What can I do to copy just one section out of the characters list and then remove it?
Upvotes: 1
Views: 684
Reputation: 21
First thanks for the answers. They did not give me the direct answer but they did lead me on the right path which led me to my answer. This is exactly what I needed.
I have also learnt to be clearer with my code and comments.
This is the code that I have written to achieve what i wanted.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Game_Play : MonoBehaviour {
public int Num_Players = 11;
public int Players = 1;
// Use this for initialization
void Start () {
//Section 1
//Create a list
// Using Character_Setup
//add 10 characters to list
List <Character_Setup> Characters = new List<Character_Setup> ();
Characters.Add (new Character_Setup ("Flamethrower"));
Characters.Add (new Character_Setup ("Shotgun"));
Characters.Add (new Character_Setup ("Rifle"));
Characters.Add (new Character_Setup ("Pistol"));
Characters.Add (new Character_Setup ("Machine Gun"));
Characters.Add (new Character_Setup ("Grenade Launcher"));
Characters.Add (new Character_Setup ("Knife"));
Characters.Add (new Character_Setup ("Rocket Launcher"));
Characters.Add (new Character_Setup ("Throwing Star"));
Characters.Add (new Character_Setup ("Sling Shot"));
foreach (Character_Setup character in Characters) {
print (character.Weapon);
}
//Section 2
// Create 2 teams Off 3 Players
// Team 1
//Create Blue Team
//use loop to fill team
//Create Variable to hold selection number
//Add to team
//Remove from Characters availble
List<Character_Setup> Blue_Team = new List<Character_Setup> ();
while (Blue_Team.Count < 3)
{
Character_Setup PlayerSelection = Characters [UnityEngine.Random.Range(0, Characters.Count)];
Blue_Team.Add (PlayerSelection);
Characters.Remove(PlayerSelection);
}
foreach (Character_Setup character in Blue_Team)
{
print (character.Weapon);
}
// Team 2
//Create Red Team
List<Character_Setup> Red_Team = new List<Character_Setup> ();
while (Red_Team.Count < 3)
{
Character_Setup PlayerSelection = Characters [UnityEngine.Random.Range (0, Characters.Count)];
Red_Team.Add (PlayerSelection);
Characters.Remove(PlayerSelection);
}
foreach (Character_Setup character in Red_Team)
{
print (character.Weapon);
}
}
Upvotes: 1
Reputation: 328
If I understand what you're asking, this code will copy a single Character
to Blue_Team
based on the Character.Weapon
value, but this can be changed to whatever you like.
Character selectedChar = Characters.Where(objChar => objChar.Weapon == "Knife");
Blue_Team.Add(selectedChar);
If you want several objects, you can return a list instead with this code:
List<Character> selectedCharList = Characters.Where(objChar => objChar.Weapon == "Knife").ToList();
Blue_Team.AddRange(selectedCharList);
This approach uses System.Linq
, which is incredibly handy when working with lists. I use it all the time.
Upvotes: 1
Reputation: 1799
One way is to have a Team
property in your Character_Setup
class, then you can select who goes into what list by that property. Should have an enum backing it.. but to get started just add public int Team = 0
, then in the constructor allow assigning Team = 1
for "blue team" and Team = 2
for "red team" (team 0 is "no team", up to you if you want to support that or not, if not just allow 1 and 2, not 0, 1, 2).
Then you can do, Blue_Team.AddRange(Characters.Where(c => c.Team == 1));
.
Another way to do it without LINQ is like this:
foreach (var charact in Characters)
{
if (charact.Team == 1)
{
Blue_Team.Add(charact);
}
if (charact.Team == 2)
{
Red_Team.Add(charact);
}
}
Upvotes: 0