- Home /
Can we use a string instead a List<> name ? [C#]
Hey everybody ! I'm making a RPG and i need to check if all guards groups in a city are full life. To make that, i use a List who contain 5 list of Guards (PNJ). So, my question is : Can i do something like this :
[Header("Guards lists")]
public List<List<PNJ>> listGroupOfGuard;
public List<PNJ> baseGroupOfGuards1;
public List<PNJ> baseGroupOfGuards2;
public List<PNJ> baseGroupOfGuards3;
public List<PNJ> baseGroupOfGuards4;
public List<PNJ> baseGroupOfGuards5;
public List<PNJ> groupOfGuards1;
public List<PNJ> groupOfGuards2;
public List<PNJ> groupOfGuards3;
public List<PNJ> groupOfGuards4;
public List<PNJ> groupOfGuards5;
public void RespawnGuards()
{
string j = "baseGroupOfGuards";
string k = "groupOfGuards";
string l = "";
string m = "";
for (int i = 1; i < 6; i++)
{
l = j + i; // l = "baseGoupOfGuards" + i (this is the prefabs) (guards can't be killed)
m = k + i; // m = "groupOfGuards" + i (this is not the prefabs) (guards can be killed)
foreach (List<PNJ> listPNJ in listGroupOfGuard)
{
if((List<PNJ>)m != (List<PNJ>)l)
{
(List<PNJ>)m = (List<PNJ>)l;
}
}
}
}
Or i must make something for each groups ?
Thank you !
Bye, xyHeat
Answer by ScaniX · Jul 30, 2016 at 04:44 PM
Well, you cannot cast a string to a list. I am not sure what you want to achieve, so I will just take a guess. :)
public List<List<PNJ>> listBaseGroupOfGuard = new List<List<PNJ>>();
public List<PNJ> baseGroupOfGuards1 = new List<PNJ>();
public List<PNJ> baseGroupOfGuards2 = new List<PNJ>();
public List<PNJ> baseGroupOfGuards3 = new List<PNJ>();
public List<PNJ> baseGroupOfGuards4 = new List<PNJ>();
public List<PNJ> baseGroupOfGuards5 = new List<PNJ>();
public List<List<PNJ>> listGroupOfGuard = new List<List<PNJ>>();
public List<PNJ> groupOfGuards1 = new List<PNJ>();
public List<PNJ> groupOfGuards2 = new List<PNJ>();
public List<PNJ> groupOfGuards3 = new List<PNJ>();
public List<PNJ> groupOfGuards4 = new List<PNJ>();
public List<PNJ> groupOfGuards5 = new List<PNJ>();
void Start() {
listGroupOfGuard.Add(groupOfGuards1);
listGroupOfGuard.Add(groupOfGuards2);
listGroupOfGuard.Add(groupOfGuards3);
listGroupOfGuard.Add(groupOfGuards4);
listGroupOfGuard.Add(groupOfGuards5);
listBaseGroupOfGuard.Add(baseGroupOfGuards1);
listBaseGroupOfGuard.Add(baseGroupOfGuards2);
listBaseGroupOfGuard.Add(baseGroupOfGuards3);
listBaseGroupOfGuard.Add(baseGroupOfGuards4);
listBaseGroupOfGuard.Add(baseGroupOfGuards5);
}
Now unless you are reassigning the members baseGroupOfGuards?
and groupOfGuards?
, you can do this to get a reference to those lists in a loop (without using reflections):
public void RespawnGuards()
{
List<PNJ> l;
List<PNJ> m;
for (int i = 1; i < 6; i++)
{
l = listBaseGroupOfGuard[i-1]; // == baseGroupOfGuardsX, where X == i
m = listGroupOfGuard[i-1]; // == groupOfGuardsX, where X == i
// Do something with those groups like refill m by instantiating something from l
// Sorry, I wasn't able to guess what your loop body was supposed to do. :)
}
}
Thank you for your answer !
" // Do something with those groups like refill m by instantiating something from l // Sorry, I wasn't able to guess what your loop body was supposed to do. :) "
This loop will refill groups which aren't full like you said :)
Thank you again !
Bye, xyHeat