Casting Issues with Javascript Array and #pragma strict

Hello,

I am using Javascript with #pragma strict enabled, and I am also using Arrays for the dynamic push and pop feature set, because I need to be able to add and remove GameObjects from a list. However when Poping objects from the Array, I receive a downcasting warning since the Array objects are untyped.

How can I avoid this? Is there another object I can use that offers the push and pop functionality?

Thank you in advance.

I think you would need to cast them. Why not use ? it’s faster by far.

Arrays do not have dynamic push/pop’s, I think you’re confused. Arrays are fixed size. Maybe you’re talking about a List or ArrayList? There is a special datastructure called a stack which gives type safe pop/pushing, it’s called System.Collections.Generic.Stack

No, the JS Array class is dynamic and has push and pop. Note the difference between Array and array. However, Hippocode is right: don’t use it. Like, ever. :wink: Use List instead, then you don’t have to cast and it’s faster.

–Eric

I’m only right when I’m not talking about #pragma strict :wink:

It might be called “Array” in javascript, but it is a dynamic list.

Yeah, if you want Push/Pop sort of behaviour, List or JS.Array is probably not the answer, but rather Queue or Stack (or Deque but that has to be downloaded/more work to get).

Queue works like a real life queue, where adding goes to the end, and removing pulls from the front, so the first one in is the first one out.

Stack works like a stack of objects (think Tower of Hanoi puzzle), this is last in, first out.

Deque (pronounced Deck) is short for Double-ended queue, and gives you both options at once.
http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=11754

There are a bunch of other containers as well(if these don’t seem like the best options): System.Collections Namespaces () | Microsoft Learn

Ntero: List and Stack have pretty much identical performance when it comes to poping/pushing elements on the end, they are both implemented with an array as backing storage and the operation is O(1) complexity on both.