• Stamina System issues...?
    5 replies, posted
I was working on a stamina system and for some reason it just wasn't working 100%... meaning it wasn't working on the clientside. instead of using NWInt for stamina i decided to experiment a little and use ply:GetTable()s... So, this is what I have and I am still wondering why it isn't working. [LUA] function GM:Think() local players = player.GetAll() for _, player in ipairs( players ) do if (player:IsSprinting() == false) then player:GetTable().Stamina = player:GetTable().Stamina + 1 end end end --Serverside GM:Think() function of course... [/LUA] Then I have a shared file which I thought would work but I think this is where I am getting most of the problems [LUA] local meta = FindMetaTable("Player") function meta:IsSprinting() if ( self:KeyDown( IN_SPEED ) ) then self:GetTable().Stamina = self:GetTable().Stamina-1 return true; else return false; end end function meta:GetStamina() return self:GetTable().Stamina end [/LUA] Then the clientside [LUA] function StaminaTest() local struc = {} struc.pos = {} struc.pos[1] = 100 -- x pos struc.pos[2] = 200 -- y pos struc.color = Color(255,0,0,255) -- Red struc.text = LocalPlayer():GetStamina() or 87394 -- Text struc.font = "DefaultFixed" -- Font struc.xalign = TEXT_ALIGN_CENTER -- Horizontal Alignment struc.yalign = TEXT_ALIGN_CENTER -- Vertical Alignment draw.Text( struc ) end hook.Add("HUDPaint", "DrawBox", StaminaTest); [/LUA] *note: I am aware that it would give a nil value if I had not set it up in my GM:PlayerSpawn or something along those lines, and I have which means that shouldn't be a problem in this case. I am pretty sure you can use GetTable() in this case because I have seen Tacoscript 2 use it, and other gamemodes using it for things like this. *another additional note: don't worry about the 89798 it was just a little test, still works with or without.
If you update the player's .Stamina serverside the clientside version won't update automatically. You have to notify the client somehow. A good way would be using usermessages. By the way. self:GetTable().Stamina == self.Stamina
[QUOTE=Crazy Quebec;21069241] By the way. self:GetTable().Stamina == self.Stamina[/QUOTE] wut. And isn't GetTable() shared though, or does that just mean that the method can be used to control clientside variables or serverside, but not shared... also, I think constantly using usermessaged for that would be a little annoying don't you think? I coulda sworn I saw in many gamemodes them using this type of GetTable() system for stats etc.
It's not exactly what you think, see for yourself : [b][url=http://wiki.garrysmod.com/?title=Entity.GetTable]Entity.GetTable [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] Also the clientside and serverside tables are completely seperate. They're not shared or synched. As for notifying the clientside you could simply put that think function in shared, so that Stamina gets updated automatically on both client and server. You can then also sync it once in a while if you want/see it differs after a while. But that shouldn't be necessary. As for using usermessages they're actually the same as using networked variables, except they are only sent to one player instead of every player that's nearby.
Okay I understand now, it would probably just be easier if I used NWInts in the first place, however I just wanted to try something a little different haha. Do you have any idea how fast umsg's run, I guess it depends on the size but the int would be so small I would think it is a bit trivial to the server memory usage if I have like 30 people on the server.
They run as fast as your internet connection and in this case are much better then Networked Vars. Like I sais networked vars would be sent to every players, that's a waste of network and computing ressources. Like I said calculate the stamina on both serverside and clientside (no need for networking then). For cases handled by the server exclusively then send an usermessage with the new value. If you do it right you'll have really few messages to send.
Sorry, you need to Log In to post a reply to this thread.