- Home /
Trouble with Add-ing and Retrieving from a List of Custom Class
Hi Folks,
I would like to love Lists, really, and I'm sure there is lots to love, however...
I'm attempting to use a List of Custom Class and when I retrieve from any of the items in the List, they are ALL of the last item I .Add-ed to the List. Crazy yeah!
Here is the Class:
public class Transport{
var position : Transform;
var confirmation : int;
var up : boolean;
var down : boolean;
var attempts : int;
public function Transport(){
this.position = null;
this.confirmation = 0;
this.up = false;
this.down = false;
this.attempts = 0;
}
}
Here are the declaration of two Lists one of the Custom Class "Transport" and a test List of int and a temp variable to .Add to the List of Transport: [outside any function]
var transport_list : List.<Transport>; // List of nergs to transport up
var xport : Transport;
var carp_list : List.<int>; // List of carp
Here is the instantiation (not sure if that is quite the right term there - please correct if there is a more accurate one - thanks :) [inside Start]
transport_list = new List.<Transport>();
xport = new Transport();
carp_list = new List.<int>();
Here is where I .Add to the Lists:
xport.confirmation = confirmation;
xport.position = loc;
transport_list.Add(xport);
carp_list.Add(confirmation);
Here is where I retrieve from the Lists (I included both variable and hardcoded indices for troubleshooting):
function show_transport_list(){
var i : int;
for(i=0; i<transport_list.Count; i++){
switch(i){
case (0):
dl("mothership : transport_list[i].position.position = 'hardcoded' " + transport_list.Count +" / 0 / " + transport_list[0].position.position + " / " + transport_list[0].confirmation);
dl("mothership : transport_list[i].position.position = 'variable ' " + transport_list.Count +" / "+ i +" / " + transport_list[i].position.position + " / " + transport_list[i].confirmation);
break;
// The other cases from case (1) to case (3) are here but it was getting too lengthy. you will see the output of it though
case (4):
dl("mothership : transport_list[i].position.position = 'hardcoded' " + transport_list.Count +" / 4 / " + transport_list[4].position.position + " / " + transport_list[4].confirmation);
dl("mothership : transport_list[i].position.position = 'variable ' " + transport_list.Count +" / "+ i +" / " + transport_list[i].position.position + " / " + transport_list[i].confirmation);
break;
}
}
for(i=0; i< carp_list.Count; i++){
switch(i){
case (0):
dl("mothership : carp_list[i] = 'hardcoded' " + carp_list.Count +" / 0 / " + carp_list[0] + " / " + carp_list[0]);
dl("mothership : carp_list[i] = 'variable ' " + carp_list.Count +" / "+ i +" / " + carp_list[i] + " / " + carp_list[i]);
break;
// The other cases from case (1) to case (3) are here but it was getting too lengthy. you will see the output of it though
case (4):
dl("mothership : carp_list[i] = 'hardcoded' " + carp_list.Count +" / 4 / " + carp_list[4] + " / " + carp_list[4]);
dl("mothership : carp_list[i] = 'variable ' " + carp_list.Count +" / "+ i +" / " + carp_list[i] + " / " + carp_list[i]);
break;
}
}
}
And what we've all been waiting for..here is the output of the retrieval:
mothership : transport_list[i].position = 'hardcoded' 3 / 0 / (232.0, 24.1, 235.0) / 1004
mothership : transport_list[i].position = 'variable ' 3 / 0 / (232.0, 24.1, 235.0) / 1004
mothership : transport_list[i].position = 'hardcoded' 3 / 1 / (232.0, 24.1, 235.0) / 1004
mothership : transport_list[i].position = 'variable ' 3 / 1 / (232.0, 24.1, 235.0) / 1004
mothership : transport_list[i].position = 'hardcoded' 3 / 2 / (232.0, 24.1, 235.0) / 1004
mothership : transport_list[i].position = 'variable ' 3 / 2 / (232.0, 24.1, 235.0) / 1004
mothership : carp_list[i] = 'hardcoded' 3 / 0 / 1002 / 1002
mothership : carp_list[i] = 'variable ' 3 / 0 / 1002 / 1002
mothership : carp_list[i] = 'hardcoded' 3 / 1 / 1003 / 1003
mothership : carp_list[i] = 'variable ' 3 / 1 / 1003 / 1003
mothership : carp_list[i] = 'hardcoded' 3 / 2 / 1004 / 1004
mothership : carp_list[i] = 'variable ' 3 / 2 / 1004 / 1004
The tell tale is that the last entry in both of the Lists is represented by the same value in the last entry in the List "carp_list" and in ALL entries of the List "transport_list".
What in the world am I doing incorrectly?
Thank you :)
I'm not entirely sure about the List.
syntax over List
but I'm going to write that off as syntactic sugar (although that might very well be the problem)
I want to see more of your add code. I think you're either adding the same thing to the list multiple times, or you're accessing the same item multiple times.
I'd add a string "name" to your transport class for debugging. When you create an instance of transport, give it a unique name, then tweak your output to say something like `Debug.Log("transport_List[" + i + " ] = " + transport_list[i].name);
That way, you could see if you're adding the same thing multiple times.
@Bustedkrutch does create a 'new' object:`xport = new Transport();`.. it looks like it should be right, but I don't know if that new object is created for each object, or just once
You guys/gals? are awesome!!!
I think I know how to solve it now, with your input. I'll be right back.
Excited!
Thank you both, @perchik and @gheeler.
@gheeler - you are correct sir! I needed to use the a "new" type before adding it to the List. BTW, can you add an answer so that I can indicate it as an answer. We are currently only in the comments section and I'd like to give you the karma you're due :)
@perchik - you are right that I had used the "new" for xport, however I used it only once in Start, which totally makes sense now that all of the output was referencing the exact same object.
So here was the solution that @gheeler was writing about:
var xport = new Transport();
xport.confirmation = confirmation;
xport.position = loc;
transport_list.Add(xport);
Just a comment about your constructor for the Transport class, namely that it's not doing anything, since those are the default values anyway. It would be a lot more useful to have a constructor that takes user-supplied values.
public class Transport{
var position : Transform;
var confirmation : int;
var up : boolean;
var down : boolean;
var attempts : int;
public function Transport(position : Transform, confirmation : int){
this.position = position;
this.confirmation = confirmation;
}
}
Then you can just do var xport = new Transport(loc, confirmation);
.
Answer by gheeler · Aug 30, 2013 at 04:01 PM
i dont know js as well as c# but when youre adding to the list, you need to make sure its a new object rather than changing the reference and just passing that.
in c# anyway there'd be a need for the 'new' keyword
although i think but im not sure if your transport class was a struct instead it would work because structs are value types compared to class being a reference type
So here was the solution that @gheeler was writing about:
var xport = new Transport();
xport.confirmation = confirmation;
xport.position = loc;
transport_list.Add(xport);
The line with "var xport = new Transport();" needed to be added each time I added to the list rather than in my original code that only created a single instance of xport in the Start function.
Again, thanks gheeler!
Your answer
Follow this Question
Related Questions
Add Item To A List Of Classes 0 Answers
Lists within lists to make a crafting system 1 Answer
Access list storing custom class variables from another script 1 Answer
How to carry List's and Vector3's to other classes? Interfaces don't work? 4 Answers
How to access individual class elements, that has an array, that is in a list for c# 1 Answer