• Store variable on the player?
    8 replies, posted
I know I can use [CODE] function ENT:SetupDataTables() self:NetworkVar( "Bool", 0, "LELELELELEL" ) end[/CODE] to store a variable on an entity. Now I know the player is an entity so I assume I can use these for the player too. When they join I need a variable the be stored on the player for example how much weed they have ;). But I also need to access this variable for other entities in the game world. How would I go about doing this, Thanks!
I'd recommend against using NW Vars for tracking things ( especially if you grab the values frequently / each frame ) because each time Get is called, it asks the server for an update. If you do use them, micro-cache the results ( don't re-get unless x time has passed ). You could simply set self.var = blah; then network the change. There are also networking systems out there which handle automatic networking of data ( and won't network unless the data actually changed ). Are you writing a game-mode or an addon?
[QUOTE=Acecool;46909952]I'd recommend against using NW Vars for tracking things ( especially if you grab the values frequently / each frame ) because each time Get is called, it asks the server for an update. If you do use them, micro-cache the results ( don't re-get unless x time has passed ). You could simply set self.var = blah; then network the change. There are also networking systems out there which handle automatic networking of data ( and won't network unless the data actually changed ). Are you writing a game-mode or an addon?[/QUOTE] Addon, Im not sure what you mean. If I just do ply.blah = 10 then in the same code do ply.blah = ply.blah + 10 I get a error saying that the value is nil.
Well, that depends how it is called... If you never set it on the realm then it'll be nil so you'd want to account for that by doing something like this: ply.blah = ( ply.blah || 0 ) + 10; which will use ply.blah if set otherwise it'll substitute 0 in place and add 10... Also, when you set the value like that, typically it is meant for that realm ( so if another realm needs it, network it ).
[QUOTE=Acecool;46910060]Well, that depends how it is called... If you never set it on the realm then it'll be nil so you'd want to account for that by doing something like this: ply.blah = ( ply.blah || 0 ) + 10; which will use ply.blah if set otherwise it'll substitute 0 in place and add 10... Also, when you set the value like that, typically it is meant for that realm ( so if another realm needs it, network it ).[/QUOTE] I have an entity called weed. I use this function but I get an error : [CODE]function ENT:Use(ply) if(IsValid(ply.weedAmount)) then ply.weedAmount = ply.weedAmount+1 else ply.weedAmount = 1 end self:Remove() end[/CODE] Error : [CODE][ERROR] lua/includes/util.lua:173: attempt to index local 'object' (a number value) 1. IsValid - lua/includes/util.lua:173 2. unknown - addons/advanced drugs/lua/entities/adrug_weed/init.lua:32[/CODE] :/ EDIT: I see my mistake, I was using IsValid -.-. SOrry for wasting your time
Acecool wastes his own time, don't worry! :) For your situation, you need to be able to tell if you're working in the shared realm, server realm, or client realm.
Don't use IsValid on ply.variable use if IsValid( ply ) then .... It can be shortened with how I wrote it... if ( !IsValid( ply ) ) then return; end ply.weedAmount = ( ply.weedAmount || 0 ) + 1; [QUOTE=Walrus Viking;46910182]Acecool wastes his own time, don't worry! :) For your situation, you need to be able to tell if you're working in the shared realm, server realm, or client realm.[/QUOTE] ENT:Use is SERVER realm, so you'd need to network the change to the client. If you always add 1 then you can simply net.Start( "UpdateWeed" ); net.Send( ply ); and on the client update the var, however it'd be better to send the current value so it can be reduced / added to etc...
[QUOTE=Walrus Viking;46910182]Acecool wastes his own time, don't worry! :) For your situation, you need to be able to tell if you're working in the shared realm, server realm, or client realm.[/QUOTE] I Was in server
The realm refers to where the code executes... So there are client files and server files and files that are used by both server and client. Just because something is "shared" doesn't mean it executes on the client when it is called on the server and vice versa. There are hooks which are called on the client and server around the same time but a lot are not. Imagine 2 pointers running through code simultaneously; one for client one for server. They may seem to sometimes "cross" but they never cross because both states are 100% isolated and separate from one another. Server code runs on server while client code runs on each client that is connected but is still considered "client" code. Some examples... GM:HUDPaint is a clientside hook, it gets executed on the client to draw the hud. If you put the code on the server, it'd just sit there and never be called. SetupMove is a shared hook, it gets executed on both client and server around the same time and if the code is the same on server as it is on the client then you can ensure the client doesn't jitter when the movement is applied. I'd recommend taking a look at a basic game-mode and add print statements in client vs server code to see how and where they execute in a proper server ( SRCDS which can be running on your machine too )... Also look at how console commands behave.. Imagine 3 console commands... cl, sv and sh. Imagine each command is in the relevant realm ( ie cl is in client only code, sv is in server only code and sh is in both client and server code or in a file executed by both ). If you call cl on the client, it will be executed on the client and work. Called on the server it won't exist ( you can force the client to call a command via server code but that is something else ). If you call sv on the client, it will execute on the server and work ( it'll be networked to the server and executed on the server ); If you call sv on the server, it'll execute on the server and will work. If you call sh on the client, it'll execute on the client. If you call sh on the server it'll execute on the server. Try to gain an understanding of how code executes and why we use client vs server code.
Sorry, you need to Log In to post a reply to this thread.