• Problems That Don't Need Their Own Thread v2.0
    5,020 replies, posted
I have a script that needs to run multiple times, and i need a way to know if it has already ran once. my attempted ended up looking like this: [LUA] b = {} b.v = {} b.v["boolean"] = {} if !b.v["boolean"]["startup"] then b.v["boolean"]["startup"] = true else print("script has already started once.") end [/lua] Does anyone know why it doesnt work?
[QUOTE=color0a;47334099]I have a script that needs to run multiple times, and i need a way to know if it has already ran once. my attempted ended up looking like this: [LUA] b = {} b.v = {} b.v["boolean"] = {} if !b.v["boolean"]["startup"] then b.v["boolean"]["startup"] = true else print("script has already started once.") end [/lua] Does anyone know why it doesnt work?[/QUOTE] Because at the start of the script you redeclare b to be an empty table and start again.
[QUOTE=color0a;47334099]I have a script that needs to run multiple times, and i need a way to know if it has already ran once. my attempted ended up looking like this: [LUA] b = {} b.v = {} b.v["boolean"] = {} if !b.v["boolean"]["startup"] then b.v["boolean"]["startup"] = true else print("script has already started once.") end [/lua] Does anyone know why it doesnt work?[/QUOTE] Because you reset B to {} every time the script is ran. Replace first line with [code] b = b or {}[/code]
[QUOTE=color0a;47334099]I have a script that needs to run multiple times, and i need a way to know if it has already ran once. my attempted ended up looking like this: [LUA] b = {} b.v = {} b.v["boolean"] = {} if !b.v["boolean"]["startup"] then b.v["boolean"]["startup"] = true else print("script has already started once.") end [/lua] Does anyone know why it doesnt work?[/QUOTE] i know [LUA] if !hasrunned then hasrunned = true else print("script has already runned") end [/LUA] works, but is there a way to keep: [LUA] b = {} b.v = {} b.v["boolean"] = {} [/LUA] [editline]16th March 2015[/editline] [QUOTE=Robotboy655;47334119]Because you reset B to {} every time the script is ran. Replace first line with [code] b = b or {}[/code][/QUOTE] that was fast. thanks <3
[QUOTE=Lixquid;47332534]Anyone know the easiest way to figure out if a vector is in your visleaf client-side? Preferably without the use of entities.. I know about [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/VisibleVec]Entity:VisibleVec[/url], but it's server-side only.[/QUOTE] Pretty sure you want to use[URL="http://wiki.garrysmod.com/page/util/GetPixelVisibleHandle"] visibility handles.[/URL] You define it once client side: [CODE]myVisibilityHandle = util.GetPixelVisibleHandle()[/CODE] [URL="http://wiki.garrysmod.com/page/util/PixelVisible"]And then you can use it on draw hooks or client ticks or whatever you want.[/URL]
ent:Visible and ent:VisibelVec perform traces, not visleaf or PVS checks. Pixel visiblity handles are similar to traces in that they try to determine if something is truely visible on the client's screen. I am not aware of a way to test for visleaf visibility or PVS visibility.
Welp then IDK what visleafs/PVS checks are.
-snip-
Been trying to figure out a way to set the players weapons back to max ammo, came up with the code below but struggling to figure out the second argument in SetAmmo? wep[i] returns all weapons the player has for example "Weapon [136][weapon_pistol]" but will return "bad argument #2 to 'SetAmmo' (number expected, got userdata)"as obviously it's not a number wep[i] is returning. [lua] local wep = ent:GetWeapons() for i=1,#wep do local maxammo = ent:GetAmmoCount( wep[i]:GetPrimaryAmmoType() ) ent:SetAmmo(maxammo, wep[i]) end[/lua]
[QUOTE=Mrkrabz;47337599]-snip-[/QUOTE] It's expecting the number of ammo to set it to. So, if you wanted all weapons to get 999 ammo, you'd set the second argument to 999.
[QUOTE=TFA;47337794]It's expecting the number of ammo to set it to. So, if you wanted all weapons to get 999 ammo, you'd set the second argument to 999.[/QUOTE] Sorry yeah, I should have explained better. I'm trying to get each weapon's max ammo and set all of the players current weapons to their max ammo, not just a set number for all weapons.
[QUOTE=Mrkrabz;47337839]Sorry yeah, I should have explained better. I'm trying to get each weapon's max ammo and set all of the players current weapons to their max ammo, not just a set number for all weapons.[/QUOTE] Firstly, you need to pass a string (ammo type) or number (ammo type ID) as the second argument to SetAmmo(); this is what's generating your error. If you want to get the max ammo a particular [b]scripted[/b] weapon, you can use [i][noparse]wep[i][/noparse].Primary.ClipSize[/i]. You can also replace Primary with Secondary for the alternate ammo type. For default ammo types, you can use the [i]sk_max_*[/i] convars. You can differentiate between the two with [i][noparse]wep[i][/noparse]:[url=http://wiki.garrysmod.com/page/Weapon/IsScripted]IsScripted[/url]()[/i].
[QUOTE=cam_;47333929]Thank you both. I have another question, though :v: I have this in cl_init.lua [code]local DontDraw = { "CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo", "CHudSuitPower" } function GM:HudShouldDraw(huditem) if(DontDraw[huditem])then return false; end end[/code] It's not working. I don't understand why. I feel like this is exactly how the wiki does it essentially.[/QUOTE] I saw this was partially answered for you, but.. [QUOTE=Drakehawke;47333933]It's HUDShouldDraw not HudShouldDraw - case matters.[/QUOTE] this also won't work because each element has a numeric key instead of the string... When you create a table like that, it becomes: [code]lua_run print( { "CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo", " CHudSuitPower" } ) > print( { "CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo", "CHudSu itPower" } )... [1] = CHudHealth [2] = CHudBattery [3] = CHudAmmo [4] = CHudSecondaryAmmo [5] = CHudSuitPower [/code] You definitely do not want to use a loop to test if a value is the name, if so prevent draw... This costs around 495 seconds over 100 million simulated frames ( or just over 15 seconds at 100 million times; simulated frames multiplies the time by x number of hud elements that it would look at meaning it wouldn't actually be that high because one time it'd be first, one time it'd be last, and all the way through )... Using the method in the link costs ~3 seconds for the same test over 100 million simulated frames, or 0.08 seconds executed 100 million times. BIG difference. Also, it takes around 14 seconds 100 million times if you use if x == blah || x == blahh || x = halb then return false; end .. The reason it takes almost as much time as the loop is because it is also executing the same number of operations as the loop minus managing k/v or i.. so you're definitely on the right track, but needed a slight re-direct. Hopefully this helps clear things up, and here... more info ( another note, the loop only went through around 6 hud elements instead of 30 or so, time would've been much longer if all were tested or in the table.. I'll update this benchmark later with that but it shows the functions used ): [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/benchmarking_tips/benchmarking_hud_stuff.lua.html[/url] Here's how to do it properly: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/proper_hud_creation.lua.html[/url] -- remove .html to view .lua -- it lists ALL of the hud elements plus one custom from example 1...
[QUOTE=thefreeman193;47338041]Firstly, you need to pass a string (ammo type) or number (ammo type ID) as the second argument to SetAmmo(); this is what's generating your error. If you want to get the max ammo a particular [b]scripted[/b] weapon, you can use [i][noparse]wep[i][/noparse].Primary.ClipSize[/i]. You can also replace Primary with Secondary for the alternate ammo type. For default ammo types, you can use the [i]sk_max_*[/i] convars. You can differentiate between the two with [i][noparse]wep[i][/noparse]:[url=http://wiki.garrysmod.com/page/Weapon/IsScripted]IsScripted[/url]()[/i].[/QUOTE] I already managed to get the max ammo for the current weapons the player has using [lua]local maxammo = ent:GetAmmoCount( wep[i]:GetPrimaryAmmoType() )[/lua] the issue is with the second argument on SetAmmo which is "ammoType". This requires for example "pistol" or the id of the gun. local wep = ent:GetWeapons() prints this out in a table, but I'm unsure how to get these out to use for SetAmmo
[QUOTE=Mrkrabz;47338091]I already managed to get the max ammo for the current weapons the player has using [lua]local maxammo = ent:GetAmmoCount( wep[i]:GetPrimaryAmmoType() )[/lua] the issue is with the second argument on SetAmmo which is "ammoType". This requires for example "pistol" or the id of the gun. local wep = ent:GetWeapons() prints this out in a table, but I'm unsure how to get these out to use for SetAmmo[/QUOTE] GetAmmoCount() returns the amount of ammo the player currently has of that type, not the maximum the weapon can carry. Once you've got the max ammo of that type, you can do: [code] ent:SetAmmo(maxammo, wep[i]:GetPrimaryAmmoType()) [/code]
[QUOTE=thefreeman193;47338172]GetAmmoCount() returns the amount of ammo the player currently has of that type, not the maximum the weapon can carry. Once you've got the max ammo of that type, you can do: [code] ent:SetAmmo(maxammo, wep[i]:GetPrimaryAmmoType()) [/code][/QUOTE] sk_max_pistol returns 150 yet the max is 256?
[QUOTE=Acecool;47338077]I saw this was partially answered for you, but.. this also won't work because each element has a numeric key instead of the string... When you create a table like that, it becomes: [code]lua_run print( { "CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo", " CHudSuitPower" } ) > print( { "CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo", "CHudSu itPower" } )... [1] = CHudHealth [2] = CHudBattery [3] = CHudAmmo [4] = CHudSecondaryAmmo [5] = CHudSuitPower [/code] You definitely do not want to use a loop to test if a value is the name, if so prevent draw... This costs around 495 seconds over 100 million simulated frames ( or just over 15 seconds at 100 million times; simulated frames multiplies the time by x number of hud elements that it would look at meaning it wouldn't actually be that high because one time it'd be first, one time it'd be last, and all the way through )... Using the method in the link costs ~3 seconds for the same test over 100 million simulated frames, or 0.08 seconds executed 100 million times. BIG difference. Also, it takes around 14 seconds 100 million times if you use if x == blah || x == blahh || x = halb then return false; end .. The reason it takes almost as much time as the loop is because it is also executing the same number of operations as the loop minus managing k/v or i.. so you're definitely on the right track, but needed a slight re-direct. Hopefully this helps clear things up, and here... more info ( another note, the loop only went through around 6 hud elements instead of 30 or so, time would've been much longer if all were tested or in the table.. I'll update this benchmark later with that but it shows the functions used ): [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/benchmarking_tips/benchmarking_hud_stuff.lua.html[/url] Here's how to do it properly: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/proper_hud_creation.lua.html[/url] -- remove .html to view .lua -- it lists ALL of the hud elements plus one custom from example 1...[/QUOTE] Thank you very much for your detailed answers; I had actually already figured out that I was checking for keys and not values, so I had switched over to table.HasValue; this is a better and cleaner looking alternative. Thank you :v:
Is there any way to get a variable that is stored on a player class? If I have something like PLAYER.Wage, how would I go about retrieving the value?
[QUOTE=G4MB!T;47340609]Is there any way to get a variable that is stored on a player class? If I have something like PLAYER.Wage, how would I go about retrieving the value?[/QUOTE] Add a function to player class that would return the value and use [url]http://wiki.garrysmod.com/page/player_manager/RunClass[/url] I know its not a good way of doing it, but player classes are, as many things in GMod, underdeveloped.
Ok thanks. I didnt realize RunClass returned anything. I think they are great so far.
If I make a variable under a player entity by doing something like Player.Money = 10, will the client be able to edit it? EDIT: I'm making the variable on a serverside lua file. EDIT 2: I also want to know if I can edit this on a shared lua file.
[QUOTE=NuclearNipple;47341609]If I make a variable under a player entity by doing something like Player.Money = 10, will the client be able to edit it? EDIT: I'm making the variable on a serverside lua file. EDIT 2: I also want to know if I can edit this on a shared lua file.[/QUOTE] If you define the variable on server, you will only be able to edit or read it on server. Client will not know about it until you send it over via net message or something. Exactly the same goes if you only set it on client.
[QUOTE=cam_;47338650]table.HasValue; this is a better and cleaner looking alternative.[/QUOTE] Please don't do this. Read the wiki page for [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/table/HasValue]table.HasValue[/url] please. The problem here is you're using this in a HUDShouldDraw hook/GM func. This is bad. Since HUDShouldDraw is called each frame (?) and you're using a loop (view the source of table.HasValue), this can cause bad slowdowns. [editline]17th March 2015[/editline] The [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/HUDShouldDraw]GM/HUDShouldDraw[/url] hook page has a better example. [editline]ee[/editline] I just read the post below. I apologize for misunderstanding. I'll leave this here because it's applicable if anyone else reads it.
he said he switched from table.HasValue to using keys
I'm having a small problem with SWEPs. I'm using a (Fairly old) version of Mad Cows Weapons which was ported to GM13 after the beta, so this problem could be the weapon base. I have set all of the weapons to have a Primary.DefaultClip of 0, and when I give myself one of the guns (Without having any ammo for it), I cannot fire, right click or anything, none of the functions are being called. If I then give myself ammo, I have to switch to another weapon, then back to the weapon I want, reload, then I can shoot, etc. However, if I give myself ammo [b]before[/b] I give myself the gun, I can reload instantly and it works how it should. Mostly I just want to know if this is a problem with the weapon base or if someone else can verify that this is a problem with Garry's Mod.
I can't decide which is the best way to emit a sound when a player hits an electric fence and a zapping visual effect (cball_explode) is played. Use the global EmitSound function, ENTITY:EmitSound on the player or fence, create a sound patch, or make my own effect that does the visual and sound together?
[QUOTE=NuclearNipple;47341609]If I make a variable under a player entity by doing something like Player.Money = 10, will the client be able to edit it? EDIT: I'm making the variable on a serverside lua file. EDIT 2: I also want to know if I can edit this on a shared lua file.[/QUOTE] It is a good idea to make the system shared but the server has the control. Make it so it doesn't matter if the client changes it ( so your setters can change and so client can see ) so that the clients value means absolutely nothing other than representing what they have if they didn't try to cheat it. When the user wants to buy something, have a shared table and only send, from the client, the item-id and the quantity to purchase; let the server figure out the price from the servers' copy of the table using the item id and quantity... Also, make sure the quantity is never < 1 and the charged value never a negative ( in trade systems you'll need to monitor negative values ). I made a simple currency system which is only missing the code to load and the code to save but the functions exist: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_currency_system/simple_currency_system.zip[/url] - zip or view: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_currency_system/_sh_currency_system.lua.html[/url] [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_currency_system/cl_currency_system.lua.html[/url] [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_currency_system/sv_currency_system.lua.html[/url] [ NOTE ] Remove .html to view / copy .Lua ... [editline]18th March 2015[/editline] [QUOTE=Robotboy655;47340645]Add a function to player class that would return the value and use [url]http://wiki.garrysmod.com/page/player_manager/RunClass[/url] I know its not a good way of doing it, but player classes are, as many things in GMod, underdeveloped.[/QUOTE] Player classes do have a lot of potential.. They're essentially hooks which only get called once if I recall correctly. I wonder if I should extend them in my dev_base so that they're easier / more intuitive to use or if I should just create a new system using the player library...
[QUOTE=Acecool;47346033]It is a good idea to make the system shared but the server has the control. Make it so it doesn't matter if the client changes it ( so your setters can change and so client can see ) so that the clients value means absolutely nothing other than representing what they have if they didn't try to cheat it. When the user wants to buy something, have a shared table and only send, from the client, the item-id and the quantity to purchase; let the server figure out the price from the servers' copy of the table using the item id and quantity... Also, make sure the quantity is never < 1 and the charged value never a negative ( in trade systems you'll need to monitor negative values ). I made a simple currency system which is only missing the code to load and the code to save but the functions exist: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_currency_system/simple_currency_system.zip[/url] - zip or view: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_currency_system/_sh_currency_system.lua.html[/url] [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_currency_system/cl_currency_system.lua.html[/url] [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_currency_system/sv_currency_system.lua.html[/url] [ NOTE ] Remove .html to view / copy .Lua ... [editline]18th March 2015[/editline] Player classes do have a lot of potential.. They're essentially hooks which only get called once if I recall correctly. I wonder if I should extend them in my dev_base so that they're easier / more intuitive to use or if I should just create a new system using the player library...[/QUOTE] If I do ExamplePlayer.Money or something of the sort on a shared file, where ExamplePlayer is a player, would it be readable by the client but only editable by the server? Because that is what I am trying to do. I want to store a variable on player and have only the server be able to edit it, but the client should be able to read it. EDIT: Never mind, I think I have got it.
[QUOTE=NuclearNipple;47347120]If I do ExamplePlayer.Money or something of the sort on a shared file, where ExamplePlayer is a player, would it be readable by the client but only editable by the server? Because that is what I am trying to do. I want to store a variable on player and have only the server be able to edit it, but the client should be able to read it.[/QUOTE] Client and Server are separate unless you specifically network the changes. If you have player.money in a shared file, both client and server will have it, but changes on each will not affect the other.
-snip-
Sorry, you need to Log In to post a reply to this thread.