Hi guys,
I'm working on a network portal where the user can select a network from the available networks list generated by the broadcastlistener, which should be fairly simple.
However I'm bit confused about the workings of CreateText():
When the list of networks on screen, that was first displayed by
CreateText() changes, I use
DeleteText() and then use
CreateText() again to display the modified list, but it doesn't seem to show that items have been removed.
I thought perhaps I need to use ClearScreen() but that didn't work.
Can anybody please tell me why?
To keep things simple and easy to test I've included a simplified
Network Selector and
Host Launcher based on the AppGameKit examples:
Network Selector main.agc
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Network Selector" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Network variables and setup.
Type NetworkNameType
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 to hold detected networks. The aim is to display this list to the screen.
Global networkListSize As Integer
networkListSize = 0 // will be used to change the size of the later on, cannot use it to iterate for that would require a constant or literal value.
networkNames As NetworkNameType[0] // iteration/ declaration of the array (new style, without the 'dim' keyword)
// 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 yet, the default value is the "No Networks" found message..
Global networkListText As String
networkListText = "No Networks Found."
inNetwork=0
// display a screen with all the input fields and instructions as long as Esc hasn't been pressed and we are n
SetClearColor(0,0,0)
ClearScreen()
While GetRawKeyPressed(27)=0
Print( ScreenFPS() )
//Print(networkListText) // print the updated list of networks to the screen (old functionality)
updateNetworkList(networkNames) // display the updayed list of networks stored in networkNames .>> Modified to use Text instead of print command below.
selectNetworkCheck() // check if any network selected by clicking on text. using:
Sync()
EndWhile
Function updateNetworkList(networkNames Ref As networkNameType[] ) // passed networkNames by reference to circumvent the array not being global.
Print( str(networkListSize) + " Network(s) Found " ) // for debug
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 (incomingMessage > 0) // while there are still messages to process (GetBroadcastMessage will return 0 if there are no more messages)
networkName$ = GetNetworkMessageString(incomingMessage) // read a string from the message - this will be the name of the agk network
// find out if the network that sent the message is already in our list..
alreadyInList = 0
For c = 1 To networkListSize // go through all. network:ListSize should be either 0 or more if any detected
// if the network already is in our list
If networkNames[c].name$ = networkName$ // check the list of networknames against the broadcasted network, if it is then..
networkNames[c].lastBroadcast# = time# // update its time of detection so that we know that it is still active
alreadyInList = 1 // mark as akready done.
Exit
EndIf
Next c
// if the network that sent the message is new (because it didn't 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
networkNames.length = networkListSize // change size of the array (new style.); this adds a new element at the end.
networkNames[networkListSize].name$ = networkName$ // add name$ and time# to the end of the resized array.
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
For c = 1 To networkListSize // therefore we cycle through all the network in our list again. e,g. 1 to 8
If time# - networkNames[c].lastBroadcast# > 2.0 // if it has found 1 that has been over 2 seconds since we last heard from a network, we can assume that it(this one)) has become inactive
// remove the network from the list
For d = c To networkListSize - 1 // start from the network we're current;y at 'c' (the one which time has expired) till the end of listsize but take one of . e.g. 4 to 7 ()8-1)
networkNames[d].name$ = networkNames[d + 1].name$ // replace networkname # 4 by networkname #4 +1(in other words shift all up one place)
networkNames[d].lastBroadcast# = networkNames[d + 1].lastBroadcast# // same for time.
Next d // until all the way through list -1
Dec networkListSize
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=1
Print("networkListSize: "+Str(networkListSize))
If networkListSize > 0 // if any networks have been detected
// add all the networks to the string as a numbered list
//networkListText = ""
// 1st delete all texs., if any..
For c = 1 to networkListSize
If GetTextExists( c ) =1
DeleteText(c) // delete all texts.
EndIf
Sync()
Next c
//ClearScreen(): swap()
//2nd displaye the new list
For c = 1 to networkListSize // till the new list size..
// networkListText = networkListText + Str(c) + ". " + networkNames[c].name$ + Chr(10)
If GetTextExists( c )=0
CreateText(c, Str(c) + ". " + networkNames[c].name$ )
SetTextSize(c,30)
SetTextPosition(c,50,50+c*20)
EndIf
Next c
networkListText="Networks Found"
ClearScreen()
Else // if no networks have been detected
// reset the message to the default no networks found message
networkListText = "Networks Found" // original
// Print (networkListText)
//Print ("No Networks Found.") // doesn't show when used print outside mainloop? not sure why..
EndIf
EndIf
EndFunction
Function selectNetworkCheck() // pseudo code..
// check if any network selected by clicking on text. using:
// For c= 1 to networkListSize
// hit=GetTextHitTest (c, GetPointerX ( ), GetPointerY ( ) )
// If hit<> 0
// networkSelected = networkNames[c].name$
// EndIF
// Next
EndFunction
Host Launcher main.agc
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Host Launcher" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
`---------------------------------------------------------------------------------------------------
// setup input and selection fields..
CreateEditBox(1)
SetEditBoxPosition(1,9,30)
SetEditBoxFocus(1,1)
CreateEditBox(2)
SetEditBoxPosition(2,9,135)
Global Selected
Selected = 0
Global networkname$
networkname$=""
While GetRawKeyPressed(13) = 0
Print (" Enter GameName")
networkname$=GetEditBoxText(1)
Print("")
Print("")
Print (" Enter Player name")
nme$=GetEditBoxText(2)
Print("")
Print("")
Print(" (If no names were entered then it generates random ones uppon enter) ")
Sync()
Sync()
EndWhile
// if no name was entered then just generate a random one.
if networkname$=""
networkname$= "AGK - Game "+str(random(2,100))
endif
if nme$=""
nme$= "Player "+str(random(2,100))
endif
// selection has been made so let's get rid of UI elements..
DeleteEditBox(1) : DeleteEditBox(2)
// host a network called ExampleNetwork and give this machine the client name Host
networkId = HostNetwork ( networkname$, nme$+ "(Host)", 1025 ) // initiate host with NetworkName, MyName(currentplayer, and on which port )
SetWindowTitle( networkname$+ " - " + nme$ +"(Host)" )
do
// check that the network is set up ok (if the host returns 0 for isnetworkactive at any point, the network has failed)
if networkId > 0 and IsNetworkActive(networkId)
// print details of the network, including client number (this number will include the host)
Print("Network Active")
Print("Number of Clients: " + Str(GetNetworkNumClients(networkId)))
// cycle through all the clients
clientId = GetNetworkFirstClient(networkId)
while clientId > 0
// print the client details
Print("Client " + Str(clientId))
// if the client has disconnected, it needs to be cleaned up and removed
if GetNetworkClientDisconnected(networkId, clientId)
// if the first item of client user data is not set (0 is default)
if GetNetworkClientUserData(networkId, clientId, 0) = 0
// set the first item of client user data to 1 so that we know we have already cleaned up (clients persist for a few cycles after being deleted)
SetNetworkClientUserData(networkId, clientId, 0, 1)
// delete the client (this will not be instant)
DeleteNetworkClient(networkId, clientId)
endif
Print(" Client Disconnected")
else
// if the client is connected, print its unique identifying name (chosen in the joinnetwork or hostnetwork commands)
Print(" Client Connected")
Print(" Name: " + GetNetworkClientName(networkId, clientId))
endif
// as this machine is the host, we can work out if the current client is the host by checking it against the current machine's id
if clientId = GetNetworkMyClientId(networkId)
Print(" Client Is Host")
else
Print(" Client Is Client")
endif
// move on to the next client
clientId = GetNetworkNextClient(networkId)
endwhile
else
Print("Network Setup Failed")
endif
// update the screen
Sync()
loop