- Home /
public static variable access returning null from others
I'm building an RPG game.
I have a public static PlayerData class that controls strength, rightHandSlot, inventory[], etc. It is not attached to anything and does not inherit from anything.
I have inventory and equipment working fine in that from my MainGUI I can call and use the rightHandSlot like this:
void Start () {
PlayerData.rightHandSlot = new InventorySlot(WeaponDatabase.PabDagger, 1);
Debug.Log (PlayerData.rightHandSlot._item._name);
}
this successfully logs the daggers name for me no problem, and I use the slots elsewhere etc.
My problem comes in with my skills:
public class GeneralSkills {
public static Skill _meleeAttack = new Skill(
"Attack", "Melee attack",
Classes.currentClass.Civilian, 1, 1, 1,
SkillCategory.General, SkillType.Melee, SkillUse.Action, SkillTarget.Mob, 1f,
new Buff[]{
new Buff(
new Damage(PlayerData.GetMeleeDamageType(), PlayerData.GetMeleeDamage(), 0)
)
}, PlayerData.baseMeleeSpeed);
}
this is just a database class that I want to continually add my skills into, this being the just basic melee attack. the normal ints and enums and all work fine, but calling stuff from the PlayerData class doesn't seem to be working from here. This script is not using monobehavior and is not attached to anything, I'm just calling it generally. The GetMeleeDamage() looks like this:
public static int GetMeleeDamage () {
if (PlayerData.rightHandSlot != null) {
Weapon curWep = (Weapon)PlayerData.rightHandSlot._item;
return Random.Range((PlayerData.baseMeleeDamage - PlayerData.baseMeleeDamageVariance) + (curWep._damage1 - curWep._damage1Variance),
(PlayerData.baseMeleeDamage + PlayerData.baseMeleeDamageVariance) + (curWep._damage1 + curWep._damage1Variance) + 1);
}
else {
return Random.Range(PlayerData.baseMeleeDamage - PlayerData.baseMeleeDamageVariance, PlayerData.baseMeleeDamage + PlayerData.baseMeleeDamageVariance + 1);
}
}
which if I look at before I I equip something, then again after, it returns the damage properly. But when I go to use my attack skill it always reads the rightHandSlot as empty, and so always only returns the default damage, never considering the weapon damage.
To summarize, the function and the variables in question are all stored on the PlayerData script. But trying to access them from the global non-object-oriented GeneralSkills class the public static PlayerData.GetMeleeDamage() function always considers public static PlayerData.rightHandSlot as null even though other scripts and PlayerData itself are well aware that it is not null and actually contains the dagger.
I dun get it XD
Thank you for any input.
by the way the skill is being defined as usable in the PlayerData script as well like this:
PlayerData.activeSkills[0] = GeneralSkills._meleeAttack;
and then gets used from the $$anonymous$$ainGUI like this:
PlayerData.UseSkill(i);
which after a bunch of checks looks like this:
int dmg = activeSkills[s]._buffs[0]._damage.GetDamage();
DamageType dmgT = activeSkills[s]._buffs[0]._damage._damageType;
player.GetComponent<PlayerScript>().target.GetComponent<$$anonymous$$obAi>().TakeDamage(dmg, dmgT);
Answer by malkere · Dec 20, 2014 at 07:28 AM
well I made this:
public class SkillTestStrike : ISkillTest {
public SkillTestStrike () {
}
public void UseSkill (GameObject mob) {
mob.GetComponent<MobAi>().TakeDamage(PlayerData.GetMeleeDamage(), PlayerData.GetMeleeDamageType());
}
}
and called it like this:
SkillTestStrike newS = new SkillTestStrike();
void Start () {
if (Input.GetKeyDown(KeyCode.Alpha3)) { newS.UseSkill(PlayerData.player.GetComponent<PlayerScript>().target); }
}
and it's properly changing depending on whether my weapon is equipped or not... I don't understand why the class in the OP won't do the same thing, but at least I found a way to do it....
this use also defeats my original purpose of a method free skill system that I could base everything off of values =/ back to being stuck where I was four days ago XD
in the end I just removed all the functions from the database and put everything as values. That way ins$$anonymous$$d of looking for the right hand weapon damage and multiplying it by 110% or whatever, I just list a multiplier of 1.1f like this:
public class SquireSkills {
public static Skill _strike = new Skill(
"Strike", "A quick swiping melee attack",
Classes.currentClass.Squire, 1, 1,
SkillCategory.Class, SkillType.$$anonymous$$elee, SkillUse.Action, SkillTarget.$$anonymous$$ob, 1f,
new Buff[]{ new Buff(new Damage(DamageType.blunt, 1, 0), 1.1f) }, 5f);
}
the buff is reporting damagetype, basedamage, damagevariance, then the multiplier to be applied to the currently held weapon 1.1f. this is then applied by the PlayerData script which doesn't have the confusion of null equipment returns.
Though I still don't fully understand why calling it from within the generic script returns a null?
Your answer

Follow this Question
Related Questions
Calling non-static from static function C# 2 Answers
Calling Update function in another function (JS) 1 Answer
code reusability question 0 Answers
rpg, skills and characters the structure, confused 0 Answers
Can't Encode MD5 2 Answers