Jump to content

Impossible to Wake Up


Recommended Posts

So basically, I have two buttons that make a player using a different stategraph sleep and wake up.

local function OnKeySleep(inst, data)    if data.inst == ThePlayer then        if data.key == KEY_O then             if TheWorld.ismastersim then                BufferedAction(inst, inst, ACTIONS.SLEEP):Do()                -- Since we are the server, do the action on the server.            else                SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.SLEEP.code, inst, ACTIONS.SLEEP.mod_name)            end        end    endendlocal function OnKeyWakeup(inst, data)    if data.inst == ThePlayer then        if data.key == KEY_P then             if TheWorld.ismastersim then                BufferedAction(inst, inst, ACTIONS.WAKEUP):Do()                -- Since we are the server, do the action on the server.            else                SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.WAKEUP.code, inst, ACTIONS.WAKEUP.mod_name)            end        end    endend

And this is in modmain:

local SLEEP = GLOBAL.Action()SLEEP.str = "Sleep"SLEEP.id = "SLEEP"SLEEP.fn = function(act)	if not act.target:HasTag("busy") or act.target:HasTag("leif") then	act.target.sg:GoToState("sleep")	act.target:AddComponent("healthRegen")	else	if act.target:HasTag("leif") then	act.target.sg:GoToState("sleeping")	act.target:AddComponent("healthRegen")endendendAddAction(SLEEP) local WAKEUP = GLOBAL.Action()WAKEUP.str = "Wakeup"WAKEUP.id = "WAKEUP"WAKEUP.fn = function(act)	act.target.sg:GoToState("wake")	act.target:RemoveComponent("healthRegen")endAddAction(WAKEUP)

Now, if I'm the hoster, I can go to sleep and wake up with no issue.  However, if I'm a client, I can't wake up, only go to sleep.  How can I fix this?

Link to comment
Share on other sites

@Mario384,

thats not how u use RPCs. u have to make a function that u want to call on the server, and add a handler, and then call that handler from the client. for your code, this would probably look like this:

in your modmain:

local function wakeup(player)GLOBAL.BufferedAction(player, player, GLOBAL.ACTIONS.WAKEUP):Do()endAddModRPCHandler(modname, "Wakeup", wakeup)
the RPC call:

SendModRPCToServer(MOD_RPC[modname]["Wakeup"])
and dont change "modname", thats on purpose. also, this rpc works even if u are host, so u can remove that TheWorld.ismastersim part and just use the RPC.

and look here for a full working example of a RPC: http://forums.kleientertainment.com/topic/54536-help-with-a-mod-spider-summoning/

Edited by Seiai
Link to comment
Share on other sites

@Mario384, this is odd.

 

Put a

print("Hi, I'm in wakeup!")

inside the wakeup function and

print("I'm next to the SendRPC")

Confirm the RPCs are being sent and received appropriately.

 

It appears the RPC is sent, but the function isn't being run.  Any idea?

 

Link to comment
Share on other sites

@Mario384, the DoWidgetButtonAction, while handled, checks

if playercontroller ~= nil and playercontroller:IsEnabled() and not player.sg:HasStateTag("busy") then

So when playing as the host, you bypass these checks because you run the action wakeup directly.

When being a client, you send the rpc when you are in the sleeping state (which has a busy state tag), so wakeup doesn't get processed.

 

So you will want to take Seiai's approach:

- in modmain

local function wakeup(player)	GLOBAL.BufferedAction(player, player, GLOBAL.ACTIONS.WAKEUP):Do()endAddModRPCHandler("leifworkaround", "Wakeup", wakeup)

- replacing the DoWidgetButtonAction

SendModRPCToServer(MOD_RPC["leifworkaround"]["Wakeup"])
Link to comment
Share on other sites

@Mario384, "leifworkaround" is just a name.

 

Only thing you need is to include

local function wakeup(player)    GLOBAL.BufferedAction(player, player, GLOBAL.ACTIONS.WAKEUP):Do()end AddModRPCHandler("leifworkaround", "Wakeup", wakeup)

in modmain, as it is,

and

SendModRPCToServer(MOD_RPC["leifworkaround"]["Wakeup"])

replacing your

SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.WAKEUP.code, inst, ACTIONS.WAKEUP.mod_name)
Link to comment
Share on other sites

@Seiai, well, yes, they are legal.

 

He has a keyhandler component in the common_postinit, so when clients press a key, events trigger client side, triggering functions like OnKeySleep, which send a RPC already defined, with the parameters it requires to function, that you can see in networkclientrpc.lua.

 

Mario made other actions trigger the same way, but I saw that they mostly triggered coming from an idle state, so no busy tag was there, so they passed the checks of the RPC he used.

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