Alright. I have a small favor to ask of the internets.
Friends and I occasionally play around and make simple capture-the-flag type games with custom-built forts and whatnot. Various LUA sweps have gone a long way to making combat actually entertaining, but there's still something missing.
We have a 10 minute build period and then we play - is it possible to have players spawn with their default weapon array for building, but once a serverside variable is set, they spawn with a completely different array?
I.e.
If variable is 0, Spawn players with weapon A, weapon B, weapon C...
If variable is 1, Spawn players with weapon B, weapon D, weapon Q...
If variable is 2, Spawn players with weapon R, weapon E, weapon X...
And so forth.
For my purposes, I actually only need two modes (0 and 1), but it should be capable of handling any (reasonable) number of weapons.
My apologies if this is misplaced, impossible, already-been-done, etc.
Thank you for putting up with the above post. >_>
[lua]local wepTab = { };
wepTab[0] = {
"weapon_smg1",
"weapon_shotgun"
}
wepTab[1] = {
"weapon_pistol",
"weapon_crowbar"
}
local function doVarLoadout( ply )
for _, v in pairs( wepTab[var] ) do
ply:GiveWeapon( v );
end
end
hook.Add( "PlayerLoadout", "doVarLoadout", doVarLoadout );[/lua]
"var" is your variable.
wepTab[0] is the stuff for var == 0
wepTab[1] is the stuff for var == 1
[editline]6th May 2011[/editline]
Put it in a serverside lua autorun file, ie
garrysmod/lua/autorun/server/varloadout.lua
Placed the file, upon loading up a listen server got the following output:
[CODE]
Lua initialized (Lua 5.1)
LuaGetfile: Not Loading autorun/
Couldn't include file 'autorun/' (File not found) (<nowhere>)
[...]
Hook 'doVarLoadout' Failed: [@lua/autorun/server/Varloadout.lua:13] bad argument #1 to 'pairs' (table expected, got nil)
[/CODE]
(I'm using a perfectly vanilla game to test this. Hence, there is nothing in lua/autorun and nothing in lua/autorun/server but varloadout.lua)
Did I do something dumb? Perhaps I don't understand LUA quite as well as I think.
(Further, does one change var from the console in-game?)
Change var to 1 or 0
[QUOTE=sintwins;29721845]Change var to 1 or 0[/QUOTE]
Tried using
[LUA]local enabled = CreateConVar("WeaponTab", "0")[/LUA]
And replacing var with WeaponTab, no luck.
Changed the for pairs loop to
[LUA]for _, v in pairs( wepTab[0] ) do[/LUA]
to test it, got the following error:
[CODE]Hook 'doVarLoadout' Failed: [lua/autorun/server/varloadout.lua:16] attempt to call method 'GiveWeapon' (a nil value)[/CODE]
I am confused.
Change GiveWeapon to Give.
Alrighty.
Nothing happened, I believe it's not stripping weapons beforehand, and just giving duplicate copies (currently using the vanilla weapons). Further, not terribly aware of how to change var (I've since replaced it with a 0 to test it, trying to get a ConVar to work.)
Currently I have:
[LUA]local WeaponTab = CreateConVar("WeaponTab", "0")
local wepTab = { };
wepTab[0] = {
"weapon_smg1",
"weapon_shotgun",
}
wepTab[1] = {
"weapon_crowbar"
}
local function doVarLoadout( ply )
for _, v in pairs( wepTab[WeaponTab] ) do
ply:Give( v );
end
end
hook.Add( "PlayerLoadout", "doVarLoadout", doVarLoadout );[/LUA]
And I get the 'bad argument' error.
Sorry I'm so bad at this :S
[b][url=http://wiki.garrysmod.com/?title=G.CreateConVar]G.CreateConVar [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] doesn't return the value it returns, a [url=http://wiki.garrysmod.com/?title=Convar]ConVar[/url] so you would need to use
[lua]
local WeaponTab = CreateConVar("WeaponTab", "0"):GetInt()
[/lua]
or you could use [b][url=http://wiki.garrysmod.com/?title=G.GetConVarNumber]G.GetConVarNumber [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
which would be
[lua]
CreateConVar("WeaponTab", "0")
local WeaponTab = GetConVarNumber("WeaponTab")
[/lua]
on a side note, use print and PrintTable to debug your values when you get bad argument errors, to check what is actually being passed to the function.
so above line 14 if you put
[lua]
print(webTab[WeaponTab])
[/lua]
it will print
[code]
nil
[/code]
in the console and you've found your bad value, then you could put a PrintTable(wepTab) to see what's in the table, if the table is ok then it's the value you are using for your index. So then print(WeaponTab) would tell you that you aren't getting a number so then go on the wiki and check what CreateConVar returns and make sure it's what you think it is.
While developing my console is always a mess, I usually print every variable until I know it's giving good values then I comment out the print in the case I need to check it later. Just don't forget to remove them before release or you'll get people complaining about console spam.
Hope this helps.
It works! Testing with a stunstick, it does in fact give it to you in the correct fashion when the convar is set. (The Print and PrintTable commands helped a lot.)
[i]However,[/i] I can't seem to get it to strip the player's weapons properly.
[LUA]CreateConVar("PlayerLoadout", "0");
local WeaponTab = GetConVarNumber("PlayerLoadout");
local wepTab = { };
wepTab[0] = {
"weapon_physcannon",
"weapon_physgun",
"gmod_camera",
"gmod_tool"
}
wepTab[1] = {
"weapon_crowbar"
}
local function doVarLoadout( ply )
local WeaponTab = GetConVarNumber("PlayerLoadout");
ply:StripWeapons();
for _, v in pairs( wepTab[WeaponTab] ) do
ply:Give( v );
end
end
hook.Add( "PlayerLoadout", "doVarLoadout", doVarLoadout );[/LUA]
Perhaps my ply:StripWeapons() is formatted wrong or something. Or maybe it gives players their default weapon palette after the hooked function runs?
EDIT: Immediately after posting this I noticed a page on the wiki that says 'return true' halts the standard loadout from running. Going to remove the weapon strip and use that at the end of the function and see what happens.
If this is in sandbox try setting sbox_weapons 0 to stop sandbox from giving weapons if returning true doesn't work for some reason, I've had loadout issues before. Also you may want to name your ConVar's a little different so other peoples stuff is less likely to overwrite your values. I usually add a few letters to the beginning of the ConVar name, like in my prop damage addon I put pdmg_ before all of them.
and always remember the cake is a lie.
[QUOTE=Fantym420;29766218]If this is in sandbox try setting sbox_weapons 0 to stop sandbox from giving weapons if returning true doesn't work for some reason, I've had loadout issues before. Also you may want to name your ConVar's a little different so other peoples stuff is less likely to overwrite your values. I usually add a few letters to the beginning of the ConVar name, like in my prop damage addon I put pdmg_ before all of them.
and always remember the cake is a lie.[/QUOTE]
Works perfectly with 'return true'.
Thank you so much for your help, good sir. Great success.
I will always doubt the truthfulness of baked goods. Thank you :D
Sorry, you need to Log In to post a reply to this thread.