Triggering an Action with a Mouseclick

From Multiverse

Jump to: navigation, search
The code in this article or section has not been verified with Multiverse platform version 1.5. You can help by testing it, and rewriting as necessary.

Contents

Overview

This example shows how to set up a mob to perform an action when the user clicks on it. This example shows how to make a wolf mob follow the PC, after the user right clicks on it. The example works with the sampleworld example files.

To implement the example:

  1. Put the MvClickExample.py and MvClickExample.xml in your asset repository in the Interface/FrameXML directory. These files can be found at the bottom of this page.
  2. Add MvClickExample.xml to your mars.toc theme file in the Interface/FrameXML directory.
  3. Add the click command to the server config/world-name/extensions-proxy.py file.

Change mob database configuration file

The example depends on adding a property to a mob, indicating that it is a click target, so it will execute a command when the user clicks on the mob.

Add the following line to mv_home/config/worldname/templates.py to set the property on a spawned wolf:

tmpl.put(WorldManagerClient.NAMESPACE, "clickCommand", "/click")

Add it just above this line in the Wolf Template section:

ObjectManagerClient.registerTemplate(tmpl)

Add click command

Finally, add the /click command to your mv_home/config/worldname/proxy.py file, as shown in the following code snippet. In this example, after the user right clicks on the mob, the wolf will follow the player around.

Note: If the wolf is stationary when you right-click on it, then it will follow your player only until the next leg of patrol behavior begins, at which point it will go back to patrol behavior. If you click on the wolf when it is already moving, the patrol behavior will be cancelled, and it will follow your player forever.

class ClickCommand (ProxyPlugin.CommandParser):
   def parse(self, cmdEvent):
       cmd = cmdEvent.getCommand()
       playerOid = cmdEvent.getObjectOid()
       targetOid = cmdEvent.getTarget()
       targetObj = ObjectStub(targetOid, None, "")
       playerHandle = EntityHandle(playerOid)
       followCmd = BaseBehavior.FollowCommandMessage(targetObj, playerHandle, 2000)
       Engine.getAgent().sendBroadcast(followCmd)

proxyPlugin.registerCommand("/click", ClickCommand())

Client files

This section provides the content of the two files required by the Multiverse Client for this example. You must add these two files to your world's asset repository in the /Interface/FrameXML directory.

UI file

Here is MvClickExample.xml:

<Ui>
   <Script file="MvClickExample.py"/>
   <Frame hidden="true">
       <Scripts language="python">
            <OnLoad>
                MvClickExample_OnLoad(this)
            </OnLoad>
            <OnEvent>
                MvClickExample_OnEvent(this, args)
            </OnEvent>
       </Scripts>
   </Frame>
</Ui>

This frame widget defines two events:

  • An onLoad event that calls the MvClickExample_OnLoad() method. This event is triggered when the frame is first loaded.
  • An onEvent event that calls the MvClickExample_OnEvent() method. This event is triggered when any other event is triggered.

The net result is that both methods are called when the frame is initially loaded, and MvClickExample_OnEvent() is called when any other event occurs.

For general information on event handling, see Client Event Handling.

Python script

Here is MvClickExample.py:

from ClientAPI import *

def MvClickExample_OnLoad(frame):
   frame.RegisterEvent("PROPERTY_clickCommand")

def MvClickExample_OnEvent(frame, event):
   if event.eventType == "PROPERTY_clickCommand":
       if event.eventArgs[0] == "any":
           oid = long(event.eventArgs[1])
           objNode = ClientAPI.World.GetObjectByOID(oid)
           objNode.SetProperty("click_handler", MvClickExample_OnClick)

def MvClickExample_OnClick(sender, e):
   ClientAPI.Network.SendTargetedCommand(sender.OID, "/click")

Implementation Details

The way this example works on the client is that a frame is sitting around listening for property updates on world objects. When a property message is received from the server with the key 'PROPERTY_clickCommand', the script looks up the object for which the property message was received, and sets the 'click_handler' property on that object to a method that will send a message to the server.

In the MarsCursor code, the OnRightClick method checks the object that the mouse is over, to see which object was clicked on. If the right click was over an object, the script code will check to see if there is a property on that object named 'click_handler'. If there is, that method will be called. If there is not, a number of mechanisms may be used to handle the click. For example, if the object has a quest, the script will send a message to get information on the quest.

The mouse click handling starts with the entry in bindings.txt that maps BUTTON2 to the RIGHTCLICK action. The Bindings.xml maps the RIGHTCLICK action to calls the MarsCursor.OnRightClick() method. To set up a binding like a left click while the control key is held down, use an entry for CONTROL-BUTTON1.

Personal tools