Jump to content

Recommended Posts

Hello! I need some help with manipulating creatures that already exist in-game.

For a character that I am creating, I need to add a tag to the birds' prefab and change the birdbrain file as well. I've seen some mods that change those things directly on modmain and I believe that's so it is easier to be compatible with other mods that change existing prefabs and brains.

I know how to change those things by recreating the prefab/brain file on my mod's scripts folder, but is there a way of doing it with "AddPrefabPostInit" and that kind of thing? I still don't know how those functions work. I don't know how to add a tag, to add a function to a creature or to actually replace part of the code.

 

For example. The birdbrain file contains this:

Spoiler

local function ShouldFlyAway(inst)
    return not (inst.sg:HasStateTag("sleeping") or
                inst.sg:HasStateTag("busy") or
                inst.sg:HasStateTag("flight"))
        and (TheWorld.state.isnight or
            (inst.components.health ~= nil and inst.components.health.takingfiredamage and not (inst.components.burnable and inst.components.burnable:IsBurning())) or
            FindEntity(inst, SEE_THREAT_DIST, nil, nil, { "notarget", "INLIMBO"}, { "player", "monster", "scarytoprey" }) ~= nil)
end

 

And I would like to add a tag to the birds' prefab -> "birdfriendly"

And so replace the ShouldFlyAway(inst) function from that to:

Spoiler

local function ShouldFlyAway(inst)
    return not (inst.sg:HasStateTag("sleeping") or
                inst.sg:HasStateTag("busy") or
                inst.sg:HasStateTag("flight"))
        and (TheWorld.state.isnight or
            (inst.components.health ~= nil and inst.components.health.takingfiredamage and not (inst.components.burnable and inst.components.burnable:IsBurning())) or
            FindEntity(inst, SEE_THREAT_DIST, nil, nil, { "notarget", "INLIMBO", "birdfriendly" }, { "player", "monster", "scarytoprey" }) ~= nil)
end

 

Is there a way of doing all of that on modmain or do I need to recreate the prefab+brain in my scripts folder?

 

Thanks in advance!

for brains you use

AddBrainPostInit(brain, fn)

for prefabs you do

AddPrefabPostInit(prefabname, fn)

 

However I'm not sure how modifying brains post init works exactly..

But for a list of all these PostInit methods that you can use, you can check scripts/modutil.lua

 

Bump

Thanks for the reply. I'm studying more the file you told me to check, Aquaterion. But if anyone can explain a bit more I'd appreciate it.

I still don't know how to replace, add or remove functions and components on modmain. I assume "AddPrefab" would ADD a new function to the prefab, but how do I change or remove an existing one? And how do I simply add a tag to a creature?

AddPrefabPostInit(prefabname, fn) would run the given function on that prefab every time 1 spawns so:
 

local function modifyBird(inst)
	inst:AddTag("birdfriendly")
	if not GLOBAL.TheWorld.ismastersim then--before this if = clientside stuff | after this if = serverside stuff
		return inst
	end
end

AddPrefabPostInit("robin", modifyBird)

every time a robin spawns, modifyBird will be called resulting in it having a new tag

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