2
\$\begingroup\$

I have a Player Attributes class:

import UnityEngine

class Stat ():
    public current as int
    public max     as int

class PlayerAttributes ( MonoBehaviour ): 
    public Name          as string
    public level         as int
    public hp            as Stat = Stat()
    public ep            as Stat = Stat()

public strength      as int   // How accurate and how much damage with melee attacks
public dexterity     as int   // How accurate and how much damage with ranged attacks
public vitality      as int   // How many hit points he has 
public power         as int   // How much damage his energy attacks deal
public resistance    as int   // How much defense he has against energy attacks
public agility       as int
public speed         as int   // How fast character attacks

public meleeAtk      as int
public rangedAtk     as int
public defense       as int
public powerAtk      as int
public powerDef      as int
public initiative    as single

public exp         as Stat = Stat()

def SetInitialStats ( 
            name       as string, 
            strength   as int, 
            dexterity  as int, 
            vitality   as int, 
            power      as int, 
            resistance as int, 
            agility    as int, 
            speed      as int, 
            exp        as int
          ):
   self.level      = 1
   self.Name       = name
   self.strength   = strength
   self.dexterity  = dexterity
   self.vitality   = vitality
   self.power      = power
   self.resistance = resistance
   self.agility    = agility
   self.speed      = speed
   self.exp.max    = self.level * (self.level + 1) * 500
   AddExp(exp)


def AddExp ( experience as int ):
   exp.current += experience

   while exp.current >= exp.max and level < 99:
     exp.current -= exp.max
     exp.max      = level * (level + 1) * 500
     level       += 1
     IncreaseStats()

def IncreaseStats ():
   strength   += 1
   dexterity  += 1
   vitality   += 1
   power      += 1
   resistance += 1
   agility    += 1
   speed      += 1

   meleeAtk   = (level * 7) + strength
   rangedAtk  = (level * 7) + dexterity
   defense    = (level * 2) + agility
   powerAtk   = (level * 7) + power
   powerDef   = (level * 2) + resistance
   initiative = 20 + ((speed * level)/1500)

   hp.max     = ((level * 5) + vitality) * 10
   hp.current = hp.max

   ep.max       = ((level * 5) + power)
   ep.current = ep.max

Jut anything simple really. Well I read in the docs to not use constructors with MonoBehaviour, but I try instantiating my variables with the Start() function which is SetInitialStats (...)

So In another script in the Start() function I call it up like this

    Player = PlayerAttributes.SetInitialStats('Robo', 20, 21, 19, 20, 18, 22, 22, 57000)

I get the warning: ' An instance of type 'PlayerAttributes' is required to access non static member'

Not sure what else I can do to setup my beginning stats.

I got this to work I Unityscript which worked with a constructor without MonoBehaviour so I don't know where else to go from here. I would like to stay using Boo.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ May I ask why you would like to stay with Boo? I mean, as a language, I strongly prefer Boo to Unityscript, but so few people actually use Boo that getting support is almost impossible. \$\endgroup\$ Commented Jan 11, 2013 at 12:31

1 Answer 1

4
\$\begingroup\$

To access non-static members of a class, you need an instance of said class.

I'm not familiar with Boo (and it's been a long time since I've used Python), but try this:

Player = PlayerAttributes()
Player.SetInitialStats('Robo', 20, 21, 19, 20, 18, 22, 22, 57000)
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Also note that you should not directly instantiate classes inherited from MonoBehaviour, they are added to game objects via gameObjectRef.AddComponent[of PlayerAttributes](). \$\endgroup\$ Commented Jan 11, 2013 at 16:31
  • \$\begingroup\$ I inherited from ScriptableObject just to see what goes on and I get warning: "PlayerAttributes must be instantiated using the ScriptableObject.CreateInstance method instead of new PlayerAttributes." \$\endgroup\$
    – zyeek
    Commented Jan 11, 2013 at 21:20
  • 1
    \$\begingroup\$ Then try doing Player = ScriptableObject.CreateInstance("PlayerAttributes") I think that will work. \$\endgroup\$ Commented Jan 11, 2013 at 21:39
  • \$\begingroup\$ That worked perfectly, just wasn't sure how to read the doc for this one, sometimes they just look confusing I suppose. Everything checks out fine, my only concern now is just with aesthetics, but is there any way possible to have my values to appear in the inspector. I am not sure if it is part of the @script notation for Unityscript, but if anyone happens to know would be much appreciated. \$\endgroup\$
    – zyeek
    Commented Jan 11, 2013 at 21:57
  • \$\begingroup\$ From looking around it will probably involving using the UnityEditor module. docs.unity3d.com/Documentation/ScriptReference/Editor.html \$\endgroup\$
    – zyeek
    Commented Jan 11, 2013 at 21:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .