• How to make your own class type thing, like ply:IsReady() etc etc
    5 replies, posted
I did a bit of c#, and I was wondering is there anyway to make something like ply:IsReady() to check if they are ready to start game, etc etc? And to make classes and stuff, google is telling me that lua is like semi object oriented
Lookup the Player metatable, then attach a function to that. [lua]PLAYER = FindMetaTable( "Player" ); function PLAYER:IsReady( ) end[/lua]
Yeah for sure! It's not that hard actually. Basically this is done by adding to the player metatable. A metatable is a table of functions that can only be performed on certain objects. In this case you'll want to use the player metatable. So your code will look something like this: [CODE] local ply = FindMetaTable( "Player" ) if !ply then return end function ply:your_function_name( args_go_here ) -- the varaible 'self' is the player the function is being run on. it works like any player object. -- do whatever logic you'd like inside the function print( self:Name() ) -- some examples of using the self object print( self:SteamID() ) self:Kill() self:ChatPrint( args_go_here ) end --Example usage: for _, ply in ipairs( player.GetAll() ) do ply:your_function_name( "haha!" ) end [/CODE]
Thanks everyone :D Alot easier then I assumed [editline]19th January 2014[/editline] Okay, so this was solved, but I have a question not worth starting a thread and related to this, How do I have a variable on that one player, example, i want to see if they are ready, this is my code [CODE] ready = false function GM:ShowSpare1( ply ) ply:IsReady(not ready) print(players) for k,v in pairs (player.GetAll()) do if (players > 1 and not gameStarted and v:IsReady()) then StartGame() print("Starting..") end end end function ply:IsReady( ready ) print( self:Name() ) if(ready == true) PrintMessage( HUD_PRINTTALK, self:GetName().." is ready." ) end if(ready == false) PrintMessage( HUD_PRINTTALK, self:GetName().." isn't ready." ) end end [/CODE] I need it so, it is a toggle "true and false" and that when every player is ready (true) then it starts, What am I doing wrong?
[quote]How do I have a variable on that one player, example, i want to see if they are ready, this is my code[/quote] In the code to set if they're ready, set [lua]ply.ready = true[/lua] then for ply:IsReady all you have to do is [lua]if (self.ready) then PrintMessage( HUD_PRINTTALK, self:GetName().." is ready." ) else PrintMessage( HUD_PRINTTALK, self:GetName().." isn't ready." ) end [/lua] Don't use any arguments for IsReady.
Thanks!
Sorry, you need to Log In to post a reply to this thread.