Quote: "To have dynamic arrays, I read in help files that you should have Arrays at the top of main code. So my question is, if you have multiple #include code files, and you have Arrays that pertain to stuff in the #include code files, can you put the Arrays at the top of the #include code files that they pertain to and still have them be dynamic, or do all Arrays no matter what have to go at the top of the Main code file to be dynamic like help file says?
Types question. Types delclarations that are part of any Array should go at the top of Main code file above Arrays they pertain to, correct? Or can you put Types that don't pertain to Arrays anywhere, even in #include code files?"
Alright, so this might get a little confusing., but as DBP will Auto-Declare and Assign Variables when you first use them this can cause some issues...
The first being that the Auto-Assignment works based on naming: i.e. A = Integer, A# = Float, A$ = String... the only way to not use the # (for Floats) or $ (for Strings) is to explicitly declare them with the 'As' Keyword.
i.e.
A As Integer
B As Float
C As String
What's more you can't have 2 Variables names the same thing... so let's say you use a Variable at the start of your code: ProgramTicks = Timer( )., but later on you're trying to declare it: Global ProgramTicks As Float... well you're going to have a problem, as the Application will think it's already declared and as a different type.
I'd suggest getting in the habit of explicitly declaring every variable you use, with the common exception is a For...Next Loop Variable; as it's common to use a single letter or such just to have a loop counter.
Now if you DO use explicit declaration, like Types and Arrays... well they'll ONLY be usable AFTER you've declared them.
And as #Include appends everything to the END of the Main Source File (with up to 255 Includes allowed., and yes these can be within other includes; but remember that would be appended to that include THEN to the parent source)
This however is where Code Flow functionality comes into play (i.e. Labels, GoSub and GoTo)
These are quite simple to use... we construct a label with the ':' Symbol at the end of an identifier...
We then have 2 Types of Jump Command
GoSub <Label> // This will jump to the given label, but acts as an anchor to where it was called; so when the Return command is called it will jump back to said Anchor point and continue.
i.e.
myMessage As String : myMessage = "Hello"
GoSub Test
Print myMessage
Wait Key
End
Test:
myMessage = myMessage + " World"
Return
GoTo <Label> // This is slightly different, it isn't an Anchor point, so even IF we do have a 'Return' Command there is nothing to return to... and in-fact it will cause the application to crash if you include it.
i.e.
myMessage As String : myMessage = "Hello"
GoTo Test
// Notice how this code never executes
Print myMessage
Wait Key
End
Test:
myMessage = myMessage + " World"
Print "Wait... where's my message!"
Wait Key
End
In essence we can essentially encapsulate our #Include Declarations in a Label and Return... this would then allow you to call a GoSub at the top of your program to declare everything within said included Source.
Alternatively, said Variables and Types will ONLY be available to that include source and later includes; which is when ordering becomes important.
Now a common trick is to basically name the GoSub something common... so let's say we have a Maths Library Source., well we'd name the file Maths.dba
I tended to use the pre-fix "Include" with the Filename., as it's easy to see that we're in this case GoSub IncludeMaths and that's where all our declarations are and usable by the Main Program.
Quote: "
Can someone give me a plain english explanation of how to update an array as needed in a program?
If you choose to give an example, please don't give code example that has a large amount of code that has nothing to do with the example and question at hand. It makes it hard for people like me to understand what is going on quickly let alone at all. A lot of times people give example and explanations that people don't need just for something that should take a smalll amount of code.
"
There are a few approaches to use., this example I create a Blank Array (with your Types) and have manually added some Template Data.
What the first loop does is Dynamically Grow the Array, while adding each line of Data (at the Bottom)
Then we print the Results in the Main Loop be getting the Data.
Sync On
Sync Rate 60
Disable EscapeKey
Type PlayerLocation
X As Integer
Y As Integer
Z As Integer
EndType
Type OnlinePlayerCharacters
Name As String
Class As String
Location As PlayerLocation
EndType
Global DIM PlayerAccounts(0) As OnlinePlayerCharacters
// Populate the Array
Restore PlayerData
For Account = 0 To 3
Array Insert At Bottom( PlayerAccounts(0) )
Index = Array Count( PlayerAccounts(0) )
Read Info$
PlayerAccounts( Index ).Name = Info$
Read Info$
PlayerAccounts( Index ).Class = Info$
Read Axis
PlayerAccounts(Index).Location.X = Axis
Read Axis
PlayerAccounts(Index).Location.Y = Axis
Read Axis
PlayerAccounts(Index).Location.Y = Axis
Next
Repeat
Cls
// Just print all of the held data
For Account = 1 To Array Count( PlayerAccounts(0) )
Print PlayerAccounts(Account).Name + " - " + PlayerAccounts(Account).Class + " - Pos [" + Str$( PlayerAccounts(Account).Location.X ) + ", " + Str$( PlayerAccounts(Account).Location.Y ) + ", " + Str$( PlayerAccounts(Account).Location.Z ) + "]"
Next
// Simple Check
For Account = 1 To Array Count( PlayerAccounts(0) )
If PlayerAccounts(Account).Name = "Alice"
Print "Account ID " + Str$(Account) + " is Alice"
EndIf
Next
Sync
Until EscapeKey( )
// Clean Up
Empty Array PlayerAccounts(0)
End
// Static Data
PlayerData:
Data "Jim", "Mage", 5, 6, 7
Data "Bob", "Ranger", 1, 4, 8
Data "Alice", "Cleric", 9, 15, 3
Data "June", "Monk", 20, 8, 13
Now typically for Static Data., you'd add a Terminator that is checked for; to know when you've reached the end... this is easier loading from Files or Network Messages., as there are definitive ends.
For example with a File you might write Repeat : // Reading Ops : Until FileEOF( FileID )
But you get the idea.