Skip to content

Instantly share code, notes, and snippets.

@Maxs1789
Last active September 20, 2017 02:09
Show Gist options
  • Save Maxs1789/ace8f1cc609335df95cf to your computer and use it in GitHub Desktop.
Save Maxs1789/ace8f1cc609335df95cf to your computer and use it in GitHub Desktop.
A debug file script for games made with the Solarus engine and that uses a debug console system.
-- Maxs1789 solarus debug script v0.1
local console = ...
local environment = {}
-- Returns the current game.
local function get_game()
return sol.main.game
end
-- Returns the current map.
local function get_map()
local game = get_game()
return game and game:get_map() or nil
end
-- Returns an entity of the current map.
local function get_entity(name)
local map = get_map()
return map and map:get_entity(name) or nil
end
-- Returns the hero entity.
local function get_hero()
return get_entity("hero")
end
-- Index function of the environment.
local function index(env, key)
-- main
if key == "env" then
return env
elseif key == "game" then
return get_game()
end
-- map
local map = get_map()
if key == "map" then
return map
elseif key == "map_id" then
return map and map:get_id() or nil
end
-- entity
local entity = map and map:get_entity(key) or nil
if entity ~= nil then
return entity
end
-- game value
return environment.getval(key)
end
-- Returns all the keys of a table.
function environment.keys(tab)
if type(tab) == "table" then
local keys = {}
for k in pairs(tab) do
table.insert(keys, k)
end
return unpack(keys)
end
return nil
end
-- Returns all the keys of the metatable value.
function environment.mkeys(val)
return environment.keys(getmetatable(val))
end
-- Returns all the named entities of the current map.
function environment.named_entities()
local map = get_map()
if map ~= nil then
local named_entities = {}
for e in map:get_entities("") do
local name = e:get_name()
if name ~= nil then
table.insert(named_entities, name)
end
end
return unpack(named_entities)
end
return nil
end
-- Alias of game:get_value method.
function environment.getval(key)
local game = get_game()
return game and game:get_value(key) or nil
end
-- Alias of game:set_value method.
function environment.setval(key, value)
local game = get_game()
if game ~= nil then
game:set_value(key, value)
end
end
-- Alias of hero:start_treasure method.
function environment.give(treasure, variant)
local hero = get_hero()
if hero ~= nil then
hero:start_treasure(treasure, variant)
end
end
-- Alias of hero:teleport method.
function environment.tp(map, destination)
local hero = get_hero()
if hero ~= nil then
hero:teleport(map, destination)
end
end
-- Alias of map:open_doors method.
function environment.open(prefix)
local map = get_map()
if map ~= nil then
map:open_doors(prefix or "")
end
end
setmetatable(environment, {__index = index})
return environment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment