Customized Reality: Creating Local and Networked Events in Jibe and Unity3d

We all share a common physical reality.   A leaf gently falling from a tree, a chilling breeze, a startled rabbit running across a field.  We experience these things in the physical world and know that others experience them the same way at the same moments in space and time.

But what if you could change that?  What if you could customize reality so that different people perceive things happening in the world differently?

We don’t have this ability the physical world.  But it can be done in virtual worlds.

The Difference between Networked and Local Events

A Networked Event is something that everyone in a virtual world sees happening in exactly the same way.  My avatar opens a door, and my friend standing next to me sees it open.  My avatar kicks a ball, and my friend sees it roll up to them.  This is how reality in the physical world works, and in some virtual world platforms like Second Life and Opensim, essentially every event happening in the virtual world is a Networked Event.

A Local Event is something that happens uniquely to each avatar in a virtual world.  My avatar walks up to a dragon who awakes and breathes fire, but my friend behind me sees the dragon sleeping.  My avatar hears a strange sound coming from a dark cave, but my friend hears nothing.  Local Events are triggered and rendered only on the computer screen of an individual person.

In a virtual world like Jibe, you have the power to create and use both types of events.

The Right Reality for the Right Job

For many things, Networked Events are crucial.  You definitely want to see a group of avatars the same way everyone else sees the group.  If an avatar sits down, then everyone else should them sitting.  But there are many cases where Local Events are a better fit.

What if an event is something complex like a flying flock of birds or a field of grass blowing in the wind?  When a Networked Event happens, the same data must be sent to everyone’s computer over the Internet.  All those changes in data being sent to each computer can eat up bandwidth and contribute to the dreaded experience of lag.  If you don’t need everyone to see something happening in exactly the same way, it’s much more efficient to use a Local Event.  Your virtual world will run faster and require less bandwidth.

And what if you want to create a self-guided educational experience in your virtual world, with different students progressing along a series of interactive experiences?  By using a combination of Networked Events (everyone sees everyone else’s avatar in the same way) and Local Events (different interactive modules behaving uniquely for each avatar), you can create an overall experience that is customized to each person as they go through it at their own pace.  You could even create a non-player character (NPC) that follows and assists each student, guiding them along with contextually relevant verbal cues that are only heard by the individual and no one else.

Like most things in life, the trick is to use the right tool for the right job.

Local and Networked Events in Jibe

The movement and positional data of every avatar in Jibe is a Networked Event.  But when you create an interactive and dynamic object like a cube that spins and talks to you when you click on it, you get to decide whether that is a Networked or a Local Event. 

By default, the behavior of all interactive and dynamic objects you build using the Unity editor are Local Events.  This is a good thing since, as I mentioned before, Local Events are much more efficient in terms of bandwidth usage.  I don’t really care if everyone experiences my dancing chickens moving and clucking in precisely the same way.  I’m more concerned with making sure my Jibe world runs smoothly and as lag-free as possible.

But let’s say you want to create a simple interactive and dynamic object like a ball that changes color when you click on it, and you want everyone else in the Jibe world to see it change color at the same time.  How would you go about doing that?

How to Make a Click-triggered iTween Event appear as a Networked Event in Jibe

The simplest way to create interactive objects in Unity that move or change behavior when you click on them is to use iTween.  Today I’ll show you how to create an object in Jibe that, when clicked on, will trigger an iTween event that is seen by everyone else in the Jibe world.

PLEASE NOTE:  This tutorial is a sneak preview!  We’re going to be including the JibeiTweenClick script and the modification to the NetworkController script in version 1.3 of Jibe (released soon!).  So, if you’re already using the Jibe Project Kit version 1.3 (or higher), you can completely skip steps #2 & #3.  Just be sure to open the JibeiTweenClick script included in your Jibe Project Kit and read the comments for additional instructions.

- Step 1: Get a copy of the iTween Visual Editor

First of all, you’ll need to grab a free copy of the iTween Visual Editor.  Launch your Unity editor, search for “iTween Visual Editor” in the Asset Store, and import it into your Jibe project.

Opening the Asset Store from inside the Unity editor.

Import the iTween Visual Editor into your Jibe project.

- Step 2: Create a new script called “JibeiTweenClick”

Create a new C# script in the Unity editor.

Creating a new C# script in the Unity editor.

Copy and paste the following code into your script.

