Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by malkere · Dec 20, 2014 at 05:48 AM · staticrpgfunctionsskills

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image malkere · Dec 20, 2014 at 05:54 AM 0
Share

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);

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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....

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image malkere · Dec 20, 2014 at 07:40 AM 0
Share

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

avatar image malkere · Dec 20, 2014 at 08:25 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges