• Allowing an entity access to a variable in a gamemode's shared.lua
    10 replies, posted
I defined "vanmoney" as a (global) variable in shared.lua, so how do I allow an entity (the ones in the gamemode folder meant for the gamemode, not random entities) access to it? It seems to be able to access meta functions, but not anything from shared.lua... that is very bad.
Just reference vanmoney in your entity script.
I did. [lua]function ENT:Use( activator, caller ) if activator:GetMoney() < 101 then activator:SetMoney(0) vanmoney = 50 else return true end end[/lua] It takes my money, but it does not update on my hud and using a console command to check the value of vanmoney, it is still at 0. No errors either. GetMoney() and SetMoney() are in sh_player file for my gamemode.
ENT:Use() is a serverside and hook, not a shared one. You have to sync it using net messages
[QUOTE=syl0r;46065686]ENT:Use() is a serverside and hook, not a shared one. You have to sync it using net messages[/QUOTE] The only way I know of how to send net messages is from client to server, I have no idea how to do it in reverse, or more specifically from server to shared.
[QUOTE=RonanZer0;46065752]The only way I know of how to send net messages is from client to server, I have no idea how to do it in reverse, or more specifically from server to shared.[/QUOTE] Take a look at this [url]http://wiki.garrysmod.com/page/Net_Library_Usage[/url]
[QUOTE=smithy285;46065826]Take a look at this [url]http://wiki.garrysmod.com/page/Net_Library_Usage[/url][/QUOTE] That's what I was doing. So, I got net library code now. But it's not working. It looks like this: Entity, use function: [lua]function ENT:Use( activator, caller ) if activator:GetMoney() < 101 then //self.Entity:Remove() net.Start( "vanmoneysync" ) net.WriteInt( activator:GetMoney() ) net.Broadcast() activator:SetMoney(0) else return true end end[/lua] Shared: [lua]net.Receive( "vanmoneysync", function( int ) print(net.ReadInt()) vanmoney = net.ReadInt() end )[/lua] However, I get errors in the server console, and now my entity won't even take my money like it did before. The error says [code]bad argument #2 to 'WriteInt' (number expected, got no value)[/code], probably because I did something hideously wrong, like with the WriteInt, WriteInt confuses me, I just want to send a completely whole number. (I just want to send the number 50...)
WriteInt takes second parameter - number of bits to send. Just put 32 there if not sure, should be enough for almost anything (It allows numbers in range -2147483648 .. 2147483647 to be sent). And this is wrong: [lua] print(net.ReadInt()) vanmoney = net.ReadInt() [/lua] You can't ReadInt() twice because you only wrote value once. Correct way: [lua] vanmoney = net.ReadInt() print(vanmoney) [/lua]
You forgot to pass the argument 8 after activator:GetMoney() [editline]24th September 2014[/editline] RIP Ninja'd
[QUOTE=BFG9000;46066015]You forgot to pass the argument 8 after activator:GetMoney() [editline]24th September 2014[/editline] RIP Ninja'd[/QUOTE] tiny economy if an 8 bit integer is enough
[QUOTE=Hoffa1337;46071432]tiny economy if an 8 bit integer is enough[/QUOTE] Wops Yeah it should be more :P
Sorry, you need to Log In to post a reply to this thread.