Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Ryan Timothy B on Fri 11/06/2010 17:59:40

Title: Issue with static int - Game.InventoryItemCount
Post by: Ryan Timothy B on Fri 11/06/2010 17:59:40
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
Title: Re: Issue with static int - Game.InventoryItemCount
Post by: Monsieur OUXX on Fri 11/06/2010 19:07:07
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?
Title: Re: Issue with static int - Game.InventoryItemCount
Post by: Ryan Timothy B on Fri 11/06/2010 19:51:23
Does it matter?

Anyway, basically this:

int test[Game.InventoryItemCount];
Title: Re: Issue with static int - Game.InventoryItemCount
Post by: Monsieur OUXX on Fri 11/06/2010 19:54:30
Quote from: Ryan Timothy on Fri 11/06/2010 19:51:23
Does it matter?

Anyway, basically this:

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 :

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


Or:

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!
Title: Re: Issue with static int - Game.InventoryItemCount
Post by: Ryan Timothy B on Fri 11/06/2010 20:08:20
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.
Title: Re: Issue with static int - Game.InventoryItemCount
Post by: Khris on Sat 12/06/2010 14:27:40
You could use a dynamic array.

int test[];

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

Title: Re: Issue with static int - Game.InventoryItemCount
Post by: Ryan Timothy B on Sat 12/06/2010 17:31:25
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.
Title: Re: Issue with static int - Game.InventoryItemCount
Post by: Crimson Wizard on Sat 12/06/2010 21:05:10
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.
Title: Re: Issue with static int - Game.InventoryItemCount
Post by: SSH on Sun 13/06/2010 10:27:40
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.
Title: Re: Issue with static int - Game.InventoryItemCount
Post by: Pumaman on Sun 13/06/2010 22:57:34
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.