Jump to content

Recommended Posts

Sorry to be a bother for something that is likely very simple, but I am having difficulty finding an example of what I want to do and I don't want to use the dirty, whole-file replacement method, so as to remain compatible with later updates. I did try the search box for "prefab function" but couldn't find what I needed.

 

For example, in the prefab file smallbird.lua, I would like to rewrite the functions FollowPlayer(inst),  OnGetItemFromPlayer(inst, giver, item) and SpawnAdult(inst).

 

I've managed to make changes previously to the smallbird brains, based on sample files and someone's "Pig Manners" mod (thanks for that! :grin:) but that method of searching nodes in the brain doesn't really seem to apply here.

 

Can someone tell me if it's a simple affair to directly overwrite a function within a prefab, or do I have to resort to some kind of witchcraft to get it done? :-)

 

It would be awesome if anyone could provide any kind of example for me. I'm also interested to know how I call things like "peep" on the birds; I can see peepchance in the lua file but no method for actually making them peep (nor in the brain). Any assistance would be most appreciated!

@outseeker, Unfortunately, for local functions like that, you can't overwrite them directly. You can, however, look at where they get attached to other tables, and replace those. In the case of FollowPlayer, it looks like it's getting attached to inst.userfunctions:

    inst.userfunctions =     {        FollowPlayer = FollowPlayer,        GetPeepChance = GetPeepChance,        SpawnTeen = SpawnTeen,        SpawnAdult = SpawnAdult,    }

So what you want to do to override that would be something like this:

local prefabs = {"smallbird", "teenbird"}for k,v in pairs(prefabs) do    AddPrefabPostInit(v, function(inst)        local OldFollowPlayer = inst.userfunctions.FollowPlayer        inst.userfunctions.FollowPlayer = function(inst)            --whatever you want to change it to            --if necessary, you can have it do the old thing by calling OldFollowPlayer(inst)        end    end)end

You can use a similar approach for the others-- it looks like OnGetItemFromPlayer is being attached to the trader component (inst.components.trader.onaccept), and SpawnAdult is also in the userfunctions. It looks like the userfunctions are called from the stategraph (SGsmallbird).

 

Edit: By the way, it makes me really happy when someone realizes that replacing files wholesale is not a good approach, compatibility-wise. So kudos to you for wanting to take a less invasive approach! You can use this sort of "store old function, replace with a new one that might call the old" as a way to do some much more compatible modding than much of what's on the workshop.

Edited by rezecib

@rezecib Thanks heaps mate! To be honest, I'm not experienced enough with LUA modding to really understand the difference between replacing the function and doing what you've illustrated here- to me they're effectively the same thing and I really appreciate the help! :grin:

 

Do I understand correctly that we are simultaneously looping through both smallbird and teenbird prefabs with this code and adding a prefabpostinit for every "pair? I don't fully understand that part of the code, can you indulge me further? Why are we using a for loop at all? Can't we just do the addprefabpostinit function portion of the code once, since we address followplayer directly via userfunctions?

 

Thanks again

 

*edit* Appreciate the kudos! I figure if I manage to create something, it will be nice and clean and as non-invasive as possible so it will always work perfectly and others can look at it for functionalities they want and good methods to use, like I do when learning myself hehe

 

*edit2* actually there is no prefab file called teenbird, do I not understand this part also? XD

Edited by outseeker

hmm yeah I really am missing something here.. When I adjust the code for SpawnAdult and replace "whatever you want to change it to" with the whole function as-is from the smallbird.lua, it crashes saying attempt to call global 'SpawnPrefab' (a nil value), being the first line of the original function. I don't get it XD

 

w00t nvm, GLOBAL.SpawnPrefab seems to get the job done :D Thanks a million!

Edited by outseeker

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