• Using self.Trololo from shared.lua in cl_init.lua
    5 replies, posted
Hello. I need to use some data from shared.lua file of my swep soted is self. table, but client and server has different entity tables. I tried to create a local variable in shared and then use it in cl_init, but that didn't work. Can anyone give an me an idea how to transfer data from shared to client, please. I know thah some people asked questions like this, but I didn't find the answer. And sorry for my English if something is wrong. Thanks in advance.
The idea of shared data, is that it is the same on the client as it is on the server (that is at initialization). If you want to access a variable on the client that was declared in a shared file, simply make it global (don't make it local). An example: [lua]-- shared.lua myVariable = 1234 -- cl_init.lua include("shared.lua") print(myVariable) -- init.lua AddCSLuaFile("shared.lua") include("shared.lua") -- not even necessary in this example, but added to show the idea of shared content files [/lua]
I've already tried that yesterday but got nil on client. [PHP]--Shared Active = self.Active --cl_init surface.DrawText("Active: " .. tostring(Active))[/PHP] If i print the var in server's console, it return true, on client it is nil.
[QUOTE=Atom_rus;24687281]I've already tried that yesterday but got nil on client. [PHP]--Shared Active = self.Active --cl_init surface.DrawText("Active: " .. tostring(Active))[/PHP] If i print the var in server's console, it return true, on client it is nil.[/QUOTE] Because you're probably setting the Variable serverside. Even if the variable is shared, you can only access it on the state it is set in. If you want it accessible from client and server you will have to set it shared. I.E [lua] -- shared.lua myNum = 14 [/lua] [lua] -- Server myNum = 22 print(myNum) -- Will print, 22 -- Client print(myNum) -- Will print, 14 -- Shared myNum = 200 -- Now the CLIENT and the SERVER will both print 200 because they both have access to the new variable setting. [/lua] Hope that explains it.
Understood :D Bigga thanks to you all.
Sorry, you need to Log In to post a reply to this thread.