CONNECTING

To access the database to save or load a game data, your movie/world/game needs first to access the server.

The method to be executed will be:


connectToNetServer(user, pw, server, 1626, movie)

To use this method we need to create an object:


gConnect = new(xtra "Multiuser")

When we make the connection with the server we will receive an answer message.

After the connection, our contacts with the server will be made sending messages to the server and receiving answer messages.

We need to create a "receptor" for this messages. We do this overriding a method/event: DefaultMessageHandler. This block will be writed in the setupscript - part of our 3dD Basic Kit. We will see the code soon. First, the complete code glued to the button for CONNECT.



global gConnect 
on mouseUp me
  whichName = member("username").text
  whichPass = member("password").text
  whichServer = member("server").text
  -- creating the object
  gConnect = new(xtra "Multiuser")
  -- defining where will be placed the DefaultMessageHandler
  gConnect.setNetMessageHandler(#DefaultMessageHandler, script "setupscript")
  --connecting
  errCode = gConnect.connectToNetServer(whichName, whichPass, whichServer, 1626, "mus1")
  
  if errCode <> 0 then 
    member("cwin").text =  "Error with ConnectToNetServer command : " & gConnect.getNetErrorString(errCode)  
  end if
  
end

In the setupscript we will have some new code. The important:

 
on DefaultMessageHandler
  newMessage = gConnect.getNetMessage()
  errCode = newMessage.errorCode
  if errCode <> 0 then 
    member("cwin").text = "Error: "& errCode & " : " & gConnect.getNetErrorString(errCode)  
  else
    -- only when the message is about the connection
    if newMessage.senderID = "System" AND newMessage.subject = "ConnectToNetServer" then
      member("cwin").text = newMessage.subject & " : " & "Connection Successful"  
    else
      -- normal message
      member("cwin").text =  string(newMessage.content[1][1])
    end if
  end if
end

The normal answer message is a matrix (two property lists nested - in Director dialet). When we are reading a field (an attribute) - our normal case in this application - the value is in the first element. So, we isolate it. We will see what we will do with it in the next lesson.

PREVIOUS LESSON NEXT LESSON
T. CONTENTS HOMEPAGE