ID:1782119
 
(See the best response by Akto.)
I wanted to give generic mobs variables like Health/MP, and players variables under mob/player/var

world/mob = /mob/player

mob/var
level = 0
health = 100
mp = 50

mob/player/var
experience = 0

However, when I try and access the mob/player/var variables in mob/Stat()
mob/Stat ( )
if ( TEST_MODE && src.key = "Dayoak" ) //The statpanel is only there for me for quick access to variables for testing, TEST_MODE is a #define I have)
if ( statpanel ( "Stats" ) )
stat ( "Level" , level )
stat ( "Experience" , "[ experience ]/[ level * 10 ]" )
stat ( "Health" , health )

if ( statpanel ( "Server" ) )
stat ( "CPU" , "[ world.cpu ]%" )
It gives me an undefined variable error,
error: experience : undefined var

I'm guessing client.statobj isn't set to /mob/player, however my Login() is set up through mob/player/Login(), shouldn't it set it to world/mob, or am I just going about this all wrong?
Best response
I think I can answer this... basically, in my unprofessional description because I am in no means a high level programmer. You have experience as a player variable. Mob/Stat() only knows level, health, and mp because its the parent of mob/player. I would move experience to your mob/var. Or you can do mob/player/Stat() I believe.
mob/player/Stat ( )
if ( TEST_MODE && src.key = "Dayoak" ) //The statpanel is only there for me for quick access to variables for testing, TEST_MODE is a #define I have)
if ( statpanel ( "Stats" ) )
stat ( "Level" , level )
stat ( "Experience" , "[ experience ]/[ level * 10 ]" )
stat ( "Health" , health )

if ( statpanel ( "Server" ) )
stat ( "CPU" , "[ world.cpu ]%" )


Do mob/player/Stat()
Woot called it! +1 Akto!
In response to Zasif
Zasif wrote:
Do mob/player/Stat()

Wow, I didn't think of that...
Thanks.
To expand on the correct answer above a bit, if you're only using that one type you're golden, but if in the future you come across an instance where you need to leave Stat() defined under the parent /mob (if you say, have two different parent types for different kinds of players), you simply need to use type-casting.

mob/Stat()
..()
if(istype(src,/mob/player))
var/mob/player/my_player = src
statpanel("Player Stats")
stat("Experience",my_player.experience)
if(istype(src,/mob/otherthing))
var/mob/otherthing/my_other = src
// ... and so on.


But yes, the answer given above is the correct course to take in this specific situation, but it's always good to know how to handle the situations not specific to this one case as well -- for the future.

Also, you should be calling "..()" within Stat() to allow other Stat() calls in the code to execute, even if you don't have any now, it's always possible you'll do so later elsewhere in the code. This'll prevent multiple calls from overriding each other.

Enjoy :)
Thanks for the thorough explanation, Nadrew.