Hmm. Still couldn't stand the idea that I apparently can't fully get my head wrapped around the type array new definition thing..
dim networkNames[0] as NetworkName I get this is the old obsolete way, so does no longer work.
dim networkNames[networkListSize] as NetworkName As suggested by TomToad and works fine, but from the manual I (mis?)understood this for backwards compatibility only and that the new style of declaring an array is:
networkNames as NetworkName[networkListSize] but that's not working for arraysize must be constant or literal integer.
So I resorted to:
networkNames as NetworkName[] for declaration empty array new style, so far so good.
But then when I hit this part..
// find out if the network that sent the message is already in our list
alreadyInList = 0
for c = 1 to networkListSize
// if the network already is in our list
if networkNames[c].name$ = networkName$ // error: "networknames" has not been defined as an array
// update its time of detection so that we know that it is still active
networkNames[c].lastBroadcast# = time#
alreadyInList = 1
exit
endif
next c
I'm getting an error:
if networkNames[c].name$ = networkName$ I'm getting an error: "networknames" has not been defined as an array
So.. I'm kinda puzzled here. I've used type arrays before, but not in this fashion and don't understand why doesn't work. If anybody has an idea, please let me know?
Full code below:
type NetworkName
name$ // the name of the network detected (this is used to connect to the network using the JoinNetwork command)
lastBroadcast# // this will store the time that the network was last heard from, allowing us to prune dead networks
endtype
// create a global array of networks detected
// the aim is to display this list to the screen
global networkListSize as integer
networkListSize = 0
//dim networkNames[0] as NetworkName // obsolete.
//dim networkNames[networkListSize] as NetworkName // suggested by TomToad, which is the definition for compatibility.
//networkNames as NetworkName[networkListSize] // declaration array new style<< not working for arraysize must be constant or literal integer.
networkNames as NetworkName[] // declaration empty array new style
networkNames[0].name$="" // fill element 0 fields with some empty data as a test, not really needed.
networkNames[0].lastBroadcast#=0.0
// create a broadcast listener to listen for agk networks
// agk networks broadcast their existance intermittently on port 45631
global broadcastListener
broadcastListener = CreateBroadcastListener(45631)
// create a string that will contain the list of networks
// as none have been found, the default value is the no networks found message
global networkListText as string
networkListText = "No Networks Found."
do
updateNetworkList() // update the list of networks stored in networkNames
Print(networkListText) // print the updated list of networks to the screen
Sync() // refresh the screen
loop
function updateNetworkList()
updates = 0 // assume that there have been no updates this cycle
time# = Timer() // save the current time
// get a message (if any) received by the broadcast listener
incomingMessage = GetBroadcastMessage(broadcastListener)
// while there are still messages to process (GetBroadcastMessage will return 0 if there are no more messages)
while (incomingMessage > 0)
// read a string from the message - this will be the name of the agk network
networkName$ = GetNetworkMessageString(incomingMessage)
// find out if the network that sent the message is already in our list
alreadyInList = 0
for c = 1 to networkListSize
// if the network already is in our list
if networkNames[c].name$ = networkName$ // error: "networknames" has not been defined as an array
// update its time of detection so that we know that it is still active
networkNames[c].lastBroadcast# = time#
alreadyInList = 1
exit
endif
next c
// if the network that sent the message is new (because it does not appear in our list)
if alreadyInList = 0
updates = 1 // record that there have been updates this cycle, meaning that the text will need to be updated later
// add the new network to our array and save its time of detection
inc networkListSize
//dim networkNames[networkListSize] // obsolete
networkNames.length = networkListSize // change size of the array new style..
networkNames[networkListSize].name$ = networkName$
networkNames[networkListSize].lastBroadcast# = time#
endif
// delete the message we were processing and move on to the next message
DeleteNetworkMessage(incomingMessage)
incomingMessage = GetBroadcastMessage(broadcastListener)
endwhile
// now that we have processed the broadcast messages, we need to check if any old networks have become inactive
// therefore we cycle through all the network in our list
for c = 1 to networkListSize
// if it has been over 2 seconds since we last heard from a network, we can assume that it has become inactive
if time# - networkNames[c].lastBroadcast# > 2.0
// remove the network from the list
for d = c to networkListSize - 1
networkNames[d].name$ = networkNames[d + 1].name$
networkNames[d].lastBroadcast# = networkNames[d + 1].lastBroadcast#
next d
dec networkListSize
// dim networkNames[networkListSize] // obsolete
networkNames.length = networkListSize // change size of the array new style..
dec c // move the index back so that we don't miss any network this cycle having just deleted one
updates = 1 // record that there have been updates this cycle, meaning that the text will need to be updated later
endif
next c
// if there were any updates this cycle, we need to regenerate the text listing the networks to be displayed to the screen
if updates
// if any networks have been detected
if networkListSize > 0
// add all the networks to the string as a numbered list
networkListText = ""
for c = 1 to networkListSize
networkListText = networkListText + Str(c) + ". " + networkNames[c].name$ + Chr(10)
next c
// if no networks have been detected
else
// reset the message to the default no networks found message
networkListText = "No Networks Found."
endif
endif
endfunction