Jump to content

Character-specific prefab interractions


Recommended Posts

I've been looking around, but haven't seen anything regarding this.

Specifically, is there a way to change the effects of flowers, garlands and sanity-giving clothing on a single character, without affecting other characters' interractions with those things?

 

Specifically what I'd like to do:

 - Reverse the pick effect of flowers

 - Reverse the passive gain of garlands

 - Cut down the effectiveness of other sanity-giving clothes by 75%.

 

I at least have a general idea of how the last one should look, but I can't seem to find an appropriate component.

Link to comment
Share on other sites

You can't make them nicely unless the prefab allows you to hook up in a clean way.

Like when it checks for a function to run, or it pushes an event.

 

When you have to workaround code for particular effects, you have to be creative and evaluate the impacts you may have in other mods. I can make a function that completely overrides what happens when you pick a flower, but if all mods did it, then we would have the onpickedfn of the last mod loaded and your character wouldn't benefit.

 

We can complement with code, or we can overwrite with it.

 

Also, I can take the listen to the pickup event approach from my character: that would take away editing the flower. But then I would have unnecessary checks seeing if the axe I picked up was a flower or not.

 

Anyways, here is the code:

local mycharacter = "wilson"local TUNING = GLOBAL.TUNING-- Here I override the onpickedfn function that runs on the pickable component-- when the flower is picked, instead of overwriting the positive effect-- I cause a negative double effect so we appear to have only a negative oneAddPrefabPostInit("flower", function(inst)	local old = inst.components.pickable.onpickedfn	inst.components.pickable.onpickedfn = function(inst, picker)		old(inst, picker)		if picker and picker.components.sanity and picker.prefab == mycharacter then			picker.components.sanity:DoDelta(-2 * TUNING.SANITY_TINY)		end	endend)-- When I equip an equippable, an event gets pushed on it-- That sends the data table with the owner and the slot it was equipped-- Here, when I equip the hat on my character, I adjust the dapperness-- I restore it when I unequip it so other characters don't get the same penaltyAddPrefabPostInit("flowerhat", function(inst)	inst:ListenForEvent("equipped", function(inst, data)		if data.owner.prefab == mycharacter then			inst.components.equippable.dapperness = -TUNING.DAPPERNESS_TINY		end	end)	inst:ListenForEvent("unequipped", function(inst, data)		if data.owner.prefab == mycharacter then			inst.components.equippable.dapperness = TUNING.DAPPERNESS_TINY		end	end)end)-- Here, we have to edit the dapperness of everything, so we go to the base component-- We edit the function that returns the dapperness of the item-- If an item helps us, we tackle its dappernessAddComponentPostInit("equippable", function(self)	local old = self.GetDapperness	self.GetDapperness = function(self, owner)		local old_dp = old(self, owner)		if (old_dp > 0) and owner and owner.prefab == mycharacter then			return old_dp * 0.25		end		return old_dp	endend)
Link to comment
Share on other sites

First, thanks for your help, this is one of the aspects of this character which I knew I'd have a problem with from the start. It's my first mod for the game, so this bit was way over my head...mainly because, while I had most of the parts, I had absolutely no idea how to put them together. To be frank, there're a lot of different guides and different ways of documenting out there which conflict with one-another, so my methods were turning into a confusing mess.
The other big thing was that I had no idea that you could specify original things like 'mycharacter'; I thought something like that would either be ignored, or that doing so would result in an error, and that I had to solidly work within pre-specified elements of the game. This is really useful to know.

 

I only have a few questions regarding this:
 - If I'm not mistaken, this changes the garland for all characters until my character takes hers off. It shouldn't be too much of an inconvenience considering that other characters don't normally use them due to having better alternatives, but wouldn't it be possible to in some way use an ownership tag like the one in the segment that adjusts dapperness of all clothing, or am I mistaken as to what that does?

 - Does it matter that the garland is before or after the universal clothing dapperness adjustment?

 - On the dapperness adjustment, does > 0 actually have to be specified, seeing as normal clothing would result in multiplying by 0, leaving its value at 0?
 - Lastly...where is this series meant to be placed? I can't for the life of me seem to put it in a place that does anything, other than crashing.

Edited by Pyr0mrcow
Link to comment
Share on other sites

@Pyr0mrcow,

 

1) AddPrefabPostInit is code that runs on a prefab after its initialized.

So, all garlands would have those listeners, but only the one who gets the event pushed would change.

That is, the garland equipped by your character.

Prefabs don't share components, when you do inst:AddComponent, you create a copy of the base class and attach it to the entity, so the equippable of garland1 is not the equippable of garland2.

 

2) In this case, it doesn't matter, since the functions are PostInits.

What would happen always is that you get the equippable component attached to the garland, then the garland is initialized.

So the postinit of equippable would run first, then the postinit of the prefab.

 

3) I specify > 0 so as to have items that reduce your sanity (e.g, the new garland, the spiderhat), not have their dapperness cut down by 75%. Unless you want it that way, of course, that would be a positive perk.

 

4) You copy and paste everything as it is, in modmain.lua.

Link to comment
Share on other sites

Aah, for some reason I thought all copies of a single object were the same object displaying in multiple places. I didn't realize that multiple instances of an object were so seperate.

And...yea, I was thinking of it as a seperate componant rather than a reverse of it when I asked the dapperness thing, not sure why, but that seems obvious now. Whoops.

 

Long story short, I was overcomplicating the heck out of trying to place and call in those aspects, and that's one of the few places I didn't try because of that. I haven't had much reason to look into lua in the past, but this is a good reason to start.

It all seems to be working perfectly now! This should be exactly what she needs, the garland and flower interactions in particular.

Here's hoping nothing else trips me up before I can at least finish the basic functionality. Thanks for all your help, and for answering my curiosity!

Edited by Pyr0mrcow
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
 Share

×
  • Create New...