Setting up SBLS for Unity – The Code

This page takes you through setting up your code to work with SBLS. It requires that you have a SBSL configuration file set up. If you haven’t set up a configuration file see Setting up SBLS For Unity.

Adding the script

Now that you have your configuration file configured, you’ll need to add the SBSLCharacter script to your player. You can do this by either dragging the SBSLCharacter script to the desired Gameobject, or clicking the Add Component button on the GameObject, then selecting Scripts -> SBLS -> SBLSCharacter.

Once the script has been added you’ll see the following options.
Screen Shot 2015-05-08 at 9.55.37 pm

There are only two fields you need to be concerned about.

  • Use Default Config – This is ticked by default and if ticked SBLS will automatically locate the default configuration and use it’s settings. Otherwise..
  • Custom Config – If you want to use a custom config file, add it here and make sure that Use Default Config is unticked

Accessing SBLS

To access SBLS you need to add the following to the beginning of any script that you want to access it in:

using SBLS;

So, the top of your script might be:

using UnityEngine;
using System.Collections;
using SBLS;

Then in the script variables add:

public SBLSCharacter character;

In the Awake or Start method you can then get the component. For example, if the SBLSCharacter script was on the same GameObject as the script you were editing, you’d have:

	void Awake () {
		// Let's get the character information for the player
		character = GetComponent<SBLSCharacter> ();
	}

Currently, you could have a file that looks like:

using UnityEngine;
using System.Collections;
using SBLS;

public class MyController : MonoBehavior {
    public SBLSCharacter character;

    void Awake () {
        // Let's get the character information for the player
        character = GetComponent<SBLSCharacter> ();
    }
}

There are a few methods that you’ll call to make it work, and they can be called anywhere you require them.

Adding XP

If you wish to add or subtract XP to the overall XP of the character use the adjustXP method. The method takes an integer (either positive or negative) and will add or subtract accordingly. It will automatically detect if the player goes up a level.

Ex:

 character.adjustXP(10);

Accessing a Skill

Accessing a skill is one of the most important parts of SBLS. To access a skill use the getSkill method. Get skill takes a string, which is the name of the string.

Ex:

SBLSSkill sk;
sk = character.getSkill("Running");

Alternately, if you wanted to adjust a skills XP with out holding the skill in a variable you can use:

character.getSkill("Jumping").adjustXp(1);

What happens when a skill levels up?

When a skill levels up a message is sent to the current GameObject called skillUpdated, and passes the skill class to the receiver. The following is the code used in the Demo Scene that comes with SBLS.

	// If a skill has been updated this will be called
	void skillUpdated(SBLSSkill sk) {
		AudioSource.PlayClipAtPoint (skillLevelUpClip, transform.position);
		Debug.Log ("Skill updated");
		isSkillUpdated = true;
		updatedSkill = sk;
		CancelInvoke ("hideSkillUpdate");
		Invoke ("hideSkillUpdate", 3.0f);
	}

The receiver is not required for SBLS to work, but it’s the best way to be notified when a skill levels up.

What happens when the character levels up?

The exact same thing happens for characters as skills when the character levels up, but instead of a message being sent to the skillUpdated method it sends a message to levelUpdated and passes the character class as the argument.

Again, the following is from the Demo Scene:

	void levelUpdated(SBLSCharacter ch) {
		AudioSource.PlayClipAtPoint (levelUpClip, transform.position);
	}

What about timed skills?

Timed skills work when you tell them to, and stop when you tell them to. For example, if you had a skill called Running, you’d tell the Running skill that it was in use when the player starts running, and tell it that they are no longer running when they stop.
The code for that would be:

			if (Input.GetKey(KeyCode.LeftShift)) {
				// Let's run!
				// We'll also add the current running level so we get faster as we level up
				actualSpeed = runSpeed + character.getSkill("Running").getLevel();

				character.getSkill("Running").inUse = true;
			} else {
				actualSpeed = speed;

				// We're walking, better let the skill know
				character.getSkill("Running").inUse = false;
			}

So, all you have to do is the skills inUse variable to true or false, and SBLS will do the rest for you.

Displaying level progress

Thankfully, SBLS has built-in methods that makes displaying level progress easy.
If you just want to display the number of XP, and the Next Level XP, then you can use the following methods.

Character

Get the current XP

character.getXp ();

Get the XP required to progress to the next level (nextLevelXP)

character.getNextXp ();

To get the current level use

character.getLevel ();

To get the width you’d need for a progress bar use

character.getProgress (widthOfProgressBar)

Where widthOfProgressBar is the width of the progress bar.

Skills

Get the current XP

character.getSkill("skillName").getXp ();

Get the XP required to progress to the next level (nextLevelXP)

character.getSkill("skillName").getNextXp ();

To get the current level use

character.getSkill("skillName").getLevel ();

To get the width you’d need for a progress bar use

character.getSkill("skillName").getProgress (widthOfProgressBar)

Where widthOfProgressBar is the width of the progress bar.

That should be enough to get you started. There’s a complete list of methods and their uses located on the >API Documentation page.