. . . Posted May 29, 2016 Share Posted May 29, 2016 (edited) Hello, I need help with trying to get a function possible with keyhandler... Okay, so here's the code in modmain... -- Sacrifice health, hunger & sanity to gain your own flesh. local SACRIFICE = GLOBAL.Action() SACRIFICE.str = "Sacrifice" SACRIFICE.id = "SACRIFICE" SACRIFICE.fn = function(inst) inst.sg:GoToState("build_pre") -- This is :161: inst.AnimState:PlayAnimation("hit") inst.components.health:DoDelta(-50) inst.components.sanity:DoDelta(-37.5) inst.components.hunger:DoDelta(-37.5) inst.components.lootdropper:SpawnLootPrefab("humanmeat") inst.components.lootdropper:SpawnLootPrefab("monstermeat") end AddAction(SACRIFICE) And here's the code in my character prefab... local function OnKeyPressed(inst, data) if data.inst == ThePlayer then if data.key == KEY_Z then print("So it has come to this, huh...") if TheWorld.ismastersim then BufferedAction(inst, inst, ACTIONS.SACRIFICE):Do() else SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.SACRIFICE.code, inst, ACTIONS.SACRIFICE.mod_name) end end end end local function common_postinit(inst) inst:AddComponent("keyhandler") inst:ListenForEvent("keypressed", OnKeyPressed) end The one in the character prefab's working fine (I think) but the code in modmain keeps crashing and I don't know how to fix it... Can someone tell me the correct way to do the modmain code, please ? And thanks for reading... EDIT: Okay, so I played around with the code a little and did this -- Sacrifice health, hunger & sanity to gain your own flesh. local SACRIFICE = GLOBAL.Action() SACRIFICE.str = "Sacrifice" SACRIFICE.id = "SACRIFICE" SACRIFICE.fn = function(act) act.target.components.health:DoDelta(-999) act.target.components.sanity:DoDelta(-999) act.target.components.hunger:DoDelta(-999) act.target.components.lootdropper:SpawnLootPrefab("humanmeat") --This causes crash act.target.components.lootdropper:SpawnLootPrefab("monstermeat") --This causes crash end Only the dodeltas work this new way which's lame , so. i'll still wait for somebody's help . Edited May 29, 2016 by SuperDavid Link to comment https://forums.kleientertainment.com/forums/topic/67728-need-help-with-keyhandler/ Share on other sites More sharing options...
Mobbstar Posted May 29, 2016 Share Posted May 29, 2016 Did you check your log in "Documents/Klei/DoNotStarveTogether"? It usually contains further information about crashes. Link to comment https://forums.kleientertainment.com/forums/topic/67728-need-help-with-keyhandler/#findComment-777704 Share on other sites More sharing options...
. . . Posted May 29, 2016 Author Share Posted May 29, 2016 The crash's is "[string "../mods/Adam Character Mod/modmain.lua"]:161: attempt to index field 'sg' (a nil value)" Link to comment https://forums.kleientertainment.com/forums/topic/67728-need-help-with-keyhandler/#findComment-777705 Share on other sites More sharing options...
CarlZalph Posted May 29, 2016 Share Posted May 29, 2016 In your components initialization part are you also creating a stategraph? inst:SetStateGraph("SGwilson") Link to comment https://forums.kleientertainment.com/forums/topic/67728-need-help-with-keyhandler/#findComment-777724 Share on other sites More sharing options...
Aquaterion Posted May 29, 2016 Share Posted May 29, 2016 while not too related inst:AddComponent("keyhandler") inst:ListenForEvent("keypressed", OnKeyPressed) This should go in master_postinit not common_postinit It's probably crashing because characters don't have lootdropper component, I think you should be able to add it to your character, and make it drop nothing until this "sacrifice" Link to comment https://forums.kleientertainment.com/forums/topic/67728-need-help-with-keyhandler/#findComment-777730 Share on other sites More sharing options...
. . . Posted May 29, 2016 Author Share Posted May 29, 2016 Well, I fixed the modmain not working but I can't get some code to work the way I want it, here it is --Sacrifice health, hunger & sanity to gain your own meat. local SACRIFICE = GLOBAL.Action() SACRIFICE.str = "Sacrifice" SACRIFICE.id = "SACRIFICE" SACRIFICE.fn = function(act) local function healthdelta(act) if act.target.components.health.current <= 50 then act.target.components.talker:Say(GetString(inst, "ANNOUNCE_REFUSESACRIFICE")) elseif act.target.components.health.current >= 51 then act.target.sg:GoToState("hit") act.target.components.health:DoDelta(-25) act.target.components.sanity:DoDelta(-37.5) act.target.components.hunger:DoDelta(-37.5) act.target.components.lootdropper:SpawnLootPrefab("humanmeat") act.target.components.lootdropper:SpawnLootPrefab("monstermeat") act.target.components.talker:Say(GetString(inst, "ANNOUNCE_SACRIFICE")) end end end AddAction(SACRIFICE) If I remove the local function healthdelta everything works fine but I want to make that he doesn't kill himself, help would be appreciated ! Link to comment https://forums.kleientertainment.com/forums/topic/67728-need-help-with-keyhandler/#findComment-777808 Share on other sites More sharing options...
Aquaterion Posted May 29, 2016 Share Posted May 29, 2016 why exactly did u define a function inside the function? you have a function called healthdelta inside sacrifice.fn and you never really call it , I think it should something like this; local SACRIFICE = GLOBAL.Action() SACRIFICE.str = "Sacrifice" SACRIFICE.id = "SACRIFICE" SACRIFICE.fn = function(act) if act.target.components.health.current <= 50 then act.target.components.talker:Say(GetString(inst, "ANNOUNCE_REFUSESACRIFICE")) elseif act.target.components.health.current >= 51 then act.target.sg:GoToState("hit") act.target.components.health:DoDelta(-25) act.target.components.sanity:DoDelta(-37.5) act.target.components.hunger:DoDelta(-37.5) act.target.components.lootdropper:SpawnLootPrefab("humanmeat") act.target.components.lootdropper:SpawnLootPrefab("monstermeat") act.target.components.talker:Say(GetString(inst, "ANNOUNCE_SACRIFICE")) end end Link to comment https://forums.kleientertainment.com/forums/topic/67728-need-help-with-keyhandler/#findComment-777812 Share on other sites More sharing options...
. . . Posted May 29, 2016 Author Share Posted May 29, 2016 7 minutes ago, Aquaterion said: why exactly did u define a function inside the function? you have a function called healthdelta inside sacrifice.fn and you never really call it , I think it should something like this; local SACRIFICE = GLOBAL.Action() SACRIFICE.str = "Sacrifice" SACRIFICE.id = "SACRIFICE" SACRIFICE.fn = function(act) if act.target.components.health.current <= 50 then act.target.components.talker:Say(GetString(inst, "ANNOUNCE_REFUSESACRIFICE")) elseif act.target.components.health.current >= 51 then act.target.sg:GoToState("hit") act.target.components.health:DoDelta(-25) act.target.components.sanity:DoDelta(-37.5) act.target.components.hunger:DoDelta(-37.5) act.target.components.lootdropper:SpawnLootPrefab("humanmeat") act.target.components.lootdropper:SpawnLootPrefab("monstermeat") act.target.components.talker:Say(GetString(inst, "ANNOUNCE_SACRIFICE")) end end I did this and it crashed saying ":161: attempt to compare nil with number" Link to comment https://forums.kleientertainment.com/forums/topic/67728-need-help-with-keyhandler/#findComment-777815 Share on other sites More sharing options...
Aquaterion Posted May 29, 2016 Share Posted May 29, 2016 for health its health.currenthealth not health.current act.target.components.health.currenthealth Link to comment https://forums.kleientertainment.com/forums/topic/67728-need-help-with-keyhandler/#findComment-777816 Share on other sites More sharing options...
. . . Posted May 29, 2016 Author Share Posted May 29, 2016 5 minutes ago, Aquaterion said: for health its health.currenthealth not health.current act.target.components.health.currenthealth It works fine now, thank you ! Link to comment https://forums.kleientertainment.com/forums/topic/67728-need-help-with-keyhandler/#findComment-777818 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