I think we've tried something similar to that, but we came across quite a few bugs with that approach: Link would alternate inconsistently between player and AI control, and it looks like there's no way to control anything other than movement from a Movement object. We'd like to get swordfighting, items, and other elements under AI control, as well, so that may not be the best approach.
My current idea is to just expose some of the GameCommand methods to lua ("game_command_pressed" and "game_command_released," specifically). That way, player input can work like it normally does, with the only change being that the commands are coming from a Lua script instead of the keyboard/joystick. Would that work just as well?
Here's the code I'm using. It compiles and runs without errors, but it doesn't do anything at the moment. I feel like I'm close to getting it working, so any help figuring out what I'm missing would be great.
In GameCommands.h
// These are now public instead of private.
void game_command_pressed(Command command);
void game_command_released(Command command);
In Game.h
// force commands
void force_command_pressed(GameCommands::Command command);
void force_command_released(GameCommands::Command command);
In Game.cpp
/**
* \brief Forces a command into the command set.
*/
void Game::force_command_pressed(GameCommands::Command command){
commands->game_command_pressed(command);
}
/**
* \brief Forces a command into the command set.
*/
void Game::force_command_released(GameCommands::Command command){
commands->game_command_released(command);
}
In GameAPI.cpp
//These lines were added to methods[]
{ "force_command_pressed", game_api_force_command_pressed },
{ "force_command_released", game_api_force_command_released },
//...
//Force command functions
int LuaContext::game_api_force_command_pressed(lua_State* l){
//Read a string from Lua
//Compare the string with our Command enums
Savegame& savegame = check_game(l, 1);
GameCommands::Command command = check_enum<GameCommands::Command>(
l, 2, GameCommands::command_names);
Game* game = savegame.get_game();
//Call game.force_command_pressed on the resultant command
game->force_command_pressed(command);
return 0;
}
int LuaContext::game_api_force_command_released(lua_State* l){
//Read a string from Lua
//Compare the string with our Command enums
Savegame& savegame = check_game(l, 1);
GameCommands::Command command = check_enum<GameCommands::Command>(
l, 2, GameCommands::command_names);
Game* game = savegame.get_game();
//Call game.force_command_pressed on the resultant command
game->force_command_released(command);
return 0;
}
In LuaContext.h:
//These were added under "FunctionExportedToLua"
game_api_force_command_pressed,
game_api_force_command_released,
And lastly, in main.lua, under update():
function sol.main:on_update()
force_command_pressed("up")
force_command_pressed("attack")
--...