[QUOTE=PortalGod;45486229]you're using net.Receive when you should be using net.ReadString, and net variables can only be read once (the print will be wrong).
also, if you plan on releasing this (or even just using this), don't use Write/ReadTable, you can easily make functions for those tables
[lua]
function net.WriteVoteInfo(t)
net.WriteUInt(#t - 1, 5) --this goes up to 32 items
for k, v in pairs(t) do
net.WriteString(v.name)
net.WriteUInt(v.votes, 6) --this goes up to 63 votes
end
end
function net.ReadVoteInfo()
local ret = {}
local len = net.ReadUInt(5) + 1
for i = 1, len do
local name = net.ReadString()
local votes = net.ReadUInt(6)
table.insert(ret, {name = name, votes = votes})
end
return ret
end[/lua][/QUOTE]
What are the benefits? Also I am kinda confused as to how you're using these functions instead of what I have done...
[QUOTE=JakeAM;45486464]What's the benefit of using those?[/QUOTE]
[url=https://github.com/garrynewman/garrysmod/blob/master/garrysmod/lua/includes/modules/net.lua#L90]net.Write/ReadTable[/url] has to write, at the very least, (#tab*48 + 8) bits for a table (I think, are doubles 32 bits?)
it also has to guess at what each thing in your table is, and since it has to be ready to handle so many different types of tables, a lot of the time it sends much more extra room than what you need (1 is one bit, but it would assume it's a double and send it as 32 bits)
In your example the net message would look like
[code]
for each entry in the main table:
8 bits for what each key in the main table's type is (number)
32 bits for the key (1)
8 bits for what each value in the main table's type is (table)
for each entry in that entry:
8 bits for what each key's type is (string)
8 bits for every character in the string ('name')
8 bits for what each value's type is (string)
8 bits for every character in that string ('sandbox')
8 bits for what each key's type is (string)
8 bits for every character in the string ('votes')
8 bits for what each value's type is (number)
32 bits for a double (0)
end
8 bits to say the table's over
end
8 bits to say the table's over
[/code]
vs
[code]5 bits for how big the table is
for each entry in the table:
8 bits for every character in the name ('sandbox')
6 bits for the number of votes (0)
end[/code]
although if votes are always 0 when you send the table, you don't even need to send that
after testing with your table, using net.WriteTable sent 984 bits, and my functions sent 194 bits
with 16 items instead of 3, WriteTable sent 5128 bits, and mine sent 869 bits
it's not too big of a difference now, but as tables get bigger, the net messages start to get huge
[editline]a[/editline]
[QUOTE=JakeAM;45486464]Also I am kinda confused as to how you're using these functions instead of what I have done...[/QUOTE]
Just replace where you use net.Write/ReadTable with net.Write/ReadVoteInfo
Do those functions need to be in both the client and server file then?
put net.WriteVoteInfo on the server, and net.ReadVoteInfo on the client
[QUOTE=Handsome Matt;45488347]Ew, let's not bloat the net table with our custom functions now. Keep everything nice and local c'mon guys.
My question: has anyone got a non retarded way of handling MySQL in Lua yet? (prepared statements and what not)[/QUOTE]
[url]https://github.com/NHargreave/GMod-Examples/tree/master/database[/url]
I made this a long time ago. I never really used it besides for a single project because I switched to Postgres.
There's lots of things I'd have done differently now, but it should be fine to use. It's commented, but I can make an example if you need.
[editline]24th July 2014[/editline]
One quick detail is that no queries ever run simultaneously, if you try to run a query while one is already running it'll be queued and run when the queries ahead of it has finished.
Any way to print the contents of a player? Like
[lua]PrintTable(LocalPlayer())[/lua] or [lua]PrintTable(LocalPlayer)[/lua]
Since, on some gamemodes, players have things like player.DarkRPVars, etc. Is there a way to print all of these?
Well i think you mean by "player.DarkRPVars" the players money or so ?
Printing all of these at once can not be done , but i think DarkRP works with [URL="https://www.youtube.com/watch?v=DXFQeOIjtog"]metatables[/URL] like they have a meta table for money/exp/etc
Just a quick question. Is it possible for the darkrp jailpos to be automatically set upon a restart? Also, is it possible to have door (eg. Pd doors) to automatically be set as "Law Only" or whatever group you want?
Need this answered ASAP!
Is it possible for only the model part of a Derma SpawnIcon to get painted over instead of the whole button?
Does anyone know if there is a hook I can use where I can sucessfully perform an HTTP GET request roughly around the moment the server starts up?
It looks like if you try a request too early you'll get an "[B]HTTP failed - ISteamHTTP isn't available![/B]" error in console.
How do i have it so they need to have the right price to buy an item with a custom money system
[CODE]local meta = FindMetaTable("Player") --Get the meta table of player
function meta:AddMoney(amount)
local current_cash = self:GetMoney()
self:SetMoney( current_cash + amount )
end
function meta:SetMoney(amount)
self:SetNetworkedInt( "Money", amount )
self:SaveMoney()
end
function meta:SaveMoney()
local cash = self:GetMoney()
self:SetPData("money", cash)
end
function meta:SaveMoneyTXT()
file.Write(gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt", self:GetMoneyString())
end
function meta:TakeMoney(amount)
--Add money function here
self:AddMoney(-amount)
end
function meta:GetMoney()
return self:GetNetworkedInt( "Money" )
end[/CODE]
[QUOTE=MasterKenneth;45494273]How do i have it so they need to have the right price to buy an item with a custom money system
[CODE]local meta = FindMetaTable("Player") --Get the meta table of player
function meta:AddMoney(amount)
local current_cash = self:GetMoney()
self:SetMoney( current_cash + amount )
end
function meta:SetMoney(amount)
self:SetNetworkedInt( "Money", amount )
self:SaveMoney()
end
function meta:SaveMoney()
local cash = self:GetMoney()
self:SetPData("money", cash)
end
function meta:SaveMoneyTXT()
file.Write(gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt", self:GetMoneyString())
end
function meta:TakeMoney(amount)
--Add money function here
self:AddMoney(-amount)
end
function meta:GetMoney()
return self:GetNetworkedInt( "Money" )
end[/CODE][/QUOTE]
I think you should learn more Lua before continuing, you seem to be asking allot of help for simple things.
[QUOTE=MasterKenneth;45494273]How do i have it so they need to have the right price to buy an item with a custom money system
[CODE]local meta = FindMetaTable("Player") --Get the meta table of player
function meta:AddMoney(amount)
local current_cash = self:GetMoney()
self:SetMoney( current_cash + amount )
end
function meta:SetMoney(amount)
self:SetNetworkedInt( "Money", amount )
self:SaveMoney()
end
function meta:SaveMoney()
local cash = self:GetMoney()
self:SetPData("money", cash)
end
function meta:SaveMoneyTXT()
file.Write(gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt", self:GetMoneyString())
end
function meta:TakeMoney(amount)
--Add money function here
self:AddMoney(-amount)
end
function meta:GetMoney()
return self:GetNetworkedInt( "Money" )
end[/CODE][/QUOTE]
[lua]
function meta:HasMoney( amount )
return self:GetNetworkedInt( "Money", 0 ) >= amount
end
[/lua]
[QUOTE=sm69baller;45493415]Well i think you mean by "player.DarkRPVars" the players money or so ?
Printing all of these at once can not be done , but i think DarkRP works with [URL="https://www.youtube.com/watch?v=DXFQeOIjtog"]metatables[/URL] like they have a meta table for money/exp/etc[/QUOTE]
You're wrong, all values stored "in a player" can be retrieved. The thing about players is that even though they act like tables, they aren't. This is why iterators don't work on them.
They're userdata, which can be assigned a metatable, which means you can make [I]__index[/I] and [I]__newindex[/I] methods for said userdata (these also allow for inheritance). These metamethods direct you to a proxy table when you're storing or reading from a player object. A consequence of this is that Lua needs to be aware of this proxy table, or in other words it has to exist somewhere within Lua.
The proper way of doing this in C/C++ is storing a [url=http://pgl.yoyo.org/luai/i/luaL_ref]reference[/url] to it, which is typically placed in the registry which you can retrieve with [I]debug.getregistry()[/I] and which is exactly what Garry has done. You can find said proxy table by placing a dummy value in it (not very elegant, I know) and scanning the registry, like so:
[lua]
local ply = LocalPlayer()
local tbl
ply.Dummy = 1337 -- this should actually be an empty table (read below)
for i, v in ipairs(debug.getregistry()) do
if istable(v) and v.Dummy == ply.Dummy then
tbl = v
break
end
end
PrintTable(tbl)
[/lua]
The value of the dummy in this case is just for clarity, in fact you'd want to make an empty table (or a [I]newproxy()[/I] which makes an empty userdata) because a table can only ever be equal to itself with no metamethods applied.
How would I place/draw a .png image on the ground below a player? How would I remove it?
[QUOTE=Ekuland;45495126]How would I place/draw a .png image on the ground below a player? How would I remove it?[/QUOTE]
By calling surface.DrawTexturedRect() in a 3D2D cam in a PrePlayerDraw hook ( or PostPlayerDraw hook ). Remove it by not calling the function.
Trying to parent a entity to player, all works except for rotation, I know it's parented because when I move the player the entity moves with the player, except when I rotate it, I don't want to use a think hook to update rotation, what do I do?
[code]
-- Entity created before hand, but not spawned yet.
ent:SetParent( ply )
ent:SetPos( ply:GetPos() )
ent:SetAngles( ply:GetAngles() )
ent:SetCollisionGroup( COLLISION_GROUP_NONE )
ent:Spawn()
[/code]
[QUOTE=Robotboy655;45495168]By calling surface.DrawTexturedRect() in a 3D2D cam in a PrePlayerDraw hook ( or PostPlayerDraw hook ). Remove it by not calling the function.[/QUOTE]
I [I]think[/I] I understand, but I'm trying to do this via a SWEP, how would I denote the player I want it to spawn under? (It can also spawn under the owner [chosen by PrimaryAttack vs SecondaryAttack])
-EDIT-
Clarifying: As in Left click shoots a player.. I need to place the X under them.
Right click needs to place the X under yourself. How do I specify the player I want to draw under for surface.DrawTexturedRect?
[QUOTE=Ekuland;45495276]I [I]think[/I] I understand, but I'm trying to do this via a SWEP, how would I denote the player I want it to spawn under? (It can also spawn under the owner [chosen by PrimaryAttack vs SecondaryAttack])
-EDIT-
Clarifying: As in Left click shoots a player.. I need to place the X under them.
Right click needs to place the X under yourself. How do I specify the player I want to draw under for surface.DrawTexturedRect?[/QUOTE]
Assign a networked variable to a player, set it from server, if it is true on client in the hook, draw the X.
If you need it showing only for SWEP owner, you could use SWEP:PreDrawViewModel
It actually may be better in PostDrawPlayer. I've found that using PreDrawPlayer for things that [I]move[/I] with the player causes issues such as stuttering because it is called before the actions that occur when rendering the player happens. By using post, it'll make it appear smooth. On top of that, Post won't be called if the player is invisible..
-snip- screw it, my script works without the flair... I'll call it good without it :3
How can I remove util.Effect created entities? I'm trying to make a nice looking 3d smoke, Currently I'm using something like this (Serverside):
[lua]
function ENT:Initialize()
self.Data = EffectData()
self.Data:SetOrigin(self:GetPos())
util.Effect("SMOKE", self.Data)
end
function ENT:Think()
-- Moving the effect, seems to work fine :).
end
function ENT:OnRemove()
self.Data:SetRadius(0)
end
[/lua]
I've tried SetRadius, SetMagnitude, Set the data to nil and everything. also getting entity from CEffectData returns my scripted entity which is creating the effect. Anyhow I can remove the smoke or any good replacement for a 3d and nice looking smoke effect? Thanks.
[QUOTE=!!!WARLOCK!!!;45496114]How can I remove util.Effect created entities? I'm trying to make a nice looking 3d smoke, Currently I'm using something like this (Serverside):
[lua]
function ENT:Initialize()
self.Data = EffectData()
self.Data:SetOrigin(self:GetPos())
util.Effect("SMOKE", self.Data)
end
function ENT:Think()
-- Moving the effect, seems to work fine :).
end
function ENT:OnRemove()
self.Data:SetRadius(0)
end
[/lua]
I've tried SetRadius, SetMagnitude, Set the data to nil and everything. also getting entity from CEffectData returns my scripted entity which is creating the effect. Anyhow I can remove the smoke or any good replacement for a 3d and nice looking smoke effect? Thanks.[/QUOTE]
I haven't worked with effects before, but maybe self.Data:Remove() ?
Is there any GeoIP related functions in standard Garry's Mod? Or do I have to use a module?
[QUOTE=smithy285;45497562]Is there any GeoIP related functions in standard Garry's Mod? Or do I have to use a module?[/QUOTE]
You could use http.fetch, pretty sure someone did that, let me find you a link.
You could also use system.GetCountry clientside
EDIT: There you go [url]http://facepunch.com/showthread.php?t=1408656&p=45360800&viewfull=1#post45360800[/url]
Next post shows system.GetCountry
Sorry for posting But Is it possible to make a swep that makes a command run in the console
[QUOTE=MasterKenneth;45500740]Sorry for posting But Is it possible to make a swep that makes a command run in the console[/QUOTE]
player.ConCommand and SWEP hooks
[lua]
function SWEP:PrimaryAttack()
self.Owner:ConCommand( "say lol" )
end
[/lua]
that didnt work
Wrap it in SERVER if, or use IsFirstTimePredicted to prevent spamming. There should be no issue with that unless you're overwriting PrimaryAttack somewhere else in the swep.
How do i do that
Sorry, you need to Log In to post a reply to this thread.