WRITE AND READ

If you will save a game you need to save the important data:

In our exercice we will save the avatar transform (position + rotation).

The technique to execute commands in the server is to send a message that has the name of the command and the parameters.

Glued to the SAVE button:

global gConnect,myworld,avatarobj

on mouseUp
  avatarobj.load=0 -- a flag

  whichUserID =  member("username").text
  whichAttribute = "avmus1pos" - attribute created to users
  whichValue = myworld.model("avatar").transform

  
  contents = [ : ]
  attributeContents = [ : ]
  
  addProp attributeContents, whichAttribute, whichValue
  
  addProp contents, #userID, whichUserID
  addProp contents, #attribute, attributeContents
  
   
  errCode = gConnect.sendNetMessage("System.DBUser.SetAttribute", "SetAttribute",  contents ) 
  
  if errCode <> 0 then 
   member("cwin").text = "Error with System.DBUser.SetAttribute command : " & gConnect.getNetErrorString(errCode)   
  end if
  
end

To read the value of the attribute, we glue to the LOAD button:

global gConnect,avatarobj

on mouseUp
  whichUserID = member("username").text
  whichAttribute = "avmus1pos"
  
  errCode = gConnect.sendNetMessage("System.DBUser.GetAttribute", "GetAttribute", [#userID: whichUserID, #attribute: whichAttribute ])
 
  if errCode <> 0 then 
     member("cwin").text = "Error with System.DBUser.GetAttribute command : " & gConnect.getNetErrorString(errCode) 
  end if
   avatarobj.load=1 - a flag to say that we have read the position
end

We need to make some modifications in the setupscript to use the position of the avatar (new code in bold):

on DefaultMessageHandler
   
  newMessage = gConnect.getNetMessage()
  errCode = newMessage.errorCode
  if errCode <> 0 then 
    member("cwin").text = "Error: "& errCode & " : " & gConnect.getNetErrorString(errCode)  
  else
    if newMessage.senderID = "System" AND newMessage.subject = "ConnectToNetServer" then
      member("cwin").text = newMessage.subject & " : " & "Connection Successful"  
    else
      member("cwin").text =  string(newMessage.content[1][1])
      avatarobj.svect=   newMessage.content[1][1]  
      if(avatarobj.load=1) then
          myworld.model("avatar").transform = avatarobj.svect  
          avatarobj.load=0
      end if
    end if
  end if
end
Try the demo, using your server prepared like we have seen in previous lessons. Place the avatar in some position. Connect (user:doe). Save the game. Refresh the page, reconnect and load the position. The precison is not very good :-(



PREVIOUS LESSON NEXT LESSON
T. CONTENTS HOMEPAGE