TTT-2 / TTT2

Trouble in Terrorist Town 2 for Garry's Mod (gmod)
https://steamcommunity.com/sharedfiles/filedetails/?id=1357204556
178 stars 74 forks source link

Error while trying to call GetEquipmentForRole(role) #619

Closed Sendaford closed 4 years ago

Sendaford commented 4 years ago

Summary

I'm not able to use the GetEquipmentForRole(role) method in my addon. It probably refers to an "old" lua file from TTT1. See error below.

Version

Description

Error in console while trying to call GetEquipmentForRole(ROLE_TRAITOR) from cl_equip.lua in an addon's lua file. Tried to call it in different scopes/events/hooks...

Console output:

[TTT2 (Base) - v0.7.2b - New Roles, Extensible, Better Performance, ULX Support] gamemodes/terrortown/gamemode/shared/sh_equip_items.lua:280: table index is nil
  1. GetEquipmentForRole - gamemodes/terrortown/gamemode/shared/sh_equip_items.lua:280
   2. v - addons/ioverride/lua/autorun/icon_overrides.lua:61
    3. Call - lua/includes/modules/hook.lua:84
     4. RoundStateChange - gamemodes/terrortown/gamemode/client/cl_main.lua:321
      5. func - gamemodes/terrortown/gamemode/client/cl_main.lua:412
       6. unknown - lua/includes/extensions/net.lua:32

To reproduce

Steps to reproduce the behaviour:

  1. Create a new autorun lua file (addons/[addon-name]/lua/autorun/[name].lua)
  2. Make it client-side only (if CLIENT then ...)
  3. Call GetEquipmentForRole(ROLETRAITOR) (e.g. local tbl = GetEquipmentForRole(ROLETRAITOR))
  4. See console output for details

Expected behaviour

I expect the method to return a table with all buyable items for the specified role, to be able to edit item names and icons.

Additional context

Tried to implement a copy of this function in the addon's lua file. It returned the desired but I was not able to edit their metadata (name, icon, type, desc, ...)

saibotk commented 4 years ago

Thank you for your interest :)

Sorry, that there is no handy way to read / have a documentation for the functions available. We are working on that, but are super limited in time at the moment due to various reasons. For now the most complete documentation can be found in the documentation that is attached to each function in the source code (You can for example use Githubs search and find the function / the definition of that function and there you can also find some documentation).

So for your specific case, i looked it up, and found that we do not have a function with your signature (with only a role argument). The function is defined here: https://github.com/TTT-2/TTT2/blob/c49cd970f1a6a89118acb1411e3cac503b546919/gamemodes/terrortown/gamemode/shared/sh_equip_items.lua#L219

and its first parameter has to be the player and the second one should be the role you want to access the shop from. While looking this up, i noticed, that we really have to clean this file up too after our UI Rework and am putting that on our TODO list. (eg. dunno why you would need a player to get the equipment, except when modified like it is when using the random shop)

If you got any other issues / comments / ideas for improvement / questions regarding anything, feel free to ask. We are happy to help you get that to work, as long as our spare time allows it. Otherwise i can always recommend joining our Discord, where you also may ask questions and get much more responses from various other people that might also be happy to help ;)

Sendaford commented 4 years ago

Hi, thanks for the quick reply. It helped me a lot.

Seems like I was working with TTT instead of TTT2. Silly me. https://github.com/Pustekuchen98/TTT/blob/ed55c561dd4bd043dd9e235049f65f42391e4ee6/gamemode/cl_equip.lua#L10

For now, I'll try to use the given method with three parameters from https://github.com/TTT-2/TTT2/blob/c49cd970f1a6a89118acb1411e3cac503b546919/gamemodes/terrortown/gamemode/shared/sh_equip_items.lua#L219 and will report later on it.

Btw. great work with TTT2, I really enjoy it.

Sendaford commented 4 years ago

Hey again, I managed to alter icons displayed in the traitor shop using following code:

if SERVER then return end

local a = GetEquipmentForRole(LocalPlayer(), ROLE_TRAITOR, false)
for k,v in pairs(a) do
    if v and v.CanBuy then
        local icon = "materials/vgui/ttt/icons/" .. v.id .. ".png"
        if(file.Exists(icon, "GAME")) then
            print("Found and replaced: " .. v.id)
            v.material = icon
            v.ttt2_cached_material = icon
        end
    end
end

So it works perfectly with three params. Thanks again.