Jump to content

Recommended Posts

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 by SuperDavid
Link to comment
https://forums.kleientertainment.com/forums/topic/67728-need-help-with-keyhandler/
Share on other sites

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"

 

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 :)

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

 

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" :(

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