Client-server Communication
From Multiverse
Contents |
Getting data from the server to the Client
On the server side, put your data in object properties, and you can get to it from the client. The Sampleworld quest marker script and the animation script are prefect examples.
Quest marker script
The quest marker script is in /Scripts/QuestMarkers.py in the Sampleworld asset repository.
import ClientAPI
questMarkers = {}
def RemoveMarker(worldObj):
# if there is a quest marker, remove it from the object
# ClientAPI.Write("QuestMarkers:RemoveMarker: Name = " + worldObj.Name + ", OID = " + str(worldObj.OID))
marker = questMarkers[worldObj.OID]
if not marker is None:
# ClientAPI.Write("QuestMarkers:RemoveMarker: Removing " + marker.Name)
worldObj.DetachObject(marker)
marker.Dispose()
questMarkers[worldObj.OID] = None
def AddMarker(worldObj, markerMeshName):
ClientAPI.Write("QuestMarkers:AddMarker: Name = " + worldObj.Name + ",
OID = " + str(worldObj.OID) + ", mesh = " + markerMeshName)
marker = questMarkers[worldObj.OID]
if not marker is None:
ClientAPI.Write("QuestMarkers: attempt to add a marker when one is already present")
marker = ClientAPI.Model.Model("marker." + str(worldObj.OID), markerMeshName)
worldObj.AttachObject("questavailable", marker)
questMarkers[worldObj.OID] = marker
def PropertyChangeHandler(worldObj, propName):
if propName == "questavailable" or propName == "questconcludable":
questAvailable = worldObj.CheckBooleanProperty('questavailable')
questConcludable = worldObj.CheckBooleanProperty('questconcludable')
# remove any previous marker
RemoveMarker(worldObj)
# if we need a marker now, add it
if questConcludable:
AddMarker(worldObj, "question.mesh")
elif questAvailable:
AddMarker(worldObj, "exclamation.mesh")
# This function is an event handler that runs when the world has been initialized.
def ObjectAddedHandler(worldObj):
if worldObj.ObjectType == ClientAPI.WorldObject.WorldObjectType.Npc:
# ClientAPI.Write("QuestMarkers: added npc object")
questMarkers[worldObj.OID] = None
# register event handlers for object
worldObj.RegisterEventHandler('PropertyChange', PropertyChangeHandler)
# ClientAPI.Write("QuestMarkers: Registered npc event handlers")
def ObjectRemovedHandler(worldObj):
if worldObj.ObjectType == ClientAPI.WorldObject.WorldObjectType.Npc:
#ClientAPI.Write("QuestMarkers: removing object")
# Remove the marker from the object
RemoveMarker(worldObj)
# now remove the marker from our dictionary
del questMarkers[worldObj.OID]
# remove event handlers for object
worldObj.RemoveEventHandler('PropertyChange', PropertyChangeHandler)
# Register an event handler that will run when the world has been initialized.
ClientAPI.RegisterEventHandler('ObjectAdded', ObjectAddedHandler)
ClientAPI.RegisterEventHandler('ObjectRemoved', ObjectRemovedHandler)
Client animation script
This is the example script /Scripts/ClientAnimations.py in the Sampleworld asset repository.
(TBD)
Calling a server script from the Client
You can call a server script from the client with the following:
ClientAPI.Network.SendTargettedCommand(ClientAPI.GetPlayerObject().OID,
"slash command")
where "slash command" is a string representing a server-based command handler (slash command) to run. See Defining Command Handlers for details.
So, for example, you can put such code in an OnClick event handler for a UI widget such as a button, or you can invoke it from a key binding, as described in Customizing Key Bindings.
