Sign in to follow this  
thomas4846

need help in coding , random speach

Recommended Posts

thomas4846    818

recently i have been coding creature and i want to include speech to may the  mod much more immersive and just overall better. i would really appreciate any help thank you 

 

i want something like when pigs react to certain things such as eating becoming a companion and e.c

Edited by thomas4845
forgot something

Share this post


Link to post
Share on other sites
YakumoYukari    148

You may have to override pig's local functions. And If you want to make things look like more 'actively', you have to read the code more 'flexible'. It would differ on a case by case in every situation.


So.. Let me give an example.
You want to make pigmans react more flexible when an item is given by the player.
First, you have to override pigman's prefab function by AddPrefabPostInit in modmain.lua

AddPrefabPostInit("pigman", PigmanOverrideFn)

And Let's write PigmanOverrideFn from scratch. 

local function PigmanOverrideFn(inst)
	if not GLOBAL.TheWorld.ismastersim then -- if it running in client, end here.
        return inst
    end
	
	-- Do the thing
end

This is a template of overriding function in modmain. 


Note, that You must remain GLOBAL.TheWorld.ismastersim even if you have

local TheWorld = GLOBAL.TheWorld

this. Actually, it doesn't work at all on DST because TheWorld is not exist on loading constructing part(such as modmain scripts) so you have to access TheWorld via GLOBAL everytime.
 

Anyway, back to the point, there's 'trader' component in pigman which makes an entity accepts(or refuses) some items and do functions.
A function we exactly are looking for is set in the pigman prefab.

inst.components.trader.onaccept = OnGetItemFromPlayer

You can override that method to make what you want.
BUT Instead of rewrite everything of it, we can do a trick called 'Chain-Function' for the best compatibility with other mods.
 

 

What is that Chain-Function? Well, it's difficult to explain at first. Let's just dive into the practical one we're trying to.

 

local _onaccept = inst.components.trader.onaccept
local function OnAccept(inst, giver, item)
	_onaccept(inst, giver, item)
	
	if giver:HasTag("polite") then
		inst.components.talker:Say("Tank you friend. You is good")
	end
end

inst.components.trader.onaccept = OnAccept

This is the code implementing the case If giver has a tag named "polite"(Woodie's hidden perk) then pigman will appreciate with the special quote.

But what is "_onaccept" thing?
This is the previous code block(reference actually) of "onaccept" method. (Someone could express it like 'oldonaccept' or a name includes 'old') So If we get that previous one, Not only it will be preserved perfectly even with any modifications of the other mods, but also we can freely execute the old one where we want! 
Use this thing usefully. Could be hard to understand at first, but It's really strong, beautiful and brilliant trick for modders.


Compound it totally, It will be like this.

local function PigmanOverrideFn(inst)
	if not GLOBAL.TheWorld.ismastersim then -- if it running in client, end here.
        return inst
    end

	local _onaccept = inst.components.trader.onaccept
    local function OnAccept(inst, giver, item)
        _onaccept(inst, giver, item)

        if giver:HasTag("polite") then
            inst.components.talker:Say("Tank you friend. You is good")
        end
    end

    inst.components.trader.onaccept = OnAccept
end


AddPrefabPostInit("pigman", PigmanOverrideFn)

That's it. Modify, Mix it what you want.

 

Lastly, the point of being 'flexible' is,
You would never know what is exactly present in the game code in a certain prefab in a certain situation at first.
You merely are 'seeing' what is 'being seen' in the game which is... 'deductive'.
But by reading the code, understanding it and making it your own with some guides or examples,


Kind of Singularity will come to you that, how stupid I was and how to see things 'inductively'.
So If you want to make something, it is necessary to understand 'what it is' and 'what it was'.

 

Good Luck with your modding.

Edited by YakumoYukari

Share this post


Link to post
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
Sign in to follow this