Atavism Blog

Atavism Blog

Here you will find Blog posts about atavism

  • Home
    Home This is where you can find all the blog posts throughout the site.
  • Categories
    Categories Displays a list of categories from this blog.
  • Tags
    Tags Displays a list of tags that have been used in the blog.
  • Bloggers
    Bloggers Search for your favorite blogger from this site.
  • Team Blogs
    Team Blogs Find your favorite team blogs here.
  • Login
    Login Login form

Creating and Accessing Properties Part II

Posted by on in Uncategorized
  • Font size: Larger Smaller
  • Hits: 2949
  • 0 Comments
  • Subscribe to this entry
  • Print

In Part I we covered creating and adding properties to our characters and mobs. It’s all good having properties on them, but to be of any use we need to access them. This second part covers accessing properties both in-game and at the Character Selection scene along with setting up listeners for when properties change.

 

Accessing/Reading Character Properties at the Selection Scene

Properties can be easily accessed for any character in the selection screen by adding the property key (name) to the end of the CharacterEntry object, such as:

string gender = (string)characterSelected ["gender"];

Where characterSelected is the CharacterEntry object for the currently selected object. The property value will need to be converted to the variable type wanted (such as string or int). Be careful when adding new property getting code here as if the character doesn’t have the property it will error out the Character Selection scene.

Note: The example above or similar can be found in the LoginUI.cs or CharacterSelectionCreation.cs (UMA) files, generally in the DrawCharacterSelectUI() or equivalent.

 

Accessing/Reading Character or Mob Properties in game

Getting player and mob properties in game is done by getting the ObjectNode or AtavismNode for the player/mob and calling GetProperty(“property_name”) on it. For example, in the MobController3D class:

bool dead = (bool)GetComponent<AtavismNode>().GetProperty("deadstate");

The slightly tricky part is getting the ObjectNode/AtavismNode, as depending on the situation there are different ways.

Getting the ObjectNode for the local player is done using:

ClientAPI.GetPlayerObject()  - See PlayerPortrait.cs

 Getting the ObjectNode for the current target for the local player is done using:

ClientAPI.GetTargetObject() - See TargetPortrait.cs

 To get the ObjectNode for any other mob or player you can get it by using:

ClientAPI.GetObjectNode(long oid) – if you have the OID for it

 Or you can get the AtavismNode if you have the gameObject to work with:

gameObject.GetComponent<AtavismNode> () - see Cursor.cs Update() function.

 

It may be a bit confusing at first (especially if you are not a programmer), but if you look through the files mentioned above it should give you a better idea.

 

Creating Property Change Handlers

Property Change Handlers are used to run updates/changes when a property has been changed (such as the health of the player has gone down and the health bar needs to change to reflect that). A listener generally wants set up in either the Start() function of a class, or in the ObjectNodeReady() function when the script is going to be attached to a mob (such as the MobController3D).

Similar to the getting of properties, the registering of a change handler requires either the ObjectNode or AtavismNode and is done in the following format:

ClientAPI.GetPlayerObject().RegisterPropertyChangeHandler(“health”, HealthHandler);

The first parameter is the property to listen for, and the second is the name of the function to run when the stat has changed. The function must match this setup:

public void FunctionName(object sender, PropertyChangeEventArgs args) {
  // Do whatever you want in here
}

 

For the health example for above the HealthHandler function looks like:

public void HealthHandler(object sender, PropertyChangeEventArgs args) {
    health = (int)ClientAPI.GetPlayerObject().GetProperty(“health”);
}

All these few lines of code do (combined with the RegisterPropertyChangeHandler above) is listen for when the health property changes, then set the local health (for the health bar) to whatever the value is the player has now.

 

Title Property Tutorial

Ok so we have now covered how to set a property and get a property, it's now time to try put in a new example in full: a Title property that is displayed before a player’s name (such as a Mr, Mrs, Dr, Warlord, Grand Marshall).

Note: this tutorial is designed for the basic demo project. If you are using UMA or NGUI with your project you may need to edit some other files instead.

Step 1) Editing the Character Creation Code to add the property

Open up the LoginUI.cs file and near the top just under:

List<CharacterEntry> characterEntries;

add a new list for the potential Titles and a string for the chosen title:

public List<string> titles;
string chosenTitle = “”;

 We will then add some code in DrawCharacterCreateUI () to draw buttons for each title option. I have put this just before line 239 - GUILayout.Label("Name:");

GUILayout.Label("Chosen Title: " + chosenTitle);
foreach (string title in titles) {
            if (GUILayout.Button(title)) {
                        chosenTitle = title;
            }
}

 And just below: properties.Add("gender", gender); (at line 255) add:

properties.Add("title", chosenTitle);

 

Step 2) Adding a list of titles to your game.

Load up the Login scene and click on the Main Camera object in the scene. You should now see a new Titles property listed in the Login UI component on it. Here you can specify which titles you would like players to choose. I have attached an image showing how it should look (along with my selection of titles).

b2ap3_thumbnail_TitlesList.PNG

 

Step 3) First Test

With a selection of titles added, you should now play your game and try create a new character. You should see a list of buttons that match your titles and you can click one to set it for your character.

 

Step 4) Altering the display of character names

Now that we can give our characters a title property it's time to get that property in game. For this tutorial I'm going to modify the MobController3D.cs file which draws the names above the heads of characters/mobs.

Open up the MobController3D.cs file and add the following line somewhere in the UI Fields region (lines 111-119 - #region UI Fields):

string title = "";

This is to store the value of the title. Next we will update the OnGUI() function to display the title before the name. Look for these lines:

if (!isPlayer)
            GUI.Label (rect, gameObject.name);

 And modify the second line so it says:

GUI.Label (rect, title + " " + gameObject.name);

You may want to comment out the first line so you can see the title above your character as it will make testing much easier.

Note: Commenting is done by adding // before the line of code.

I also found out that the line up about 5 lines above that says:

Vector2 size = GUI.skin.GetStyle (styleName).CalcSize (new GUIContent (gameObject.name));

needs to be changed to:

Vector2 size = GUI.skin.GetStyle (styleName).CalcSize (new GUIContent (title + " " + gameObject.name));

otherwise it doesn't have enough space to draw the title and name.

 

Step 5) Creating the Property Change Handler

The last step before final testing is to add a property change handler for the title property.

I created a new function called HandleTitle which looks like:

public void HandleTitle (object sender, PropertyChangeEventArgs args)
{
           title = (string)Client.Instance.WorldManager.GetObjectNode(oid).GetProperty("title");
}

 

It can go anywhere in the file, I added it after HandleCombatState().

The final line of code is to set up the registration of the property change handler. Find your way to the ObjectNodeReady() function and just after the line:

GetComponent<AtavismNode>().RegisterObjectPropertyChangeHandler("combatstate", HandleCombatState);

add:

GetComponent<AtavismNode>().RegisterObjectPropertyChangeHandler("title", HandleTitle);

 

Note: the line before may look slightly different, it is changing in update 2.2.

 

Step 6) Final Test

Finally, log in with your new character that has a title and see what their name looks like! Here is how mine turned out:

 b2ap3_thumbnail_WarlordSoomy.PNG

 

Extension activity:

Try get the Title to show up with the player name in the Player Portrait by editing the PlayerPortrait.cs.

 

0

Comments

  • No comments made yet. Be the first to submit a comment

Leave your comment

Guest
Guest Saturday, 29 April 2017

Intel

Unity

S5 Box

Sign On