Goregonzola Posted September 9, 2021 Share Posted September 9, 2021 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! Link to comment https://forums.kleientertainment.com/forums/topic/133460-once-again-seeking-for-a-helping-hand-for-survivor-abilities/ Share on other sites More sharing options...
Monti18 Posted September 9, 2021 Share Posted September 9, 2021 (edited) 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 Edited September 9, 2021 by Monti18 1 1 Link to comment https://forums.kleientertainment.com/forums/topic/133460-once-again-seeking-for-a-helping-hand-for-survivor-abilities/#findComment-1493108 Share on other sites More sharing options...
Goregonzola Posted September 9, 2021 Author Share Posted September 9, 2021 @Monti18 I'm still searching my jaw on the floor 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! Link to comment https://forums.kleientertainment.com/forums/topic/133460-once-again-seeking-for-a-helping-hand-for-survivor-abilities/#findComment-1493144 Share on other sites More sharing options...
DecDuck Posted September 10, 2021 Share Posted September 10, 2021 (edited) You might want to use inventory:FindItems() instead, then loop through the results. That should remove all stacks. Edited September 10, 2021 by decduck3 This is lua, not c# 2 Link to comment https://forums.kleientertainment.com/forums/topic/133460-once-again-seeking-for-a-helping-hand-for-survivor-abilities/#findComment-1493282 Share on other sites More sharing options...
Monti18 Posted September 10, 2021 Share Posted September 10, 2021 @C_Thun Haha a lot of trial and error (mostly errors) 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) 2 Link to comment https://forums.kleientertainment.com/forums/topic/133460-once-again-seeking-for-a-helping-hand-for-survivor-abilities/#findComment-1493379 Share on other sites More sharing options...
Goregonzola Posted September 11, 2021 Author Share Posted September 11, 2021 @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 Link to comment https://forums.kleientertainment.com/forums/topic/133460-once-again-seeking-for-a-helping-hand-for-survivor-abilities/#findComment-1493875 Share on other sites More sharing options...
Monti18 Posted September 11, 2021 Share Posted September 11, 2021 @C_Thun Oh you're right, I wrote items instead of item. As items is nil as it isn't defined, it crashes. Just change it to item and it should work 2 Link to comment https://forums.kleientertainment.com/forums/topic/133460-once-again-seeking-for-a-helping-hand-for-survivor-abilities/#findComment-1493878 Share on other sites More sharing options...
Goregonzola Posted September 11, 2021 Author Share Posted September 11, 2021 Ohh, I'm blushing that I haven't been able to notice it. Thanksies 1 Link to comment https://forums.kleientertainment.com/forums/topic/133460-once-again-seeking-for-a-helping-hand-for-survivor-abilities/#findComment-1493990 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now