Page 1 of 1
Change storage variable from game
Posted: Fri Nov 15, 2024 3:53 pm
by SLywnow
Is it possible to change mod's storage variable by some command? I know that
Code: Select all
/c __mod name__ game.player.print(serpent.dump(storage))
will show all data from mod, but how can i change it?
Re: Change storage variable from game
Posted: Sat Nov 16, 2024 10:23 am
by SLywnow
if someone will find it, i found only one solution: custom commands
use this event to run function that will change variable you need
https://lua-api.factorio.com/latest/cla ... mmand_used
Re: Change storage variable from game
Posted: Sat Nov 16, 2024 11:19 am
by Pi-C
LuaGameScript::console_command_used is a read-only flag (Boolean value) that is set when the player has used a console command ('/c some_command'). When the flag hasn't been set, you will be prompted to enter the command again because that will disable achievements for that game.
I guess what you really meant was the
on_console_command event. However, I don't think this will give you write access to a mod's table 'storage'! After all, the general idea is that the environment of each mod is sandboxed, so that other mods can't interfere with it.
What you could do is hacking the mod by editing your local copy: just
define a command that will write to that mod's table 'storage' in control.lua (create the file if it doesn't exist)! As the function for the command has been defined within that mod, it will be able to read/write the mods data.
Re: Change storage variable from game
Posted: Sat Nov 16, 2024 7:46 pm
by Honktown
/c __mod-name
is Lua code executed in the mods environment. Changing a mods global storage in a command would be executed as being within the mod itself, as if ran during on_configuration_changed or in a new command e.g. .
There is one small issue with command code, that newlines are removed. A short comment in lua is usually written leading with -- , and ending at a new line. This unfortunately, when [copied and pasted] as a command, forces all the following text to be behind the comment.
versus
Code: Select all
/c
local condition = true
if condition then
-- only run if condition is true
game.print("hello")
end
Error (with the benefit that commands are safety wrapped, so an _immediate_ error doesn't kick one out to the main menu.
Edit:
The long form of a comment looks like --[[ only run if condition is true ]] (usually), and since that has delimiters, code will continue on after the ]]
Re: Change storage variable from game
Posted: Sat Nov 16, 2024 10:22 pm
by SLywnow
Guys, it's really much easier to make just variable editor command like this:
Code: Select all
script.on_event(defines.events.on_console_command,function(event)
command(event)
end)
function command (event)
if event.command == "your_command" then
local par = splitString(event.parameters," ")
--now you can get command parameters by id, like:
local num = tonumber(par[2]) --converts second parameter into int
--checking first parameter and changing variable
if par[1] == "set" then
storage.variable = num
elseif par[1] == "add" then
storage.variable = storage.variable+num
end
end
end
function splitString(input, delimiter)
local result = {}
local pattern = string.format("([^%s]+)", delimiter)
for word in string.gmatch(input, pattern) do
table.insert(result, word)
end
return result
end
I tested it already and it works pretty cool