• Print

Author Topic: Ulx completes  (Read 23278 times)

0 Members and 1 Guest are viewing this topic.

Offline skillz

  • Newbie
  • *
  • Posts: 29
  • Karma: 1
Ulx completes
« on: May 31, 2013, 08:25:39 am »
Hey for one of the commands i'm making for ttt, i want to have all the equipment items to be loaded as completes for the command. when i tried to reference the table of equipment it said that it didn't exist. i've successfully referenced the table in another one of my commands so this leads me to believe the completes are loaded first? Any ideas as to what could be my problem. Im not going to bother posting code because i just want to find out if this is ulx or my coding, if its my coding i can fix it in time.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Ulx completes
« Reply #1 on: May 31, 2013, 09:41:59 am »
The completes are loaded when the map starts, so it has to be available at that time. Just make sure that the table you're using is created and populated when the file executes ABOVE where you're passing it to ULX.
Experiencing God's grace one day at a time.

Offline skillz

  • Newbie
  • *
  • Posts: 29
  • Karma: 1
Re: Ulx completes
« Reply #2 on: May 31, 2013, 10:06:14 am »
Does that mean the completes are loaded before the rest of the commands in the lua file?

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Ulx completes
« Reply #3 on: May 31, 2013, 10:27:21 am »
It means your table is ready before you pass it in, yes.
Experiencing God's grace one day at a time.

Offline skillz

  • Newbie
  • *
  • Posts: 29
  • Karma: 1
Re: Ulx completes
« Reply #4 on: May 31, 2013, 10:30:57 am »
Hmmmm ok ill try and work out why its not working.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Ulx completes
« Reply #5 on: May 31, 2013, 10:42:46 am »
A good way to tell is to print out the table directly after you pass it. For example, with the votemap command:

Code: Lua
  1. local votemap2 = ulx.command( CATEGORY_NAME, "ulx votemap2", ulx.votemap2, "!votemap2" )
  2. votemap2:addParam{ type=ULib.cmds.StringArg, completes=ulx.maps, hint="map", error="invalid map \"%s\" specified", ULib.cmds.restrictToCompletes, ULib.cmds.takeRestOfLine, repeat_min=1, repeat_max=10 }
  3. votemap2:defaultAccess( ULib.ACCESS_ADMIN )
  4. votemap2:help( "Starts a public map vote." )
  5. PrintTable(ulx.maps) -- This is the important line, see if it prints what you're expecting
Experiencing God's grace one day at a time.

Offline skillz

  • Newbie
  • *
  • Posts: 29
  • Karma: 1
Re: Ulx completes
« Reply #6 on: May 31, 2013, 11:33:29 am »
The table i was trying to reference was actually the EquipmentItems table that's created in the ttt files,  i was referencing it inside the complete, obvious if the completes are loaded before the creation of the table then it would be nil. Tahts why i came to the conclusion that the completes are being loaded before hand. I am however hoping thats not the case because i see no way to load the table as completes if that's the case.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Ulx completes
« Reply #7 on: May 31, 2013, 11:53:47 am »
The other option is to keep the reference constant, but this requires a deeper understanding of how Lua tracks objects. If you share the relevant bits of code I'll help you modify it to work. The relevant bits being where the table is actually generated as well as where you are building the command that uses the completes.
Experiencing God's grace one day at a time.

Offline skillz

  • Newbie
  • *
  • Posts: 29
  • Karma: 1
Re: Ulx completes
« Reply #8 on: May 31, 2013, 12:16:05 pm »
The table is generated in garrysmod\gamemodes\terrortown\gamemode\gamemode\equip_items_shd.lua
Code: [Select]

-- This table is used by the client to show items in the equipment menu, and by
-- the server to check if a certain role is allowed to buy a certain item.


-- If you have custom items you want to add, consider using a separate lua
-- script that uses table.insert to add an entry to this table. This method
-- means you won't have to add your code back in after every TTT update. Just
-- make sure the script is also run on the client.
--
-- For example:
--   table.insert(EquipmentItems[ROLE_DETECTIVE], { id = EQUIP_ARMOR, ... })
--
-- Note that for existing items you can just do:
--   table.insert(EquipmentItems[ROLE_DETECTIVE], GetEquipmentItem(ROLE_TRAITOR, EQUIP_ARMOR))


-- Special equipment bitflags. Every unique piece of equipment needs its own
-- id. The number should increase by a factor of two for every item (ie. ids
-- should be powers of two). So if you were to add five more pieces of
-- equipment, they should have the following ids: 8, 16, 32, 64, 128...
EQUIP_NONE     = 0
EQUIP_ARMOR    = 1
EQUIP_RADAR    = 2
EQUIP_DISGUISE = 4


-- Icon doesn't have to be in this dir, but all default ones are in here
local mat_dir = "VGUI/ttt/"


-- Stick to around 35 characters per description line, and add a "\n" where you
-- want a new line to start.

