The problem is your code structure is completely fubar. Let's start with the host portion. After it creates the network and listens for clients, it'll go on to an infinite do-loop. There is no condition where it will ever exit that loop and therefore you won't be able to press the other virtual button. It's the same story for "join" portion of your code. And you've used NetID for the serve and client. Use separate variables.
I've never used the networking commands before, and frankly it confuses me. The way the help files make it sound like, you have to grab all clients before you can do anything after creating the network. Which to me is strange behavior as how can any clients connect before the network has been established?
From what I've read in the help docs about these commands and looking at your code, I'd say the client never gets the variable from the server because you're setting "var" before any clients have connected. So unless the server has persistent data and pushes old values to new clients (highly doubt it) then "var" simply doesn't exist when you try to read it. I don't really like how GetNetworkClientInteger() returns 0 if there's nothing to read. What if I wanted to send a value of 0 to clients?
This is untested and I barely have an understanding of the multiplayer commands, but I *think* this would work.
clients as integer[] // list of clients the server can see
myClients as integer[] // list of clients as THIS client can see
do
// To start the server
if GetVirtualButtonPressed(1)
// Create the network
ServerID = HostNetwork("NetworkName", "HostName", port)
clients.insert(GetNetworkFirstClient(ServerID))
endif
// To connect the client
if GetVirtualButtonPressed(2)
clientId = JoinNetwork(GetEditBoxText(1), port, "ClientName")
if GetNetworkExists(clientId)
myClients.insert(GetNetworkFirstClient(clientId))
endif
endif
// Press 's' to set "var" on the server
if getRawKeyPressed(83) = 1
SetNetworkLocalInteger(ServerID, "var", random(2, 50), 0)
endif
// Server network has been created, listen for clients
if ServerID > 0
nextClient = GetNetworkNextClient(ServerID)
if nextClient > 0 then clients.insert(nextClient)
endif
// Client connected to server
if clientId > 0
// Listen for other clients that connect after you
nextClient = GetNetworkNextClient(clientId)
if nextClient > 0 then myClients.insert(nextClient)
// Watch for variables
varFromServer = GetNetworkClientInteger(clientId, myClients[0], "var")
if varFromServer > 0
receivedValue = varFromServer
endif
endif
print("client pulled value from server: " + str(receivedValue))
sync()
loop