Run lua functions from the command line

Discussion in 'Lua Discussions' started by Tiina Onir, Nov 12, 2019.

Thread Status:
Not open for further replies.
  1. Tiina Onir

    Tiina Onir Avatar

    Messages:
    1,103
    Likes Received:
    1,900
    Trophy Points:
    125
    Location:
    Bramble, South Paladis
    While not as powerful as being able to do /lua MyFuncHere("awesome!", "args!") I did quickly put together a way to run your own commands:

    Code:
    playerName = 'Tiina Onir';
    
    command = { };
    
    function command.ShowStats()
      ConsoleLog(string.format("total stats: %d", ShroudGetStatCount()));
      for i=0, ShroudGetStatCount(), 2 do
        ConsoleLog(string.format('%d: %s - %d: %s', i, ShroudGetStatNameByNumber(i), i+1, ShroudGetStatNameByNumber(i+1)));
      end
    end
    
    function command.ShowStat(si)
      i = tonumber(si);
      ConsoleLog(string.format('(%d)%s: %f', i, ShroudGetStatNameByNumber(i), ShroudGetStatValueByNumber(i)));
    end
    
    function command.findStat(args)
      for i=0, ShroudGetStatCount(), 1 do
        if string.match(string.lower(ShroudGetStatNameByNumber(i)), string.lower(args)) then
          ConsoleLog(string.format('(%d) %s', i, ShroudGetStatNameByNumber(i)));
        end
      end
    end
    
    function ShroudOnConsoleInput(type, src, msg)
      if type == 'Local' and src == playerName then
        local cmd, arg = string.match(msg, '\\(%w+)%s*(%w*)');
        if not pcall(command[cmd], arg) then
          ConsoleLog("Command Not Found");
        end
      end
    end
    that will allow you to say "\ShowStats" and it will list all the stats and their values, or "\ShowStat 39" and it will show you stat 39, it's name, and it's value. You can add any command to this by just making it "command.AwsomeFunctionName()", although you're limited to just one string argument (you can break it apart once your in the function).

    EDIT: you do have to change the player name to be your name!
     
    Last edited: Nov 14, 2019
  2. Tiina Onir

    Tiina Onir Avatar

    Messages:
    1,103
    Likes Received:
    1,900
    Trophy Points:
    125
    Location:
    Bramble, South Paladis
    I've updated the code above to handle non-existent commands better
     
    Drake Aedus likes this.
  3. Tiina Onir

    Tiina Onir Avatar

    Messages:
    1,103
    Likes Received:
    1,900
    Trophy Points:
    125
    Location:
    Bramble, South Paladis
    I added a "findStat" command to search for stats by name, and tell you the number for them.
     
Thread Status:
Not open for further replies.