Issue with static int - Game.InventoryItemCount

Started by Ryan Timothy B, Fri 11/06/2010 17:59:40

Previous topic - Next topic

Ryan Timothy B

This isn't a big issue but it should work since it's a static int.

I was trying to create an array using Game.InventoryItemCount as the size but it says "Array size must be constant value".  But it is defined at build and never changed.

Edit: Changed thread title

Monsieur OUXX

Quote from: Ryan Timothy on Fri 11/06/2010 17:59:40
I was trying to create an array using Game.InventoryItemCount as the size

Could you show your code?
 

Ryan Timothy B

Does it matter?

Anyway, basically this:
Code: ags

int test[Game.InventoryItemCount];

Monsieur OUXX

Quote from: Ryan Timothy on Fri 11/06/2010 19:51:23
Does it matter?

Anyway, basically this:
Code: ags

int test[Game.InventoryItemCount];


Instinctively, I'd say it's because you can only use a real number when you declare that kind of arrays. If you want to use a variable as the size of the array, then you have to use dynamic arrays, created inside a function.

Either :
Code: ags

int myarray[666]; //anywhere in the code


Or:
Code: ags

int myarray[]; //anywhere in the code

function foo() {
   int size = 666; 
   myarray = new int[size];
}


...But you can't mix those 2 methods.

If I'm wrong, then it means AGS has changed a lot since the last time I used it!
 

Ryan Timothy B

#4
I'm saying that since Game.InventoryItemCount is a Static int that never changes once the game builds (unless I'm wrong), I see no reasons why it wouldn't work to declare the size of an array at build.

Anyway, I made a work around that didn't even need arrays.  I actually used a view.

Khris

You could use a dynamic array.

Code: ags
int test[];

// game_start
  test = new int[Game.InventoryItemCount];


Ryan Timothy B

I definitely could've as Ouxx has already pointed out too :P - but of course I already knew about it.  I literally just pointed this out because it bothered me that a static int couldn't be used when its value never changes once the game builds.  It's the only reason why I pointed it out.  Whether it gets fixed or not or is even worthy of the attention is another story.

Crimson Wizard

#7
The fact that Game.InventoryItemCount does not change during run-time is irrelevant. According to scripting language grammar rules, you can't use variable to determine fixed size of an array. And there are (and should be) no exceptions.

SSH

You're confusing constant with static. static means it retains is value between function calls, constant means it is unchanging. And quite often constants aren't, in actual implementation. I think Monkey or Khris wrote up once how to work around this problem with the dynamic array stuff.
12

Pumaman

As SSH says, "static" does not mean "constant".

For example, at some point I might add support to AGS to allow you to dynamically create inventory items at run-time, so the InventoryItemCount would not be constant.

Currently to define an array size you can use a #define (since this is actually constant) or one of the built-in constants such as AGS_MAX_INV_ITEMS. Otherwise, use a dynamic array, as has already been mentioned.

SMF spam blocked by CleanTalk