Jump to content

How to fix the mod without restarting the server?

Recommended Posts

For example, I have some wrong code:

v = SpawnPrefab("twigs")

I forgot to write "GLOBAL." so this will cause a crash.


Is there any way to fix the mod using only console? There are many people on the server. All I need is add some variable to the mod's name space:

SpawnPrefab = GLOBAL.SpawnPrefab

Is it possible?


Link to comment
Share on other sites

To my surprise:

ModManager:GetMod("modname").env.SpawnPrefab = SpawnPrefab

would work.


That is, if your SpawnPrefab is inside a function, or a DoTaskInTime, or a DoPeriodicTask (that means the GLOBAL gets checked and assigned on every function run, so the value loads itself in other times beyond the mod loading).

Link to comment
Share on other sites

  • Developer

Another possibility would be to look at the functionality in DoReload() in reload.lua. I don't know if anyone has ever used it for reloading mod lua files, so there may be some "quirks".


But if you simply just need to drop some variables into your mod's environment, @DarkXero's method should work just fine.

Link to comment
Share on other sites

@PeterA, now that you mentioned this, I tried using it.


My first issue is that modmain is basically unreachable doing hotswap("modmain").

It went through all the scripts folders, in all my mods, and the game scripts folder.

I would have to edit the custom loader or copy the functionality of reload outside it.


Then I tried to make functions in modmain depend on values outside it. For example, I make a global table, and there I store all values and functions I will use in modmain, then I write the modmain using the table values. I use files in scripts to load these values, so when I hotswap them, if I changed any values, the table would have the new values.


Well, I got functions getting executed many times and some values not getting replaced correctly (although in some cases it worked!). I presume hotswapping modmains will have the many function executions effect: I didn't remove the postinit of an AddPrefabPostInit, and reloading the file would cause to add yet another postinit, not replace it.


Overall, I would say not worth the trouble.

If you have to unload and load the mod for changes to be correct then you may as well restart the server.

Link to comment
Share on other sites

There are various relations and  hooks like this:

fn_enabled = truecomp = require "components/comp"old_fn = comp.fnnew_fn = function(...)    if fn_enabled then        --  -> custom code    end    return old_fn(self,...)endcomp.fn = new_fn



So we need some function for unloading mod and clearing all that stuff. For example:

ModOnRemove = function()    if comp.fn == new_fn then        comp.fn = old_fn    else        fn_enabled = false    endend

Generally it isn't so necessary. Therefore it is possible to concentrate on more important things. And I will use this:

ModManager:GetMod("modname").env.SpawnPrefab = SpawnPrefab

​Thank DarkXero for neat solution. :)

Edited by Maris
Link to comment
Share on other sites

@Maris, yes Maris, that's basically what the hotswap in reload.lua does.


It works to switch values that a mod constantly reloads, or functions that get constantly executed. The downside to doing that is that some functions can't get unexecuted, and provoke permanent changes until quitting and reloading, so you end up executing a function with errors and in top of that, another one with the corrected data. A mess.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...