• Net library is not working for me
    9 replies, posted
I am using the net library for the first time and it really does not work for me In lua-loading file (pas_init.lua) [code] util.AddNetworkString( "ToolsTest" ) --Include Server Files include("pas/server/settings.lua") include("pas/pas.lua") include("pas/propprotection.lua") --Add Client Side Files AddCSLuaFile("autorun/client/pas_init.lua") AddCSLuaFile("pas/client/hud.lua") AddCSLuaFile("pas/client/panel.lua") [/code] In the Server File (settings.lua) [code] function FirstSpawn( ply ) net.Start( "ToolsTest" ) net.WriteString("teststring!") net.Broadcast() end hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn ) [/code] In the client File (panel.lua) Called when the player presses a button. [code] net.Receive( "ToolsTest", function(len) MsgN("RECEIVE!") end) [/code] But it does not show "RECEIVE!" And i don't know why O.o
Restart the server or do changelevel. When adding a network string via util.AddNetworkString, it sometimes glitches and requires a total reload.
thank you for your answer. But I just tried this several times and it still shows nothing in my client console :/
BTW. I see you sending the cl files via AddCSLuaFiles, but I don't see you including them in any cl file :P.
Oh I never heard of that.. I am quite new to LUA-Coding ^^ so I just have to insert IncludeClientFile("name") in each client lua file? Edit: Oh, yes.. sorry.. i forgot! I also have a cl_pas_init with this code: [code] --Include Client Files include("pas/client/panel.lua") include("pas/client/hud.lua") [/code]
[QUOTE=Paku596;42262044]Oh I never heard of that.. I am quite new to LUA-Coding ^^ so I just have to insert IncludeClientFile("name") in each client lua file?[/QUOTE] Nope, just in the main cl file (like cl_init), do include('file/here.lua') It's just like in serverside files
So.. this also was not the problem. I really can't find out what I did wrong -.-
There's more wrong than that. Let's start from the beginning, the file structure. What you want to do is have your "pas_init.lua" file in the "lua/autorun/" folder, but just for simplicity's sake we'll have that file handle both the server and client initialization. Something like this would suffice: [lua] AddCSLuaFile() -- Make the client download this file. AddCSLuaFile( "pas/cl_test.lua" ) -- Make the client download your "pas/cl_test.lua" file. if SERVER then include( "pas/test.lua" ) -- Run the serverside "pas/test.lua" file. else include( "pas/cl_test.lua" ) -- Run the clientside "pas/cl_test.lua" file. end [/lua] The "AddCSLuaFile()" function is a valid function on both the client and the serverside environment, but it won't do anything on the client. Next, make 2 files; "pas/test.lua" and "pas/cl_test.lua". You can change that to whatever you want accordingly. [I]test.lua[/I] [lua] util.AddNetworkString( "test" ) -- Cache the net message. local function Test( ply ) net.Start( "test" ) net.WriteString( "Hello, your name is: " .. ply:Nick() ) net.Send( ply ) end hook.Add( "PlayerInitialSpawn", "Test", Test ) [/lua] This will send a net message with the string "Hello, your name is: <name>" to any player who has just joined. [I]cl_test.lua[/I] [lua] local function Test() local str = net.ReadString() -- Here, we read the string that was sent from the server chat.AddText( str ) -- And here we print it to the chat end net.Receive( "test", Test ) [/lua] And here we specify that whenever the client receives the net message 'test', it'll read 1 string from that net message and then print it into the chat. What you were doing is creating the above function whenever the player clicked on a button which is wrong, it's like punching your mailbox expecting letters to appear. The letter appears after the post office has sent the letter to you. Also, note that I localized both functions 'Test', there's no reason to pollute the global environment with generic functions like that at all. Always localize variables/functions/whatever unless you absolutely need them to be global.
OMG thank you for your amazing answer. I did everything like you wrote and it works now! I just have to change everything how I need it and everything works perfect! Big thanks! :D
And just a tip. If you are making a mod, it's always good to put your functions into a table so they don't collide with other mods that might have the same name for a function. For example, if you got a function called LoadDatabase and some other mod has such a function then yours or his will get repleaced (only one function of such name can exist). So it's easier to create a table and keep functions in here, I personally prefer making 3 tables for each envirnoment. [code]sv_MM = {} cl_MM = {} sh_MM = {}[/code]-- MM stands for My Mod. Just create them at the top of main files (for example, do "local sv_MM = {}" on top of init.lua). This way, you can name your function whatever you want and it will never collide. The function creating will just look a bit different: [code]sv_MM.Initialization()[/code] PS. You can also do local function to prevent the collision, but it's just better to make a local table
Sorry, you need to Log In to post a reply to this thread.