EquipmentItems = {
   [ROLE_DETECTIVE] = {

      -- body armor
      {  id       = EQUIP_ARMOR,
         loadout  = true, -- default equipment for detectives
         type     = "item_passive",
         material = mat_dir .. "icon_armor",
         name     = "item_armor",
         desc     = "item_armor_desc"
      },

      -- radar
      {  id       = EQUIP_RADAR,
         type     = "item_active",
         material = mat_dir .. "icon_radar",
         name     = "item_radar",
         desc     = "item_radar_desc"
      }


      -- The default TTT equipment uses the language system to allow
      -- translation. Below is an example of how the type, name and desc fields
      -- would look with explicit non-localized text (which is probably what you
      -- want when modding).

--      {  id       = EQUIP_ARMOR,
--         loadout  = true, -- default equipment for detectives
--         type     = "Passive effect item",
--         material = mat_dir .. "icon_armor",
--         name     = "Body Armor",
--         desc     = "Reduces bullet damage by 30% when\nyou get hit."
--      },
   };


   [ROLE_TRAITOR] = {
      -- body armor
      {  id       = EQUIP_ARMOR,
         type     = "item_passive",
         material = mat_dir .. "icon_armor",
         name     = "item_armor",
         desc     = "item_armor_desc"
      },

      -- radar
      {  id       = EQUIP_RADAR,
         type     = "item_active",
         material = mat_dir .. "icon_radar",
         name     = "item_radar",
         desc     = "item_radar_desc"
      },

      -- disguiser
      {  id       = EQUIP_DISGUISE,
         type     = "item_active",
         material = mat_dir .. "icon_disguise",
         name     = "item_disg",
         desc     = "item_disg_desc"
      }
   };
};


-- Search if an item is in the equipment table of a given role, and return it if
-- it exists, else return nil.
function GetEquipmentItem(role, id)
   local tbl = EquipmentItems[role]
   if not tbl then return end

   for k, v in pairs(tbl) do
      if v and v.id == id then
         return v
      end
   end
end

I'm creating the command in garrysmod\addons\ULX\lua\modules\sh\ttt_fun.lua

Code: [Select]
--[Helper Functions]---------------------------------------------------------------------------
function GetEquipment()
local item_table
for role,_ in pairs(EquipmentItems) do
for _, item in pairs(EquipmentItems[])
EquipmentItems
end

local items = MergeTable(EquipmentItems[ROLE_TRAITOR], EquipmentItems[ROLE_DETECTIVE])
if type(items_table) == "table" then
for _, item in pairs(items_table) do
if item.id then
table.insert(ulx.get_equipment, item.id)
end
end
end
return tbl
end
--[End]----------------------------------------------------------------------------------------



--[Ulx Completes]------------------------------------------------------------------------------
ulx.get_equipment = {}
function GetEquipment()
table.Empty( ulx.get_equipment )

local items = MergeTable(EquipmentItems[ROLE_TRAITOR], EquipmentItems[ROLE_DETECTIVE])
if type(items_table) == "table" then
for _, item in pairs(items_table) do
if item.id then
table.insert(ulx.get_equipment, item.id)
end
end
end
end
hook.Add( ULib.HOOK_UCLCHANGED, "ULXGetEquipment", GetEquipment )
GetEquipment()
--[End]----------------------------------------------------------------------------------------



--[Toggle spectator]---------------------------------------------------------------------------
--[[ulx.spec][Forces <target(s)> to and from spectator.]
@param  {[PlayerObject]} calling_ply   [The player who used the command.]
@param  {[PlayerObject]} target_plys   [The player(s) who will have the effects of the command applied to them.]
--]]
function ulx.GiveEquipment( calling_ply, target_plys, equipment )
if not GetConVarString("gamemode") == "terrortown" then ULib.tsayError( calling_ply, gamemode_error, true ) else
for i=1, #target_plys do
target_plys[ i ]:GiveEquipmentItem(equipment)
end
ulx.fancyLogAdmin( calling_ply, "#A gave #T " .. equipment, target_plys)
end
end
local armor = ulx.command( CATEGORY_NAME, "ulx Give Equipment", ulx.GiveEquipment )
GiveEquipment:addParam{ type=ULib.cmds.PlayersArg }
GiveEquipment:addParam{ type=ULib.cmds.StringArg, completes=ulx.get_equipment, hint="Equipment", error="Invalid equpiment:\"%s\" specified", ULib.cmds.restrictToCompletes }
GiveEquipment:defaultAccess( ULib.ACCESS_SUPERADMIN )
GiveEquipment:help( "Give <target(s)> specified equpiment." )
--[End]----------------------------------------------------------------------------------------

I can sort out the rest, i know how the completes work from the examples included in other ulx commands, it's mainly just referencing the table. Also thanks for dedicating so much time to my cause, i'm not sure if i would do the same although i would hope that i would. The code is quite broken and mismatched, i normally fix the code as i go along but i can't work out what's wrong if i can't get the table.
« Last Edit: May 31, 2013, 12:19:37 pm by skillz »

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Ulx completes
« Reply #9 on: May 31, 2013, 02:02:46 pm »
Hmmm, that table format is a lot more complicated than I expected and I'm not sure what string your trying to pull out exactly (your code appears to be quite broken). But, it does seem like all the data you need should be available more or less immediately. For your case I think I'd recommend putting your command registration in an "Initialize" hook callback. You can then also preprocess the autocomplete table according to your needs. Try to make it as simple as possible first just to make sure it works, then build the complexity up.
Experiencing God's grace one day at a time.

