Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [BUG] When i initialize an array with one element it doesn't work

Author
Message
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 22nd Feb 2016 05:21


displays';
names[0]=
names1[0]=cat

Sign up for NaGaCreMo!
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 22nd Feb 2016 07:45
When you define the array, the number in th [] is the number of elements in that array, however when printing the element, the number in the [] is the number of the element to use - starting from 0, and that is key to the problem.

global names as string[0] = [ "cat" ]

means you are creating an array containing zero elements. Change the zero to 1 like this:

global names as string[1] = [ "cat" ]

and then use it like this:

print ( names[0] )

Result =
Cat.
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 22nd Feb 2016 08:06
I don't like that at all!

Although CJB's code does work. The explanation for why it works means that the code should fall over on this line:
global names1 as string[1] = [ "cat", "dog" ]
Because now we are trying to assign the value "dog" to an element of names1 beyond the length of the array.

I believe there is a bug here and the code should work in the way that blink is trying to use it.
AGK V2 user - Tier 1 (mostly)
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 22nd Feb 2016 08:14 Edited at: 22nd Feb 2016 13:09
I have to admit, that does look like a bug. At first I thought that by assigning the values of "cat" and "dog" to an array of one string, we were dynamically extending the array, but when trying to add "frog" to the same array, you get an error "Number of default values exceeds array size". So yes, looks like a bug.
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
hakimfullmetal
9
Years of Service
User Offline
Joined: 17th Feb 2015
Location:
Posted: 22nd Feb 2016 11:27
But when I tried to assign "cat" as a string for names[0] manually, the print("names[0]="+names[0]) did shows "cat", instead of an empty string



So this bug only occurs when we tried to assign values to arrays with size of[0], by 'directly' assigning it in this form?
global names as string[0] = [ "cat" ]

But will not occur if we do this?
names[0] = "cat"
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 22nd Feb 2016 23:04 Edited at: 22nd Feb 2016 23:19
yes hakimfullmetal. only when initializing a definition.

i think it's an anomaly.
the number in the [] is not in fact the number of elements, it is the index of the last element. ie
array[3] means the array has 4 elements; 0, 1, 2, 3
array[0] means the array has one element 0 but the initialization syntax doesn't seem to work when it's zero
i think the compiler see's zero in the [] and doesn't bother initializing the array
you can also define an empty array like this; array[-1]

Sign up for NaGaCreMo!
Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 23rd Feb 2016 15:33
Fixed for the next version, the bug was with arrays with length 0 (1 element) losing their default value during compiling.

Login to post a reply

Server time is: 2024-11-25 09:27:21
Your offset time is: 2024-11-25 09:27:21