Jump to content

Recommended Posts

Hey-hey!

It's-a-me again. Soo, I feel like I've set the programming bar a bit high for myself about my next character.

I'm looking for references I could use to puzzle together some codings for my survivor. Here are the perks I have in mind for my gal:

1) I would like to add a consumables that is restricted to her. It should set her max stats randomly between 75 and 250 whenever she eats this food.

2) A crafting material that gets removed from any player's inventory if they get hit while they carry it (every carried stack, not just a single piece).

3) A food which is, when consumed, either does nothing or doubles the current Health of the survivor (50%-50%).

They're not extremely complicated, I just don't exactly know how to approach them, as there are no items that do similar stuff in the game.

Thanks in advance for everyone! You're cape-less heroes!

Cheerio!

1)

 

Spoiler

By changing the CanEat function, only your character can eat this food.


--modmain
local function OnlyEatenByMe(eater)
	local old_CanEat = eater.CanEat
	eater.CanEat = function(eater, food_inst,...)
		if eater.inst and eater.inst.prefab == "mycharacterprefab" and food_inst.prefab == "myconsumableprefab" then
			return false
		end
		return unpack{old_CanEat(eater, food_inst,...)}
	end
end

AddComponentPostInit("eater",OnlyEatenByMe)

For the special effect, here is the code:


--character prefab 

local function OnEat(inst,food)
	if inst and food and food.prefab = "myconsumableprefab" then
		local stats = {
			{"health","maxhealth"},
			{"sanity","max"},
			{"hunger","max"},
		}
		for k,v in ipairs(stats) do
			local stat = math.random(75,250)
			if inst.components[v[1]] then
				local old_percent = inst.components[v[1]]:GetPercent() -- if you want to save the percents of the previous value and apply it
				inst.components[v[1]][v[2]] = stat
				inst.components[v[1]]:SetPercent(old_percent)
			end
		end
	end
end

--master postinit

inst.components.eater:SetOnEatFn(OnEat)

2)

Spoiler

Adding this into the modmain will listen for the attacked event and remove the first stackof the item found.


local function RemoveItem(inst)
	if inst and inst.components.inventory then
		local item = inst.components.inventory:FindItem(function(item) return item.prefab == "youritem" end)
		if item then
			item:Remove()
		end
	end
end

AddPlayerPostInit(function(inst)
	if not GLOBAL.TheWorld.ismastersim then
		return inst
	end
	inst:ListenForEvent("attacked",RemoveItem)
end)

 

3)

Spoiler

This will double the health of the character that is eating this with a chance of 50%


--food item prefab

local function DoubleOrNothing(inst,eater)
	if eater then
		local success = math.random() < 0.5
		if success then
			if eater.components.health then
				local old_percent = eater.components.health:GetPercent()
				local old_max = eater.components.health.maxhealth
				eater.components.health:SetMaxHealth(old_max * 2)
				eater.components.health:SetPercent(old_percent)
			end
		end
	end
end

--in the fn() of the food item
inst.components.edible:SetOnEatFn(DoubleOrNothing)

 

The functions should work like this, if something is unclear or you need more help let me know! :)

Code is not tested, so there could be errors in there :D

Edited by Monti18
  • Like 1
  • Thanks 1

@Monti18 I'm still searching my jaw on the floor :love_heart:

How did you got such practice in coding?

A tiny question about 2)'s solution: I haven't tested it yet either, but could this coding be cheesed if the player distributes the tokens into small stacks in their inventory? I'll test it tomorrow, as I'm not at my modding PC at the moment.

Anyway, huge thanks again!

 

@C_Thun Haha a lot of trial and error (mostly errors) :D

It's really mostly trying to get different things done ;)

I missunderstood how this item should work, I though it should remove just one stack of this item.

Then it's best to do it like @decduck3 said:

local function RemoveItem(inst)
	if inst and inst.components.inventory then
		local item = inst.components.inventory:FindItems(function(item) return item.prefab == "youritem" end)
		if item then
      		for k,v in ipairs(items) do
				v:Remove()
        	end
		end
	end
end

AddPlayerPostInit(function(inst)
	if not GLOBAL.TheWorld.ismastersim then
		return inst
	end
	inst:ListenForEvent("attacked",RemoveItem)
end)

 

  • Like 2

@Monti18 Uhh, I've got a crash when my character got damaged.

It's something about the token removal, but I wasn't able to sort out what's wrong:

The crash log refers to the red line here, stating that "bad argument #1 to 'ipairs' (table expected, got nil)"

Spoiler

local function RemoveItem(inst)
    if inst and inst.components.inventory then
        local item = inst.components.inventory:FindItems(function(item) return item.prefab == "lucktoken" end)
        if item then
              for k,v in ipairs(items) do
                v:Remove()
            end
        end
    end
end

I've attached the whole server log, in case it's needed.

server_log.txt

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