Offline skillz

  • Newbie
  • *
  • Posts: 29
  • Karma: 1
Re: Ulx completes
« Reply #10 on: May 31, 2013, 03:00:20 pm »
Yes it would seem badking likes complicated, i had to revise some of what i know on tables but i can reference the tables correctly in simple tests. How would i go about doing an "Initialize" hook callback? please provide a small extract of code? The reason why the code is so broken is because I literally copied it over from another function, changed a few things and i was still very much undecided about what route to take, i think i left the code halfway through changing to another method.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Ulx completes
« Reply #11 on: May 31, 2013, 03:05:21 pm »
Code: Lua
  1. local function onInit()
  2.   -- Create your table here, in this case it would be "ulx.maps".
  3.   local votemap2 = ulx.command( CATEGORY_NAME, "ulx votemap2", ulx.votemap2, "!votemap2" )
  4.   votemap2:addParam{ type=ULib.cmds.StringArg, completes=ulx.maps, hint="map", error="invalid map \"%s\" specified", ULib.cmds.restrictToCompletes, ULib.cmds.takeRestOfLine, repeat_min=1, repeat_max=10 }
  5.   votemap2:defaultAccess( ULib.ACCESS_ADMIN )
  6.   votemap2:help( "Starts a public map vote." )
  7. end
  8. hook.Add("Initialize", "TestInitHere", onInit)
Experiencing God's grace one day at a time.

Offline skillz

  • Newbie
  • *
  • Posts: 29
  • Karma: 1
Re: Ulx completes
« Reply #12 on: May 31, 2013, 03:49:40 pm »
Hmmmm ill give that a try, one reason i didn't just make the table then and there is that custom equipment can be added to the table elsewhere, like on my own ttt sever. would this still be the best way to do this?

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Ulx completes
« Reply #13 on: May 31, 2013, 08:11:13 pm »
Hmmmm ill give that a try, one reason i didn't just make the table then and there is that custom equipment can be added to the table elsewhere, like on my own ttt sever. would this still be the best way to do this?

Yes, all the custom additions should have finished at this point
Experiencing God's grace one day at a time.

Offline skillz

  • Newbie
  • *
  • Posts: 29
  • Karma: 1
Re: Ulx completes
« Reply #14 on: June 01, 2013, 08:15:19 am »
This is my current code, the table is now found and the id's are loaded in correctly, they are numbers. i've still got to remove duplicates though.
Code: [Select]
--[Ulx Completes]------------------------------------------------------------------------------
ulx.get_equipment = {}
function GetEquipment()
table.Empty( ulx.get_equipment )

for role, _ in pairs(EquipmentItems) do
for _, item in pairs(EquipmentItems[role]) do
table.insert(ulx.get_equipment, tostring(item.id))
end
end
end
hook.Add( "Initialize", "ULXGetEquipment", GetEquipment )
hook.Add( ULib.HOOK_UCLCHANGED, "ULXGetEquipment", GetEquipment )
--[End]----------------------------------------------------------------------------------------



--[Toggle spectator]---------------------------------------------------------------------------
--[[ulx.spec][Forces <target(s)> to and from spectator.]
@param  {[PlayerObject]} calling_ply   [The player who used the command.]
@param  {[PlayerObject]} target_plys   [The player(s) who will have the effects of the command applied to them.]
--]]
function ulx.GiveEquipment( calling_ply, target_plys, equipment )
if not GetConVarString("gamemode") == "terrortown" then ULib.tsayError( calling_ply, gamemode_error, true ) else
for i=1, #target_plys do
target_plys[i]:GiveEquipmentItem(tonumber(equipment))
end
--ulx.fancyLogAdmin( calling_ply, "#A gave #T " .. equipment, target_plys)
end
end
local gequip = ulx.command( CATEGORY_NAME, "ulx GiveEquipment", ulx.GiveEquipment )
gequip:addParam{ type=ULib.cmds.PlayersArg }
gequip:addParam{ type=ULib.cmds.StringArg, completes=ulx.get_equipment, hint="Equipment", error="Invalid equpiment:\"%s\" specified", ULib.cmds.restrictToCompletes }
gequip:defaultAccess( ULib.ACCESS_SUPERADMIN )
gequip:help( "Give <target(s)> specified equpiment." )
--[End]----------------------------------------------------------------------------------------

Whenever i run the command i get this error in the server console:

[ERROR] addons/ulib/lua/ulib/shared/commands.lua:1296: Invalid command!
1. __fn - [C]:-1
2. unknown - addons/ulib/lua/ulib/shared/commands.lua:1296
3. Run - lua/includes/modules/concommand.lua:69
4. unknown - addons/ulib/lua/ulib/shared/commands.lua:1310
5. unknown - lua/includes/modules/concommand.lua:69

I also get this error when it tries to autocomplete in the console, as soon as i press space after typing "ulx GiveEquipment", I get this error:

[ERROR] Invalid command!
  1. unknown - [C]:-1

  • Print