Jump to content

Need help with multiple keyhandler actions!


Recommended Posts

Hello, I just found out I have a big problem :(! So I wanted to have multiple keyhandler actions so I had code like this before (in my character prefab)

local function OnKeyPressed(inst, data)
if data.inst == ThePlayer then
if data.key == KEY_LEFTBRACKET then
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 OnKeyPressed(inst, data)
if data.inst == ThePlayer then
if data.key == KEY_RIGHTBRACKET then
if TheWorld.ismastersim then
BufferedAction(inst, inst, ACTIONS.STEALTH):Do()
else
SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.STEALTH.code, inst, ACTIONS.STEALTH.mod_name)
         end
      end
   end
end

but then I realized that the top action gets ignored when there's two of them :(! So I tried doing something like this 

local function OnKeyPressed(inst, data)
if data.inst == ThePlayer then
if data.key == KEY_LEFTBRACKET then
if TheWorld.ismastersim then
BufferedAction(inst, inst, ACTIONS.SACRIFICE):Do()
else
SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.SACRIFICE.code, inst, ACTIONS.SACRIFICE.mod_name)
elseif data.key == KEY_RIGHTBRACKET then
if TheWorld.ismastersim then
BufferedAction(inst, inst, ACTIONS.STEALTH):Do()
else
SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.STEALTH.code, inst, ACTIONS.STEALTH.mod_name)
         end
      end
   end
end
end

but it just crashes the game instantly and doesn't give any errors, can someone please help because I really wanted to have multiple keyhandler actions for my character... It really would suck if I could only have one :(.... Any help would make me super happy :)!!!

Edited by SuperDavid
Link to comment
Share on other sites

Put them both in 1 function, something like this:

local function OnKeyPressed(inst, data)
  if data.inst == ThePlayer then
    if data.key == KEY_LEFTBRACKET then
        --code
    elseif data.key == KEY_RIGHTBRACKET then
        --other ocde
    end
  end
end

 

Link to comment
Share on other sites

8 hours ago, Aquaterion said:

Put them both in 1 function, something like this:


local function OnKeyPressed(inst, data)
  if data.inst == ThePlayer then
    if data.key == KEY_LEFTBRACKET then
        --code
    elseif data.key == KEY_RIGHTBRACKET then
        --other ocde
    end
  end
end

I did this

local function OnKeyPressed(inst, data)
  if data.inst == ThePlayer then
    if data.key == KEY_LEFTBRACKET and TheWorld.ismastersim then
    BufferedAction(inst, inst, ACTIONS.SACRIFICE):Do()
    else --If I remove this the game doesn't instantly crash
    SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.SACRIFICE.code, inst, ACTIONS.SACRIFICE.mod_name)
    elseif data.key == KEY_RIGHTBRACKET and TheWorld.ismastersim then
    BufferedAction(inst, inst, ACTIONS.STEALTH):Do()
    else --If I remove this the game doesn't instantly crash
    SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.STEALTH.code, inst, ACTIONS.STEALTH.mod_name)
    end
  end
end

but the game instantly crashes unless I remove those two "else" and when I remove them the actions can't be triggered :(... Thanks so much for your help Aquaterion :D!!!

Link to comment
Share on other sites

local function OnKeyPressed(inst, data)
  if data.inst == ThePlayer then
    if data.key == KEY_LEFTBRACKET then
      if TheWorld.ismastersim then
        BufferedAction(inst, inst, ACTIONS.SACRIFICE):Do()
      else
        SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.SACRIFICE.code, inst, ACTIONS.SACRIFICE.mod_name)
      end
    elseif data.key == KEY_RIGHTBRACKET then
      if TheWorld.ismastersim then
        BufferedAction(inst, inst, ACTIONS.STEALTH):Do()
      else
        SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.STEALTH.code, inst, ACTIONS.STEALTH.mod_name)
      end 
    end
  end
end

 

Edited by Aquaterion
Link to comment
Share on other sites

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
 Share

×
  • Create New...