/* Copyright (c) 2009-11, ReactionGrid Inc. http://reactiongrid.com
* See License.txt for full licence information. All other rights reserved.
*
* JibeiTweenClick.cs Revision 1.3.1105.11
* Synchronise iTween events across the network  */

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class JibeiTweenClick : MonoBehaviour {

// Name of iTween event to fire – this value shows up in the Inspector
public string iTweenEventToFire;

void OnMouseDown()
{
// when the user clicks, run the event and send notification over the network
RunEvent();
SendEventNotification();
}

void SendEventNotification()
{
// Sending two pieces of information – one is the name of this object,
// the other is the name of the iTween event (in case we have more than one)
Dictionary<string, string> dataToSend = new Dictionary<string, string>();
dataToSend["SendingObjectName"] = gameObject.name;
dataToSend["iTweenEventToFire"] = iTweenEventToFire;
// get a reference to the Network Controller and send the message
NetworkController netController = GameObject.Find(“NetworkController”).GetComponent<NetworkController>();
Debug.Log(“Sending data”);
netController.SendCustomData(dataToSend);
}

void RunEvent()
{
// Run the actual iTween event – either in response to mouse click
// or in response to network message
iTweenEvent.GetEvent(gameObject, iTweenEventToFire).Play();
}

public void ProcessEvent(Dictionary<string, string> dataReceived, string sendingUserName)
{
// Called from NetworkController when a custom message is received.
foreach (KeyValuePair<string, string> dataItem in dataReceived)
{
if (dataReceived["iTweenEventToFire"] != null)
{
iTweenEventToFire = dataReceived["iTweenEventToFire"];
RunEvent();
}
}
}
}

Save the script and name it JibeiTweenClick.

- Step 3: Modify the NetworkController Script in your Jibe project

We’re going to make a slight modification to the NetworkController script in your Jibe project.  Find the script in your Jibe project folder and edit it.

Open and edit the NetworkController script in your Jibe project folder.

Go to line # 537 of the NetworkController script and add the following code immediately after it.

Dictionary<string, string> dataReceived = e.CustomData;
IJibePlayer player = e.SendingPlayer;

if (dataReceived["SendingObjectName"] != null)
{
GameObject customDataObject = GameObject.Find(dataReceived["SendingObjectName"]);
if (dataReceived["iTweenEventToFire"] != null)
{
customDataObject.GetComponent<JibeiTweenClick>().ProcessEvent(e.CustomData, e.SendingPlayer.Name);
}
else
{
customDataObject.GetComponent<SampleCustomData>().ShowReceivedData(e.CustomData, e.SendingPlayer.Name);
}
}

Here’s how it should look once you’ve added it.

The new code (highlighted) inside the NetworkController script.

- Step 4: Create a sphere and add an iTween event to make it change color from green to red

Create a sphere GameObject in your scene.

Creating a new sphere GameObject in your Unity scene.

Your new sphere in your Unity scene.

Let’s make it a green sphere.  Find the Green shader in your Jibe project and drag it onto your sphere.

Drag this onto your sphere to make it green.

Now you have a green sphere.

While you have your sphere selected in your Scene, add an iTween event.

Adding an iTween event to your sphere.

You could make any iTween event that you like (here’s a complete list of what all the different iTween commands do).  But for today, we’re just going to make an iTween event that causes the sphere to change from green to red.

Your sphere with an iTween event to make it change from red to green.

We want the event to only start when someone clicks on it, so remember to uncheck the “play automatically” box.  And name your iTween event “changetored.”

- Step 5: Add your “JibeiTweenClick” script to your sphere

Drag your JibeiTweenClick script from your Project folder onto your sphere.

JibeiTweenClick script and iTween event ready to go in your sphere!

Enter the name of your iTween Event (changetored) in the inspector field for your JibeiTweenClick script.

You’re done!

Fire up your Jibe world and invite a friend to join you.  When anyone clicks on the green sphere, everyone in the Jibe world will immediately see it change to red.

“I think this ball belongs to the big kitty behind us.  Be careful, man!”

Visit my Jibe world to see an example of this sphere, and check out my other tutorials if you’d like to learn how to do even more with Jibe.  Have fun!

-John “Pathfinder” Lester

6 thoughts on “Customized Reality: Creating Local and Networked Events in Jibe and Unity3d

  1. Pingback: How to add a Memory Puzzle Game to your Jibe world | Be Cunning and Full of Tricks

  2. Hello!

    May I extrapolate from this to assume that the following is possible?

    I can have multiple human or animal meshes which, as networked events, appear to everyone as holding place in an idle animation.

    But, I can have local triggers that a single (local) user can trip which would set into motion a series of other behaviors/animations, including having the mesh locally follow the user, while everyone else continues to see that mesh standing still in idle.

    Correct?

  3. Great explanation.
    I’m curious as to the pieces that made this a networked tween as opposed to a local one. Is it the use of the network controller in the JibeiTweenClick script?

    Wondering so that if I create something separate from scratch I’ll know which pieces to incorporate.

  4. Oh and a comment. For those using Jibe 1.3 this will not work until you un-comment a line in the “JibeiTweenClick.cs” script. The comments tell you which line to un-comment, it’s in “RunEvent()”.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s