Heya,
Total newb question here and I’m hoping someone can fix my problem in like 5 seconds. I’m using Javascript and trying to use an Array to hold a very short list that will usually have nothing in it, but might on occasion have a few things. It’s a first in, first out, so I’m using Array. Here’s the code…
...
class PopupData
{
var type : PopupType; // an enum
var message : String;
var trackingId : int;
}
class PopupManager extends MonoBehavior {
...
private var popupQueue : Array;
private var curPopup : PopupData;
...
///////////////////////////////////////////////////////////////////
function ShowNextPopup()
{
if (popupQueue.length <= 0) {
curPopup = null;
return;
}
// pop item off queue.
curPopup = [B]__popupQueue.Shift();__[/B]
//Version 2 -- curPopup = (PopupData) popupQueue.Shift();
}
}
Unity gives me this error: Cannot convert ‘Object’ to ‘PopupData’. I tried doing version 2, with a typecast. I assume I’m doing something really simple and dumb, but the documentation explaining Array does not have a SINGLE example that doesn’t use Strings. They never assign to anything typed, and always use the results into a generlc var. I do have #pragma strict turned on, if it matters.
Gigiwoo.
This is the C# - try it:
curPopup = (PopupData)popupQueue.Shift();
I don’t have the time to give better advice (soz).
I had already tried that and it doesn’t compile. Something about expecting a ‘;’. It’s javascript for what it’s worth.
Gigiwoo.
Hi,
may sound like a dumb response but as far as i know UnityScript is case sensitive and Shift must be written with a lower case s: shift.
Also I am sure you could use the .Net/Mono Collections, especially the Queue class or even better the generic form of the Queue class:
Thats exactly the class for your purpose.
[I]import[/I] System.Collections.Generic;
..
private var popupQueue : Queue.<PopupData>;
..
popupQueue = new Queue.<PopupData>();
..
popupQueue.Enqueue(newPopupData);
..
curPopup = popupQueue.Dequeue();
(I am not using UnityScript, so the code is maybe not correct.)
What happens if you create a temp variable with no type, shift into that, then assign it to curPopup? Does it produce the same error?
I’d recommend doing what Marrrk said, and use generic Queue. There’s very little reason to use Array; mostly it should be avoided.
The Array class functions can either be upper or lower case.
–Eric
Heya,
Thanks for the replies. I ended up using List. I coded that up before I realized you guys had replied. So, for other folks that are having trouble with Array, List works just fine. The javascript implementation looks something like this:
private var popupQueue : List.<PopupData>;
// in the Start method.
popupQueue = new List.<PopupData>();
// to add an entry.
var newData : PopupData = new PopupData();
...
popupQueue.Add(newData);
// To use the data
if (popupQueue.Count <= 0) {
return;
}
// pop item off queue.
curPopup = popupQueue[0];
popupQueue.RemoveAt(0);
For what it’s worth, the problem was with typecasting. Which, I finally realized was solved by using ‘as objecttype’. I’m still having trouble typecasting complex things, like an ArrayList of GameObjects for example in this:
var notifyList: ArrayList = notifications[name] as ArrayList;
for (var observer : GameObject in notifyList) {
observer.ANYTHING;
}
The .ANYTHING part doesn’t work. You can’t call GameObjects directly because it’s not a GameObject. So, I had to use a temporary variable. At least with #pragma strict it’s a problem.
Gigiwoo
Should use List. instead of ArrayList.
–Eric