HowTo: Execute console commands from Lua

Here’s a simple Gem that adds a new EBUS that lets you run console commands from Lua.

PRO TIP: You can use a console command to exit game mode in the editor. Very handy when you have a Quit button in your UI that you want to exit the game in the launcher but only leave game mode in the editor. Just execute the following command from Lua when the button is pressed: “py general.exit_game_mode()”

4817-luaconsolegem-ly19.zip (17.3 KB)luaconsolegem-ly19.zip

To install:

  1. Extract the zip to your dev/Gems folder

  2. Open ProjectConfigurator, enable this Gem in your project and save

  3. In a command window run lmbr_waf configure

  4. Build your project from Visual Studio or the command line
    Example usage:

  5. Create an entity that has a Lua script component.

  6. Select the dev/Gems/LuaConsole/scripts/components/LuaConsoleTestComponent.lua file

  7. A Command property will appear in the component’s properties after you select this file

  8. Enter a console command to run or leave the example one

  9. Run the game and the command will be executed immediately
    LuaConsoleTestComponent.lua

    local LuaConsole = {
Properties = {
Command = { default="r_displayInfo 2", description="Console command to run automatically"}
}
}
function LuaConsole:OnActivate()
-- run the console command
LuaConsoleRequestBus.Broadcast.ExecuteConsoleString(self.Properties.Command)
end
return LuaConsole

Awesome! I needed something like this earlier today to change the map from my menu to the game. I ended up modifying the StarterGameUtility to bring in a new lua command but this will be a more robust solution.

This Gem is what I’ve been looking for.

Thank you!!

Hello, I tried to build this, but got the next exception:

Output:
                StdAfx.cpp
                e:\amazon\lumberyard\1.21.2.0\dev\gems\luaconsole\code\source\StdAfx.h(4): fatal error C1083: Cannot open include file: 'platform.h': No such file or directory