MrKrab Posted September 24, 2015 Share Posted September 24, 2015 I've been working on a code and I'm having few issues with writing code for certain actions and events. I want to know:1. How to make my character lose more sanity when wet?2. How to make my character walk slow when it is raining?3. And how to make my character eat specific food items like wigfrid? Thank you! Link to comment https://forums.kleientertainment.com/forums/topic/58087-need-some-help-with-code/ Share on other sites More sharing options...
Doctor Donna Posted September 24, 2015 Share Posted September 24, 2015 I've been working on a code and I'm having few issues with writing code for certain actions and events. I want to know:1. How to make my character lose more sanity when wet?2. How to make my character walk slow when it is raining?3. And how to make my character eat specific food items like wigfrid? Thank you! I think for rain sanity drain you can look in WX-78 lua. And maybe this code for specific food (in character lua): local function cantEatMeatballs(inst) local eater = inst.components.eater local _pte = eater.PrefersToEat eater.PrefersToEat = function(self, food) if food ~= nil and food.prefab == "meatballs" then return end return _pte(self, food) endend that is what wigfrid had (in character lua) under local function master_init(inst) inst.components.eater:SetDiet({ FOODGROUP.OMNI }, { FOODTYPE.MEAT }) I hope I can help GreetingsMariewx78.luawathgrithr.lua Link to comment https://forums.kleientertainment.com/forums/topic/58087-need-some-help-with-code/#findComment-675642 Share on other sites More sharing options...
MrKrab Posted September 24, 2015 Author Share Posted September 24, 2015 that is what wigfrid had (in character lua) under local function master_init(inst) inst.components.eater:SetDiet({ FOODGROUP.OMNI }, { FOODTYPE.MEAT })Thank you so much! The only eat meat is working fine. but I can't find a way to drain sanity while wet. Plus I tried this to slow down the character during rain but it does not seem to work.local function updatespeed(inst) if TheWorld.state.israining then inst.components.locomotor.runspeed = 3 endendOne more thing.. Can you tell me how can I make a food item increase health when eaten? Thanks! Link to comment https://forums.kleientertainment.com/forums/topic/58087-need-some-help-with-code/#findComment-675646 Share on other sites More sharing options...
Doctor Donna Posted September 24, 2015 Share Posted September 24, 2015 (edited) Thank you so much! The only eat meat is working fine. but I can't find a way to drain sanity while wet. Plus I tried this to slow down the character during rain but it does not seem to work.local function updatespeed(inst) if TheWorld.state.israining then inst.components.locomotor.runspeed = 3 endendOne more thing.. Can you tell me how can I make a food item increase health when eaten? Thanks! completely new food item? mhm with sanity drain and wet.. I cant help I think. If I find something, I let you know. Edited September 24, 2015 by EulenMarie Link to comment https://forums.kleientertainment.com/forums/topic/58087-need-some-help-with-code/#findComment-675694 Share on other sites More sharing options...
DrSmugleaf Posted September 24, 2015 Share Posted September 24, 2015 Lose sanity when wet, from rosedisciple, in your character.lua:-- Wetness Debuff functionlocal function onmoisturedelta(inst) if inst.components.moisture:IsWet() then -- If the character is wet inst.components.sanity.dapperness = TUNING.DAPPERNESS_TINY*-1 -- Drain sanity at a rate of 1.33/minute else -- If the character is not wet inst.components.sanity.dapperness = 0 -- Reset the sanity drain to 0 endendIn your character.lua's master_postinit: inst:ListenForEvent("moisturedelta", onmoisturedelta)As for the character speed when raining, you need this in your character.lua's master_postinit: inst:WatchWorldState("daytime", function(inst) updatespeed(inst) end , TheWorld) inst:WatchWorldState("dusktime", function(inst) updatespeed(inst) end , TheWorld) inst:WatchWorldState("nighttime", function(inst) updatespeed(inst) end , TheWorld) updatespeed(inst) Link to comment https://forums.kleientertainment.com/forums/topic/58087-need-some-help-with-code/#findComment-675696 Share on other sites More sharing options...
MrKrab Posted September 24, 2015 Author Share Posted September 24, 2015 completely new food item?No I want to use the existing item. I want fish to give like 30 health points to my character when eaten. Link to comment https://forums.kleientertainment.com/forums/topic/58087-need-some-help-with-code/#findComment-675697 Share on other sites More sharing options...
DrSmugleaf Posted September 24, 2015 Share Posted September 24, 2015 (edited) No I want to use the existing item. I want fish to give like 30 health points to my character when eaten. In your character.lua:local function oneat(inst, food) if food and food.components.edible and food.prefab == "fish" then inst.components.health:DoDelta(30) endendIn your character.lua's master_postinit:inst.components.eater:SetOnEatFn(oneat) Edited September 24, 2015 by DrSmugleaf Link to comment https://forums.kleientertainment.com/forums/topic/58087-need-some-help-with-code/#findComment-675698 Share on other sites More sharing options...
MrKrab Posted September 24, 2015 Author Share Posted September 24, 2015 (edited) In your character.lua's master_postinit:?1inst:ListenForEvent("moisturedelta", onmoisturedelta)Hey I tried the code you gave. I am having a little problem. The mod crashes and it gives me error saying variable "onmoisturedelta" is not declared Thank you! Nevermind I figured it out. Thank you so much for you help. I really appreciate it. One last thing.. I don't quite really understand the code you gave me for speed. Can you please explain it? Thanks again! Edited September 24, 2015 by MrKrab Link to comment https://forums.kleientertainment.com/forums/topic/58087-need-some-help-with-code/#findComment-675710 Share on other sites More sharing options...
DrSmugleaf Posted September 24, 2015 Share Posted September 24, 2015 (edited) You changed the name of this function or didn't add it: Lose sanity when wet, from rosedisciple, in your character.lua:-- Wetness Debuff functionlocal function onmoisturedelta(inst) if inst.components.moisture:IsWet() then -- If the character is wet inst.components.sanity.dapperness = TUNING.DAPPERNESS_TINY*-1 -- Drain sanity at a rate of 1.33/minute else -- If the character is not wet inst.components.sanity.dapperness = 0 -- Reset the sanity drain to 0 endend Edited September 24, 2015 by DrSmugleaf Link to comment https://forums.kleientertainment.com/forums/topic/58087-need-some-help-with-code/#findComment-675711 Share on other sites More sharing options...
DrSmugleaf Posted September 24, 2015 Share Posted September 24, 2015 (edited) Hey I tried the code you gave. I am having a little problem. The mod crashes and it gives me error saying variable "onmoisturedelta" is not declared Thank you! Nevermind I figured it out. Thank you so much for you help. I really appreciate it. One last thing.. I don't quite really understand the code you gave me for speed. Can you please explain it? Thanks again!inst:WatchWorldState("daytime", function(inst) updatespeed(inst) end , TheWorld)inst:WatchWorldState("dusktime", function(inst) updatespeed(inst) end , TheWorld)inst:WatchWorldState("nighttime", function(inst) updatespeed(inst) end , TheWorld)updatespeed(inst)inst:WatchWorldState assigns a function to a world state, like daytime, dusktime, or nighttime.The function gets triggered when those world states are active, which means that it will get triggered at all times, since the same function is assigned to all 3 times of day.So in this case you are assigning a function which runs the updatespeed function to the world states "daytime", "dusktime" and "nighttime".As for "TheWorld" at the end, I don't know why it needs to be there, but it needs to be there, so good enough for me, maybe someone else can explain it.The updatespeed at the end just makes sure that the function gets executed when the server starts, as a just-in-case thing. I believe you can do like WX and only watch for when the world is raining, instead of watching for 3 different world states, but when people ask for a "function when its raining", they usually modify it later for other things like daytime, dusktime, and nighttime, so that's why it is like that. Sorry for the late response, didn't see your edit. Edited September 24, 2015 by DrSmugleaf Link to comment https://forums.kleientertainment.com/forums/topic/58087-need-some-help-with-code/#findComment-675731 Share on other sites More sharing options...
MrKrab Posted September 24, 2015 Author Share Posted September 24, 2015 1 2 3 4 inst:WatchWorldState("daytime", function(inst) updatespeed(inst) end , TheWorld) inst:WatchWorldState("dusktime", function(inst) updatespeed(inst) end , TheWorld) inst:WatchWorldState("nighttime", function(inst) updatespeed(inst) end , TheWorld) updatespeed(inst) inst:WatchWorldState assigns a function to a world state, like daytime, dusktime, or nighttime.The function gets triggered when those world states are active, which means that it will get triggered at all times, since the same function is assigned to all 3 times of day.So in this case you are assigning a function which runs the updatespeed function to the world states "daytime", "dusktime" and "nighttime".As for "TheWorld" at the end, I don't know why it needs to be there, but it needs to be there, so good enough for me, maybe someone else can explain it.The updatespeed at the end just makes sure that the function gets executed when the server starts, as a just-in-case thing. I believe you can do like WX and only watch for when the world is raining, instead of watching for 3 different world states, but when people ask for a "function when its raining", they usually modify it later for other things like daytime, dusktime, and nighttime, so that's why it is like that. Sorry for the late response, didn't see your edit.Thank you so much for your help! Link to comment https://forums.kleientertainment.com/forums/topic/58087-need-some-help-with-code/#findComment-675742 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