Jump to content

Overwrite Default Behavior/functions


Recommended Posts

So, how would one go about overwriting the functions that are normally present.


I can go in and manually change the code in the lua files to mimic the behavior I want, but obviously to create this as a mod, I cannot simply upload the new files.  Not only would that cause conflicts but as a programmer in general it just makes me want to puke thinking about modifying base files instead of overwriting the behavior via a module.


So, lets say I want to change:


function DoAgeWorld()


which would be already present in gamelogic.lua


How would I go about overwriting the inherent usage?  It seems that recreating the declaration of the function inside the mod will not overwrite the entirety of it.  So it would seem that I would have to do it some other way.  I did a bit of searching on the forum and could find similar, but not exact, topics.


It seems, from the other topics, that I can add to the function (sort of), but that I could not inherently overwrite the behavior.  This doesn't sound right.


A side question - how does the priority call within the modinfo.lua work?  Is it even looked at?

Link to comment
Share on other sites

DoAgeWorld is a local function. In Lua functions are just regular values, and a local function is just a local variable storing a function. Additionally, a file is just a function. So what you're trying to do is modify a local variable from outside the function that defines it.

The short answer is it can't be done. The long answer is it can, but only through very hackish means, such as passing the file through string substitution, making that function global, and then overwriting it.

Link to comment
Share on other sites

Yeah, I gathered the scope situation, but what I was hoping was that when the game was compiled from the list of luas that that it would build a master to use at any given point during the runtime.  I suppose looking at the console output this is not the case (it regularly calls any number of files).


The DoAgeWorld was a bad example.  In all actuality I'm looking at a way to redefine XP and death through the HandleDeathCleanup.


As it stands, it is a local function, and so is normally not even available without going through the gamelogic lua.  However I want to basically modify that function, and how it works.


If the only way to do it is a "hackish" way of doing it, then I will wait until there is better support for this sort of modding.  If that never happens, then it is the will of Klei (and I submit to said will) that it shall not be done.


Like I said - going into the file itself I can mimic exactly what I'm looking for.  But without an ability to override it via load order or anything like that, it sounds like I can only let it be and/or add to it.

Link to comment
Share on other sites


You don't need to modify the actual game files. If you put a gamelogic.lua inside a folder called scripts/ within your mod folder it will be loaded instead of the game's gamelogic.lua.

But since gamelogic.lua is a very important file, dealing with things like worldgen and save loading, overriding it doesn't sit right with me.

I did write a method being used in the No Delete on Death mod precisely to override HandleDeathCleanup. It's not as bad as overriding the entire file, but it's still quite hackish.

Link to comment
Share on other sites

While that method (putting it in the /scripts folder) would work - how would it stand for compatibility with other mods.


And I agree - I don't like the idea of overriding the file in such a manner.  If I were to, say, override the HandleDeathCleanup and that was the only piece in the gamelogic.lua would it simply override the file and lose all the other functions?  I can test it if you're not sure, but that is the way it sounds like it loads (rather than concatenating the files and using the most recent function declaration).


I know that Klei is planning on kind of separating things out.  I think that this is another example of something they should separate out.  All these "world gen" functions could be split into other files (each containing a separate portion of world gen) and then called back in with the gamelogic.lua - it would allow us to modify the individual files (such as HandleDeathCleanup) without touching the other files.


I still would rather have a concatenation and function overwrite allowing multiple mods to be used together and a "load order" to be established.  Thus allowing, say my version of HandleDeathCleanup not to interact with something changing the DoAgeWorld.  As my current understanding would suggest, one would work while the other would not (they would both try and override the file and only one would load).


No matter.  I'll do some playing around.  For now I've got client projects to do and will be away from the game for a bit.  I'll check back in a few days and see where things are.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

  • Create New...