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 :slight_smile: [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*.position.position = 'hardcoded' " + transport_list.Count +"  /  0  /  " + transport_list[0].position.position + " / " + transport_list[0].confirmation);*

dl(“mothership : transport_list.position.position = 'variable ’ " + transport_list.Count +” / “+ i +” / " + transport_list.position.position + " / " + transport_list*.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*.position.position = ‘hardcoded’ " + transport_list.Count +” / 4 / " + transport_list[4].position.position + " / " + transport_list[4].confirmation);
dl(“mothership : transport_list.position.position = 'variable ’ " + transport_list.Count +” / “+ i +” / " + transport_list.position.position + " / " + transport_list.confirmation);
_ break;
}
}*_

* for(i=0; i< carp_list.Count; i++){*

* switch(i){*

* case (0):*
dl("mothership : carp_list = ‘hardcoded’ " + carp_list.Count +" / 0 / " + carp_list[0] + " / " + carp_list[0]);
dl(“mothership : carp_list = 'variable ’ " + carp_list.Count +” / “+ i +” / " + carp_list + " / " + carp_list*);
_ 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 = ‘hardcoded’ " + carp_list.Count +" / 4 / " + carp_list[4] + " / " + carp_list[4]);
dl(“mothership : carp_list = 'variable ’ " + carp_list.Count +” / “+ i +” / " + carp_list + " / " + carp_list*);
_ break;
}
}
}
And what we’ve all been waiting for…here is the output of the retrieval:_

mothership : transport_list.position = ‘hardcoded’ 3 / 0 / (232.0, 24.1, 235.0) / 1004*

mothership : transport_list*.position = 'variable ’ 3 / 0 / (232.0, 24.1, 235.0) / 1004*
mothership : transport_list*.position = ‘hardcoded’ 3 / 1 / (232.0, 24.1, 235.0) / 1004*
mothership : transport_list*.position = 'variable ’ 3 / 1 / (232.0, 24.1, 235.0) / 1004*
mothership : transport_list*.position = ‘hardcoded’ 3 / 2 / (232.0, 24.1, 235.0) / 1004*
mothership : transport_list*.position = 'variable ’ 3 / 2 / (232.0, 24.1, 235.0) / 1004*
mothership : carp_list = ‘hardcoded’ 3 / 0 / 1002 / 1002
mothership : carp_list = 'variable ’ 3 / 0 / 1002 / 1002
mothership : carp_list = ‘hardcoded’ 3 / 1 / 1003 / 1003
mothership : carp_list = 'variable ’ 3 / 1 / 1003 / 1003
mothership : carp_list = ‘hardcoded’ 3 / 2 / 1004 / 1004
mothership : carp_list = '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 :slight_smile:

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