Jump to content

Steam workshop mods - appending. How to change existing function?


necris

Recommended Posts

New to Avorion modding but have been modding since Morrowind and Starcraft, programmed since last millenia and works as an IT consultant.

 

Making small changes and testing stuff have been easy, but when I now want to do it properly I run into some problems.

I think I'm just to unfamiliar still to come up with any good solutions and am therefore in need of your help.

 

I want to make some changes to existing functions in the files below so that a miner uses my custom mine command instead:

 

data/scripts/entity/ai/mine.lua
data/scripts/entity/craftorders.lua
data/scripts/player/map/mapcommands.lua

 

According to the "Write your own mod"-guide I can't just change what order is called from galaxymap portraits and/or right-click context menu. Mod data is appended to files with nameclashes so I couldn't overwrite what those clicks do.

 

How would you solve this?  Make a new mine.lua file and a new command added to the context menu that uses that new mine.lua file?

Am I overthinking this and missing something simple?

Link to comment
Share on other sites

Am I overthinking this and missing something simple?

 

If you just want to change the existing mine command, yes. You dont have to modify these:

data/scripts/entity/craftorders.lua
data/scripts/player/map/mapcommands.lua

 

You can simply overwride just the mine.lua like I did in the attached example. This will modify the vanilla game file. All other scripts and the game itself will use the modded script this way.

miningExample.zip

Link to comment
Share on other sites

Am I overthinking this and missing something simple?

 

If you just want to change the existing mine command, yes. You dont have to modify these:

data/scripts/entity/craftorders.lua
data/scripts/player/map/mapcommands.lua

 

You can simply overwride just the mine.lua like I did in the attached example. This will modify the vanilla game file. All other scripts and the game itself will use the modded script this way.

 

Thanks for the example, I'm a little confused though. The original Mine.lua already contains a function called AIMine.findMinedAsteroid(), doesn't appending another cause issues? Do you know if the function replaced or if the code is appended to that function? Where in that function is it appended is it appended if it is appended?

 

 

Link to comment
Share on other sites

Let me improve the comments of my example mine.lua:

 

local MyModTemplateFindMinedAsteroid = AIMine.findMinedAsteroid -- Save original function in a local variable
function AIMine.findMinedAsteroid() -- Override original function; we still have it in 'MyModTemplateFindMinedAsteroid'
    -- Add some custom code here...
    
    if myCustomVariable == true then -- When this is true, skip searching for asteroids, but do not throw warnings to player
        noAsteroidsLeft = true
        noAsteroidsLeftTimer = 10 * 60 -- ten minutes
        
        ShipAI():setPassive()
    else
        -- Execute original function, we stored before
        -- You could skip this to completely override it, but that may decrease mod compatibility to other mods. Sometimes its required though
        MyModTemplateFindMinedAsteroid()
    end
    -- Add more custom code here
end

 

I think that should make it clear. If not feel free to ask.

 

Link to comment
Share on other sites

Let me improve the comments of my example mine.lua:

 

local MyModTemplateFindMinedAsteroid = AIMine.findMinedAsteroid -- Save original function in a local variable
function AIMine.findMinedAsteroid() -- Override original function; we still have it in 'MyModTemplateFindMinedAsteroid'
    -- Add some custom code here...
    
    if myCustomVariable == true then -- When this is true, skip searching for asteroids, but do not throw warnings to player
        noAsteroidsLeft = true
        noAsteroidsLeftTimer = 10 * 60 -- ten minutes
        
        ShipAI():setPassive()
    else
        -- Execute original function, we stored before
        -- You could skip this to completely override it, but that may decrease mod compatibility to other mods. Sometimes its required though
        MyModTemplateFindMinedAsteroid()
    end
    -- Add more custom code here
end

 

I think that should make it clear. If not feel free to ask.

 

Thanks, my lua skills are lacking. I get now that Lua does not overload functions, it handles name conflicts by using the most recently defined function, which is why you save the previous one since the version in the mod will be appended to the end and therefore always be the "most recent one".

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