Change storage variable from game

Place to get help with not working mods / modding interface.
User avatar
SLywnow
Inserter
Inserter
Posts: 39
Joined: Sat Apr 25, 2020 7:37 pm
Contact:

Change storage variable from game

Post 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?
User avatar
SLywnow
Inserter
Inserter
Posts: 39
Joined: Sat Apr 25, 2020 7:37 pm
Contact:

Re: Change storage variable from game

Post 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
Pi-C
Smart Inserter
Smart Inserter
Posts: 1737
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Change storage variable from game

Post by Pi-C »

SLywnow wrote: Sat Nov 16, 2024 10:23 am 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
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.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Honktown
Smart Inserter
Smart Inserter
Posts: 1042
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: Change storage variable from game

Post 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. .

Code: Select all

/c
storage.value = "x"

Code: Select all

/c
game.print(storage.value)
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.

Code: Select all

/c
game.print("hello")
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 ]]
I have mods! I guess!
Link
User avatar
SLywnow
Inserter
Inserter
Posts: 39
Joined: Sat Apr 25, 2020 7:37 pm
Contact:

Re: Change storage variable from game

Post 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
Post Reply

Return to “Modding help”