Jump to content

Mod to prevent player from accidentally eating monster meat


Mrgiovanny

Recommended Posts

I am very new at modding and trying to search through all the lua files to find and combine something to make the player unable to eat certain items. For example monster meat or rot. During a play, I never eat these items, however, I do accidentally eat them quite a lot (miss clicking when cooking at fire for example). Therefore I would like to develop a mod to prevent eating these and maybe other components as a quality of life adjustment. I can remove the edible part from an item. Unfortunately, this also prevents a pig from eating monster meat --> exacltly what I do not want. 

Anyone who can help me in this venture?

Link to comment
Share on other sites

I would suggest creating a custom food group and then throwing the edible food items of your choice in there.
After that you would need to add that custom food group to all creatures (like pigmen) which you would want to still be able to eat them.

I have created a tutorial in which I explain how to do that. (You would just need to skip the part where you would normally add the custom food group to your character, and it should work. :) )

Here's the tutorial.

Link to comment
Share on other sites

On 21-2-2019 at 12:34 AM, BraveChicken said:

I would suggest creating a custom food group and then throwing the edible food items of your choice in there.

I could do this, although it looks pretty tough to do that to every creature. I might still have the same problem that my pigmen won't change in to werepigs though. I still don't understand the code. What I made in my modmain.lua is the following

Result of this code: I cannot eat the monster meat anymore. Pigmen can still eat it, but won't transform into werepigs anymore. I suspect it is because the "inst.components.werebeast" part. But I don't know enough of the coding to understand what this does. Any idea?

 

AddPrefabPostInit("monstermeat", function(inst)
inst.components.edible.foodtype = "HORRIBLE" --make food horrible (in this case monstermeat) example pigskin is horrible
end)

AddPrefabPostInit("pigman", function(inst)
local function OnEat_new(inst, food)
    if food.components.edible
       and food.components.edible.foodtype == "HORRIBLE"
       --and inst.components.werebeast
       and not inst.components.werebeast:IsInWereState() then
        if food.components.edible:GetHealth() < 0 then
            inst.components.werebeast:TriggerDelta(1)
        end
    end
    
    if food.components.edible and food.components.edible.foodtype == "VEGGIE" then
        local poo = SpawnPrefab("poop")
        poo.Transform:SetPosition(inst.Transform:GetWorldPosition())        
    end
    
end
end)

Link to comment
Share on other sites

13 hours ago, Mrgiovanny said:

I could do this, although it looks pretty tough to do that to every creature. I might still have the same problem that my pigmen won't change in to werepigs though. I still don't understand the code. What I made in my modmain.lua is the following

Result of this code: I cannot eat the monster meat anymore. Pigmen can still eat it, but won't transform into werepigs anymore. I suspect it is because the "inst.components.werebeast" part. But I don't know enough of the coding to understand what this does. Any idea?

Ah! I see. I looked up into it and here's how to simply tweak it a little bit:

 

    AddPrefabPostInit("pigman", function(inst)
            inst.components.eater.ablefoods = { "HORRIBLE", "MEAT", "VEGGIE", "INSECT", "SEEDS", "GENERIC" }
            inst.components.eater.foodprefs = { "HORRIBLE", "MEAT", "VEGGIE", "INSECT", "SEEDS", "GENERIC" }
            
            local function OnEat(inst, food)
                if food.components.edible
                   and (food.components.edible.foodtype == "MEAT" or food.components.edible.foodtype == "HORRIBLE")
                   and inst.components.werebeast
                   and not inst.components.werebeast:IsInWereState() then
                    if food.components.edible:GetHealth() < 0 then
                        inst.components.werebeast:TriggerDelta(1)
                    end
                end
                
                if food.components.edible and food.components.edible.foodtype == "VEGGIE" then
                    local poo = SpawnPrefab("poop")
                    poo.Transform:SetPosition(inst.Transform:GetWorldPosition())        
                end                
            end
            inst.components.eater:SetOnEatFn(OnEat)                
    end)


And of course, you don't need to add the custom food type to every single creature in the game, as only few of them can actually eat meat. (So it would be more just like pigmen, spiders, hounds, and such :p )

I hope that helps.
Let me know if you'll have any more issues with this. :) 

Link to comment
Share on other sites

I would propose the much simpler method of extending the CanEat function of any player's eater-component, and just deny the eating of particular prefabs.

local inedibles = { "monstermeat", "cookedmonstermeat", "monstermeat_dried", "spoiled_food" }

AddPlayerPostInit(function(inst)
	if inst.components.eater then
		local old_CanEat = inst.components.eater.CanEat
		
		inst.components.eater.CanEat = function(inst)
			if inst.prefab in inedibles then
				return false
			end
			return old_CanEat(inst)
		end
	end
end)

 

