Commands From Chat

Commands From Chat

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Commands From Chat

Post by kalda341 »

I am having problems executing any commands from chat. I really need to be able to do a /spring.echo ("something") but I get no response.
0 x

User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7024
Joined: 16 Nov 2004, 13:08

Re: Commands From Chat

Post by zwzsg »

Chat doesn't take Lua commands.
0 x

User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Commands From Chat

Post by knorke »

SendCommands()
ie Spring.SendCommands ("resbar 0") to hide resource bars

edit
wops misread
Last edited by knorke on 12 Dec 2010, 20:52, edited 1 time in total.
0 x

kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Commands From Chat

Post by kalda341 »

So is there anyway to run a command and get a reply in chat? I need a print or echo.
0 x

User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7024
Joined: 16 Nov 2004, 13:08

Re: Commands From Chat

Post by zwzsg »

Nah but kalda341 is trying to do the other way around. (Not chat command in Lua, but Lua in chat commands).

Which is not possible, unless you write a special gadget to retrieve and parse and execute what's written in chat.
0 x

kalda341
Posts: 101
Joined: 19 Nov 2010, 09:44

Re: Commands From Chat

Post by kalda341 »

Is there any developers console in spring?
0 x

SirMaverick
Posts: 834
Joined: 19 May 2009, 21:10

Re: Commands From Chat

Post by SirMaverick »

What you can do is to bind a chat command to execute a certain function. Within that function you can e.g. call Spring.Echo().
0 x

User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7024
Joined: 16 Nov 2004, 13:08

Re: Commands From Chat

Post by zwzsg »

kalda341: Don't try to type Lua commands in chat. It's a chat, not a command console. Run Spring windowed, and have Notepad++ in another window with your file open. When you want to test changes, reload widget or type /luarules reload. In the gadget or widget, use Spring.Echo("Hello World!")

(There do is a way for gadgets to react to chat commands, but it's a bit tricky, not something I'd advise a noob to start with.)
0 x

User avatar
quantum
Posts: 590
Joined: 19 Sep 2006, 22:48

Re: Commands From Chat

Post by quantum »

Here's a widget that executes Lua commands typed in the console. It's from 2007 though, it might need some fixing.

Code: Select all

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    cmd_doline.lua
--  brief:   adds a command to run raw LUA commands from the game console
--  author:  Dave Rodgers
--
--  Copyright (C) 2007.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:GetInfo()
  return {
    name      = "DoLine",
    desc      = "Adds '/luaui <run|echo> ...' to run lua commands  (for devs)",
    author    = "trepan",
    date      = "Jan 8, 2007",
    license   = "GNU GPL, v2 or later",
    layer     = 0,
    enabled   = true  --  loaded by default?
  }
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- Low level LUA access
--

local function RunCmd(cmd, optLine)
  print(optLine)
  local chunk, err = loadstring(optLine, 'run')
  if (chunk == nil) then
    print('doline error: ' .. err)
  else
    local success, err = pcall(chunk)
    if (not success) then
      Spring.Echo(err)
    end	
  end
  return true
end


local function EchoCmd(cmd, optLine)
  local chunk, err = loadstring("return " .. optLine, 'echo')
  if (chunk == nil) then
    print('doline error: ' .. err)
  else
    local results = { pcall(chunk) }
    local success = results[1]
    if (not success) then
      Spring.Echo(results[2])
    else
      table.remove(results, 1)
      Spring.Echo(unpack(results))
    end
  end
  return true
end


function widget:Initialize()
  widgetHandler:AddAction("doline",  RunCmd)  -- backwards compatible
  widgetHandler:AddAction("run",     RunCmd)
  widgetHandler:AddAction("echolua", EchoCmd, nil)
  widgetHandler:AddAction("echo",    EchoCmd, nil, "t") -- text only

  -- NOTE: that last entry is text only so that we do not
  --       override the default "/echo" Spring command when
  --       it is bound to a keyset.
end


--------------------------------------------------------------------------------
0 x

Post Reply

Return to “Lua Scripts”