• Adding a variable to the player class
    10 replies, posted
Hi I want to add a bool variable to the player class, so I can simply check if ply.Sleeping then.. How would I do this? I've been searching but I only find network variables, which I don't think is the best way for this.
In any of the player class hooks use self.Player.Sleeping = true, keeping the variable in player class isn't a good idea if you are me.
[QUOTE=Robotboy655;42242466]In any of the player class hooks use self.Player.Sleeping = true, keeping the variable in player class isn't a good idea if you are me.[/QUOTE] Any reason why you wouldn't? At first glance, that seems like a pretty good way to do things.
[QUOTE=Robotboy655;42242466]In any of the player class hooks use self.Player.Sleeping = true, keeping the variable in player class isn't a good idea if you are me.[/QUOTE] Could you show an example?
[QUOTE=wranders;42242579]Any reason why you wouldn't? At first glance, that seems like a pretty good way to do things.[/QUOTE] Because I don't think its table is copied per player. So, for example, if you set a variable on player1s class, all players with the same class will have the same variable in their class. I am not even sure how you would access player class table from outside of the class file. [editline]19th September 2013[/editline] [QUOTE=Busan1;42242595]Could you show an example?[/QUOTE] There's plenty in the base classes shipped with GMod.
[QUOTE=Robotboy655;42242876]Because I don't think its table is copied per player. So, for example, if you set a variable on player1s class, all players with the same class will have the same variable in their class. I am not even sure how you would access player class table from outside of the class file. [editline]19th September 2013[/editline] There's plenty in the base classes shipped with GMod.[/QUOTE] I ment to the player object just like ply:GetModel() only gets that objects model, so to change ply.Sleeping = false would only change ply. Anyway thank you for your help I'll have a look in the files.
In player class hooks, local ply = self.Player, if that makes it easier for understand.
I think the other posts here are leading in you in the wrong direction. First, modifying the player class isn't too difficult. LUA's Javascript-like objects makes adding properties and functions really easy- so if "ply" is your player object, you can really just do ply.Sleeping = true/false in the desired scope and that will work just fine. Even if a player is never set to have their .Sleep property at all, you can still do [code]if ply.Sleeping then etc... end[/code] Because nil results as false in this case. However, doing that is bad practice. Luckily there actually is a pretty simple way to modify the actual player object without tampering with the base code. For example, you can create a PLAYER.lua (you can name it whatever) file in your gamemode's basic /gamemode/ directory and use [code]include( "PLAYER.lua" )[/code] in your init.lua file to load it (or you can put it in shared if you want). Inside, the PLAYER.lua file, all you have to do is start it with [code] local PLY = FindMetaTable( "Player" ) [/code] Now PLY is an addition to the actual Player object (because it pulls it from the table). So for adding a sleeping property, all you have to do is [code]PLY.Sleeping = false[/code] and now every player that joins your server will have that property. If you need clients to tell if a player is sleeping or not, you can just have [code]PLY:SetNWBool( "Sleeping", false )[/code] but unless clients actually need to know, it's not necessary.
[QUOTE=iSchmal;42245931]I think the other posts here are leading in you in the wrong direction. ....[/QUOTE] This is mostly what I was thinking, but instead of NetVars, use metatables. I'll put something together when I get home.
But you dont have to set the default value in the metatable
[QUOTE=iSchmal;42245931] text [/QUOTE] Thank you very much for your reply. I did it another way, in the PlayerInitialSpawn hook I set NWBool to false, then I simple it from there.
Sorry, you need to Log In to post a reply to this thread.