
Our world has only two residents:
and they can't see eachother ! :-)
We need to create, using 3ds max two new objects who will be the "real bodies" (oops!) of our residents.
The names defined in 3ds Max are: "red" and : "ye"
Our idea is: doe will see admin and, when admin walks, doe will see his walk! And vice-versa.
We will use messages transmited between the two users sending his positions (transforms) continualy.
This is not a good techinical aproach because will use lots of CPU, but is simple for learning.
In the avatarobj script, in the control method - that is a loop - we will add the code:
on control me
--NEW
-- conn is a flag: ON when we have a connection
if member("username").text = "doe" AND conn = 1 then
myworld.model("red").transform = myworld.model("avatar").transform
whichMsg = myworld.model("red").transform
--the transmission of the position
errCode = gConnect.sendNetMessage("admin", "avatarpos", whichMsg)
if errCode <> 0 then
member("cwin").text = "Error sending message: " & gConnect.getNetErrorString(errCode)
end if
end if
--the same for admin
if member("username").text = "admin" AND conn = 1 then
myworld.model("ye").transform = myworld.model("avatar").transform
whichMsg = myworld.model("ye").transform
errCode = gConnect.sendNetMessage("doe", "avatarpos", whichMsg)
if errCode <> 0 then
member("cwin").text = "Error sending message: " & gConnect.getNetErrorString(errCode)
end if
end if
-- old code ------------------------
if keypressed(126) then
acc = 2
else
acc = 0
end if
etc. etc.
To receive the new position of the other avatar and make the adjustments we have new code in the setupscript
on DefaultMessageHandler
newMessage = gConnect.getNetMessage()
errCode = newMessage.errorCode
if errCode <> 0 then
member("cwin").text = "Error: "& errCode & " : " & gConnect.getNetErrorString(errCode)
else
-- connection message
if newMessage.senderID = "System" AND newMessage.subject = "ConnectToNetServer" then
member("cwin").text = newMessage.subject & " : " & "Connection Successful"
avatarobj.conn = 1
else
-- chat message
if newMessage.subject = "chat" then
member("cwin").text = string(newMessage.senderID & ": "& newMessage.content)
else
--NEW
-- position of the other avatar
if newMessage.subject = "avatarpos" then
avatarobj.svect= newMessage.content
if member("username").text = "doe" then
myworld.model("ye").transform = avatarobj.svect
end if
if member("username").text = "admin" then
myworld.model("red").transform = avatarobj.svect
end if
else
--receiving avatar transform after save/load
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 if
end if
end
This a simple exercive about avatares and multiusers worlds. If you will try to build a "real" game you will have many challenges. A "real" multiuser game can cost some millons of dolares... and many time... and many designers and programmers... Good luck :-)
You can download our exercice in .DIR format:
Try the demo, using your server prepared like we have seen in previous lessons. Open two web pages using the URL of this page. Connect the two users. Walk with one and look in the other movie. When you have the "focus" the avatar will move. There are a delay of 1/2 - 1 minut to the other avatar begins to walk. Try and wait. If your CPU doesn't crash before all will be funny :-(
The chat and the "load" after a save are not working well. Fix this bugs is a "homework" for you :-)
PREVIOUS LESSON NEXT LESSON T. CONTENTS HOMEPAGE
