Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
1  
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. –  DormoTheNord Jan 11 '13 at 12:31

1 Answer 1

up vote 4 down vote accepted

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)
share|improve this answer
1  
Also note that you should not directly instantiate classes inherited from MonoBehaviour, they are added to game objects via gameObjectRef.AddComponent[of PlayerAttributes](). –  michael.bartnett Jan 11 '13 at 16:31
    
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." –  zyeek Jan 11 '13 at 21:20
1  
Then try doing Player = ScriptableObject.CreateInstance("PlayerAttributes") I think that will work. –  DormoTheNord Jan 11 '13 at 21:39
    
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. –  zyeek Jan 11 '13 at 21:57
    
From looking around it will probably involving using the UnityEditor module. docs.unity3d.com/Documentation/ScriptReference/Editor.html –  zyeek Jan 11 '13 at 21:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.