Array and data types [SOLVED!]

Started by NiksterG, Fri 24/10/2008 00:12:20

Previous topic - Next topic

NiksterG

I put this topic here because it deals with the way the engine handles arrays and data types, but if it's in the wrong spot, feel free to move it.

I am trying to make a dynamic array of InventoryItems. However, every time I try to compile, it gives me the error message: "Cannot declare local instance of managed type."

I thought, okay, maybe I just can't declare it in the function, it's got to be declared at the top of my global script. So I do that, and depending on whether I put

"InventoryItem* opponentdeck[]"
or
"InventoryItem opponentdeck[]"


I either get "Cannot declare local instance of managed type."
or
"Parse error in expr near 'new' ". (Respectively).

Is it not possible to make an array of InventoryItems? Or am I doing something wrong?

Here's my code, maybe that will help:
Code: ags

// main global script file
InventoryItem* opponentdeck[];

function ShuffleDeck() {
	opponentdeck = new InventoryItem[OpponentDeck.ItemCount];
       // shuffling code here
}
Check out my games! (Not all made with AGS)

Khris


NiksterG

Ah yes, my favorite type of answer: short, sweet, and to the point.

I guess there's nothing more to add. Hopefully Chris can implement this sometime in the future. I'll have to think of a workaround for the time being.
Check out my games! (Not all made with AGS)

GarageGothic

#3
Short and to the point perhaps, but I'm not sure it's correct. You code seems to compile fine for me, if only I change "OpponentDeck.ItemCount" to another value (such as Game.InventoryItemCount or 50). Dynamic arrays should work for all built in types, so it must cover InventoryItem* too. I'd suggest you look into your OpponentDeck code instead if you want to find the problem. It could be that you're trying to initialize a zero or negative sized array.

Edit: Most likely zero as I now realize your "deck" is in fact an inventory window - good idea.

Khris

Yes, sorry, I was mistaken: according to the manual, it's not possible to create arrays of custom structs, but built-in types should work.

monkey0506

Actually for verification, a static array would work fine with custom structs:

Code: ags
struct my_struct {
  int my_int;
};

my_struct my_vars[20]; // size is defined as integer constant, compiles properly


It's only dynamic arrays that don't work with custom structs (yet):

Code: ags
struct my_struct {
  int my_int;
}

my_struct my_vars[]; // size is undefined, attempting to declare dynamic array of custom type, unsupported and will not compile (CRASHES)


;)

NiksterG

Well, I'm glad that's all cleared up, but I'm still having problems. I'm sticking with the code I posted before, but I still get "Parse error in expr near 'new' ". Just for kicks, I changed the variable OpponentDeck.ItemCount to just a constant (50) but still, same error message. I'm thinking it could be I'm using an older version? (3.0.1)

Any help is appreciated.
Check out my games! (Not all made with AGS)

Pumaman

Ok, I pasted this code:

Code: ags
InventoryItem* opponentdeck[];

function ShuffleDeck() {
  opponentdeck = new InventoryItem[50];
}


into my global script, and it compiled fine.

Can you double-check that the code you've posted is the same as what's actually in your game?

NiksterG

Yep, that's definitely what I've got. I just double- and triple-checked.

This problem's actually been bugging me for a while (about two months O_o)... If it's not the version number, I think I will start over on this project, especially since it does have some sloppy code. I just taken a computer science course, so I actually think I can do better now anyway.

I would like to figure this out either way, though... I'm just that obsessive  ;D

EDIT: I just copy and pasted the above code into a brand new game, and it also seems to work. I don't understand why it does not compile in one game, but it does in the other.  ???
Check out my games! (Not all made with AGS)

Pumaman

If you like, upload your game files and I can take a look and find out why this isn't working for you.

NiksterG

My game files are pretty big for my web server (26MB) and I can only hold about 20 MB at the moment. (Stupid free web hosts, making server size so small... >:()

If you can figure out my other problem (I posted in the AGS RC1 thread) that'll do fine for now. Until then, I'll try to find a bigger web host.
Check out my games! (Not all made with AGS)

Pumaman

Remember that you don't need to upload any of the sub-folders like Compiled, Music, Sound, etc -- only the files in the main game folder would be needed for me to look into this.

NiksterG

Well, since you really seem to want to check it out, I'll upload it. I don't know if the rooms, code, etc will compile correctly, since I did add a number of PlayMusic and other commands. I'm just leaving the code like it is (again, fairly sloppy, not much commenting :P)

The lines of code you are looking for are lines 57 where I declare the dynamic array, and line 772 where I try to set it to a value.

Sorry for the horrible coding... I'm remaking this now, so it should be a little better in version 2.0  ;D

http://www.freewebs.com/magicianxy/niksterg_oldgame.zip
Check out my games! (Not all made with AGS)

Pumaman

Hmm, I un-commented line 772 and it compiled fine.

EDIT: having looked further, it looks like this is a bug that was fixed in 3.0.2 where dynamic arrays didn't work if you had Left-To-Right precedence turned off. So yes, if you wanted to continue making this game then just upgrade to 3.0.2 SP1 and the error should go away.

NiksterG

AHA! I told it was a version problem.  ;D

I downloaded 3.0.2 SP1 and problem disappeared. Thanks Chris! Is there anything you can't solve?  ;)
Check out my games! (Not all made with AGS)

SMF spam blocked by CleanTalk