Is there some problem I'm missing here?

Link to comment
Share on other sites

3 hours ago, Ultroman said:

I would propose the much simpler method of extending the CanEat function of any player's eater-component, and just deny the eating of particular prefabs.

That's an interesting approach. I haven't tried such method before. o:
I'll keep that one in mind :p 

However, when I tried testing it, it crashed my game. (Saying: 'then' expected near 'in') 

Link to comment
Share on other sites

Yeah, sorry. I was writing half Python half LUA :p

local inedibles = { "monstermeat", "cookedmonstermeat", "monstermeat_dried", "spoiled_food" }

AddPlayerPostInit(function(inst)
	if inst.components.eater then
		local old_CanEat = inst.components.eater.CanEat
		
		inst.components.eater.CanEat = function(inst)
			for i, v in ipairs(inedibles) do
				if inst.prefab == v then
					return false
				end
			end
			return old_CanEat(inst)
		end
	end
end)

 

Link to comment
Share on other sites

19 hours ago, Ultroman said:

Yeah, sorry. I was writing half Python half LUA :p

Hmm... this still doesn't want to work for me. o.o

This time the games works fine, but this blocks my character from eating anything at all for some reason, and not only the selected foods.
(I would play around with it myself but I haven't really used this type of method too much before to know exactly how it works)

Link to comment
Share on other sites

11 minutes ago, Ultroman said:

send it to me

The whole file? XD or the whole mod? (I usually use a testing files for that, so I have a lot of script in there. I can still send it to you in a privet message if you wish.) (I really feel kinda bad for spamming this post XD)
As for the script, it's same as you placed it. 
(Even though I think there was a typo so I tried it as "ipairs" and "pairs" just to be sure. XD

Link to comment
Share on other sites

Ah, I think I know what the problem is. It might be the ambiguous variable "inst". It refers to the player being changed, instead of the food, so it's asking whether the character can eat itself, to which the answer is luckily "No" :) Try this instead.

	local inedibles = { "monstermeat", "corn" }

	AddPlayerPostInit(function(inst)
		if inst.components.eater then
			local old_CanEat = inst.components.eater.CanEat
			
			inst.components.eater.CanEat = function(food_inst)
				for i, v in ipairs(inedibles) do
					if food_inst.prefab == v then
						return false
					end
				end
				return old_CanEat(food_inst)
			end
		end
	end)

 

Link to comment
Share on other sites

6 minutes ago, Ultroman said:

:) Try this instead.

Still can't eat anything XD lol I'm so sorry! XD

I'll just take it as I'm obviously missing something, and your code is probably perfectly fine. XD And I'll just stop bothering you. XD
Thank you anyways! :p 
 

Link to comment
Share on other sites

What? No? That leaves me here with no idea what's going on xD It's a challenge for me, and I want to help you fix it. Let me test something.

I had forgotten to add "self" to the function. Since it's declared with : instead of . it needs to be handed the "self".

	local inedibles = { "monstermeat", "corn" }

	AddPlayerPostInit(function(inst)
		if inst.components.eater then
			local old_CanEat = inst.components.eater.CanEat
			
			inst.components.eater.CanEat = function(self, food_inst)
				for i, v in ipairs(inedibles) do
					if food_inst.prefab == v then
						return false
					end
				end
				return old_CanEat(self, food_inst)
			end
		end
	end)

 

It works now.

But can I ask why you're overwriting game prefabs and brains? Unless there's an insanely good reason, that's incredibly bad practice when modding. It makes your mod extremely incompatible with other mods.

Link to comment
Share on other sites

Yes it does work now! XD Thank you. and sorry... XD

45 minutes ago, Ultroman said:

But can I ask why you're overwriting game prefabs and brains? Unless there's an insanely good reason, that's incredibly bad practice when modding. It makes your mod extremely incompatible with other mods.

All of those files are just a way I tend to learn and test things XD (I don't really belong to people which tend to ask for help, so I usually try to figure things out on my own. That's also why I have a whole separate mod which I use only to test things out) 

I wanted to create a brain for one of my custom creatures which I made for one of my characters, and to try and learn how some things work, I usually copy the files and try to play around there to see what does what XD And (back then) while still having 0 knowledge of how to script for this game, I somehow managed to create a custom brain for my creature. (Made me really happy to see it work. XD) So yea, don't mind all of those messy files there XD

Link to comment
Share on other sites

Ah, that's smart, actually :) Just asking out of concern ;)

Learning by yourself is really the best, but don't hold back if you're totally lost on something, or if you want to bounce a solution off of us, to hear if it's feasible or if there might be a better way. That's what we're here for :D

Link to comment
Share on other sites

Archived

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