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.

DarkBASIC Professional Discussion / Frustration with arrays

Author
Message
anwserman
15
Years of Service
User Offline
Joined: 20th May 2011
Location: Wisconsin
Posted: 23rd Jun 2011 13:43 Edited at: 23rd Jun 2011 13:45
Once I think I got arrays figured out, I somehow manage to screw things up. I swear that this code is correct, but I cannot get the S4_PlayerList() array to work.

I deleted out portions of the code that didn't deal with the array at all.



Any suggestions? I have no idea what to do. No matter what, I get an array item count of -1, or -2 if you include the fact I subtract 1 from it as well.

I based this code off of some other working code that I have that involves arrays, but I'm dumbfounded on why this doesn't work
Devonps
16
Years of Service
User Offline
Joined: 5th Nov 2009
Location: Nottingham
Posted: 23rd Jun 2011 14:25
Hi,

It seems to me that you are not actually inserting anything into the s4_PlayerList() array after you issue the command array insert at bottom s4_PlayerList() and that's why you are getting an array count that is less than zero.

So after you issue the command array insert at bottom follow that with actually adding something into the s4_PlayerList() array, don't forget you'll need to populate all the elements of the different UDTs that you reference in your s4_PlayerList array.

Good luck.

Marriage is a circle of rings....
Engagement ring, Wedding ring, Suffering!
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 23rd Jun 2011 19:18

You can't insert arrays into arrays like this.

My signature is NOT a moderator plaything! Stop changing it!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 23rd Jun 2011 19:53 Edited at: 23rd Jun 2011 19:54
Why can't you?



... and due to the fact that arrays have a 'current' item, and that item is set to point to newly inserted items, you could simplify the function a little:


[edit]
This makes it clear to me that there's some other code causing the original problem.

anwserman
15
Years of Service
User Offline
Joined: 20th May 2011
Location: Wisconsin
Posted: 23rd Jun 2011 23:20 Edited at: 23rd Jun 2011 23:46
IanM, like what other code? This code that I provided is the only thing that accesses or uses the arrays. I'm confused to how something else within the code could be causing this to not work.

EDIT: Well, that's stupid. Putting the declaration of the array to the top of the main start-up file caused it to work. So, I'm guessing this is a bug with the DarkBasic compiler then?

If I have a global variable, I shouldn't be forced to put it at the top of my code, especially if nothing is referring to it until a thousand lines later.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 23rd Jun 2011 23:39
Like the bit where you actually call LoadPlayer, and the bit where you actually read back the array (like I've posted in my additions to your code).

If you run my code, you'll see that there's actually nothing wrong with the code you posted, so the problem must be elsewhere.

anwserman
15
Years of Service
User Offline
Joined: 20th May 2011
Location: Wisconsin
Posted: 23rd Jun 2011 23:47
Heh, got it working now IanM. BTW, before I forget, your plugins rock.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 24th Jun 2011 00:28
Quote: "Putting the declaration of the array to the top of the main start-up file caused it to work."

That's not a compiler bug, but a simple requirement based on the fact that arrays are not fixed in size.

If they were fixed in size, then it wouldn't really matter where you declared them as the compiler could simply assume that a DIM anywhere in the code can define and size the array for use.

However, they are not fixed. That means that the compiler can't simply allocate memory to them at compile time - it doesn't know how much to allocate, even if you use a number - you may change the size later, or DIM it multiple times with different sizes. Instead, has to wait until runtime, and the obvious place to allocate the memory for an array is at the DIM statement itself. So it's up to you to allow the DIM statement to execute, and to allocate the memory you specify at that point.

Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 24th Jun 2011 19:37
Quote: "If I have a global variable, I shouldn't be forced to put it at the top of my code, especially if nothing is referring to it until a thousand lines later. "

You don't have to put it at the top of your code, just anywhere where it'll be executed before using the array...

My signature is NOT a moderator plaything! Stop changing it!

Login to post a reply

Server time is: 2026-07-10 17:39:04
Your offset time is: 2026-07-10 17:39:04