Skill system (help)

Hi guys me and my friend are trying to create a skill system for our game but we are running into problems and error trying to just level up just one skill to test.

The error string is not found we know what is wrong with but we cant seem to fix it

please help :face_with_spiral_eyes:

// This for the skill system of currents

// These are the classes for each skill
class woodCutting {
	static var level : int = 1;
	static var curXp : float = 0;
	static var maxXp : float = 10;
	static var xpUp : float = 1;
}

class gathering {
	var level = int;
	var curXp = float;
	var maxXp = float;
	var xpUp = float;
}

class fishing {
	var level = int;
	var curXp = float;
	var maxXp = float;
	var xpUp = float;
}

class hunting {
	var level = int;
	var curXp = float;
	var maxXp = float;
	var xpUp = float;
}

class mining {
	var level = int;
	var curXp = float;
	var maxXp = float;
	var xpUp = float;
}

class cooking {
	var level = int;
	var curXp = float;
	var maxXp = float;
	var xpUp = float;
}

class combatMelee {
	var level = int;
	var curXp = float;
	var maxXp = float;
	var xpUp = float;
}

class combatRanged {
	var level = int;
	var curXp = float;
	var maxXp = float;
	var xpUp = float;
}

class endurance {
	var level = int;
	var curXp = float;
	var maxXp = float;
	var xpUp = float;
}

var myClass : string;
var woodCount : int = 0;
var gatherCount : int = 0;

function Update(){
	if (woodCount + 1){
		myClass = woodCutting;
		myClass.curXp = myClass.curXP + myClass.xpUp;
		if(myClass.curXP == myClass.maxXP){
			levelUp();
		}

	}
	
	if (gatherCount + 1){
		myClass = gathering;
		myClass.curXp = myClass.curXP + myClass.xpUp;
		if(myClass.curXP == myClass.maxXP){
			levelUp();
		}

	}
	
	if(Input.GetKeyDown("l")){
	woodCount ++;
	}
}

function levelUp(){
	myClass.level += 1;
	myClass.maxXp += 1000;
	myClass.curXP = 0;
	Debug.Log("level up");
}

I don’t know exactly what you are trying to accomplish, but “if (woodCount + 1)” isn’t a thing. Like, this doesn’t do anything.

It should be:

if (woodCutting >= 1)

// or

if (woodCutting == 1)

Do a google search for UnityScript Comparison Operators to learn more.

What we are trying to do is when we cut a tree down the experience is gained and if we acquire enough xp we level up but the variable (my class) seem to be the problem but we dont know how to fix it.

Oh, sorry. I zipped through here pretty quick, guess I missed the most obvious problem here; you created a different class for each skill, and they all have the same variables. This is very wasteful. Try this:

public class Skill
{
	var level : int = 0;
	var currentXP : float = 0;
	var maxXp : float = 0;
	var xpUp : float = 0;
	// This will replace woodCount and gatherCount.
	var useCount : int = 0;
	var used : boolean = false;
	
	// Use the skill.
	function Use()
	{
		used = true;
		useCount += 1;
	}
	
	// This will do the work of adding the xp, checking for a level up, and returns true if a level up occured (false if not).
	function RaiseXP() : boolean
	{
		// Add the xp.
		currentXP += xpUp;
		// Check if the xp is greater than or equal to the max xp.
		if (currentXP >= maxXP)
		{
			// Increase the level.
			level++;
			// Return true.
			return true;
		}
		// Return false.
		return false;
	}
}

var woodCutting : Skill;
var gathering : Skill;

function Update()
{
	if (woodCutting.used)
	{
		// Prepare the woodCutting skill to be used again.
		woodCutting.used = false;
		// Raise the woodCutting experience.
		woodCutting.RaiseXP();
	}
	
	if (gathering.used)
	{
		// Prepare the gathering skill to be used again.
		gathering.used = false;
		// Raise the gathering experience.
		gathering.RaiseXP();
	}
	
	// When you want to use a skill outside of this class, use
	// myClassFileName.skillName.Use();
	//
	// For example: myCharacter.woodCutting.Use();
}

I’m not great with UnityScript, so I’m not certain if everything is 100% correct, but the general idea is solid. Also, you can compress this even more by removing the skill checks from the Update function and calling RaiseXP() in Use(), but only do this if there is no other functionality to add based on which skill is being used.

I hope this was helpful. :smile:

But wouldn’t the use function be called on both gathering and wood cutting?
I have another script that act as the wood cutting that cuts a tree down, for test purposes would the key down work?

I am really glad that your helped me with this thank you. :stuck_out_tongue:

You can put all your skills in one class (for now I’ll just call it Character).

Attach a Character component to the Player. Then set the values in the Inspector as you need them.

Now, in the script that deals with the player’s input (or the script that handles a particular skill), get the Character component and call the appropriate skill’s Use method.

// WoodCutting.js
var myCharacter : Character;
var cutWoodAllowed : boolean = false;

function Start()
{
   myCharacter = gameObject.GetComponent("Character");
}

function Update()
{
   // Before checking Input, determine if the Player is in front of a tree. If they are, set cutWoodAllow to true.
   // If they are not, set cutWoodAllow to false.

   if (cutWoodAllowed  Input.GetKeyDown(KeyCode.E))
   {
      myCharacter.woodCutting.Use();
   }
}

Do the same for each skill, or combine them all into one class.

there is a weird error on this

if (woodCutting.used)

What is the error? Without that info, I can only suggest you try

if (woodCutting.used == true)

or you can see if setting the variables to public in the Skill class.

You could create an array class to manage all your skill settings from the inspector. It’s nice and neat and frees up space to manage all your functions from one script.

// Untested example code

var skills : sks[];

class sks {
  var name : String;
  var max : float;
  // etc
}

// Then use an index in functions to reduce clutter

function SkillCheck() {
    //------------//
    if(skills[0].name=="WoodCutting") {
      if(skills[0].max>=100) {
       // Build me new desk - lol
      }
    }
    //------------//
}