Question

So I'm trying to create a list of console commands for a game, and to make it slightly nicer I want to have three commands on a single line.

Example:

Commands.afk.Args.Name = "!afk";
Commands.transfer.Args.Name = "!transfer";
Commands.jump.Args.Name = "!jump";

Would be:

[ !afk ] [ !jump ] [ !transfer ]

I think I know how to do this (by using table.concat), but this code also requires to verify that the commands we're forwarding belong to the user's access group (we don't want to send them administrator commands if they're just a guest for instance).

Commands.[commandname].Args.Access returns a number between 1 and 6, which corresponds to the membership grade that the user belongs to. The function Core:GetAccess(player) (where player is the user's unique entity table) returns the membership group that the user belongs to- the Commands.[commandname].Args.Access and Core:GetAccess(player) need to match in order to allow the command to be listed.

I'm not too fussed about formatting at the moment, I'm just more concerned whether my specific usage case is actually possible or not. If it is possible, can a simple table.concat be used?

I've looked on the internet for the solution to this, but nothing appears to match my specifric usage case.

Was it helpful?

Solution

You could just iterate through all commands and set it inside loop.

for i,v in pairs(Commands) do
    v.Args=v.Args or {};
    v.Args["name"]=i;
end

Just call this little loop right after you load all chat-commands.

OTHER TIPS

table.concat only formats plain string items from array-like tables.

If you need to do more complicated work then that that you need to write some code yourself.

Something like the following might work:

local available_commands = {}
for cmd, tab in pairs(Commands) do
    if cmd.Args.Access == Core:GetAccess(player) then
        available_commands[#available_commands + 1] = "[!"..cmd.Args.Name.."]"
    end
end
local display_string = table.concat(available_commands)
print(display_string)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top