Jump to content

Recommended Posts

I tried to make a mod that check which action the player is doing, then if is picking up grass or twigs do an action, but my mod is supposed to run in client.
I also tried to check the player action via GetBufferedAction() that worked in a no caves/singleplayer world but not in a caves/multiplayer world,
also tried GetDebugString(), it's print the player info in multiplayer world, except the action player is doing at the moment

my idea are check for the tag "doing" then wait for the item twig/grass, to appear in the inventory, but this feels overkill for the problem.

Is there any other way to check the player action, and know what prefab the player is interacting in client?

Edited by Helwdrokin
Spoiler
local function UpdateActionPreview(inst)
	local function OnPerformAction(inst)
		print("action", inst.last_buffaction, inst)
	end
	inst:ListenForEvent("playeractivated", function(inst)
		local PreviewBufferedAction_old = inst.PreviewBufferedAction 
		inst.PreviewBufferedAction = function(self, buffaction, ...)
			inst.last_buffaction = buffaction 
			--print(self, buffaction) --GLOBAL.dumptable(buffaction)
			local result = PreviewBufferedAction_old(self, buffaction, ...)
			return result
		end
		inst:ListenForEvent("performaction", OnPerformAction, inst)
	end, inst)
	inst:ListenForEvent("playerdeactivated", function(inst)
		inst:RemoveEventCallback("performaction", OnPerformAction, inst)
	end, inst)
end
AddPlayerPostInit(UpdateActionPreview)

 

Not sure if there's a better way but this probably does what you're looking for. PreviewBufferedAction is pre, OnPerformAction is post.

Edited by oregu
  • Like 1

This approach didn't worked for me (maybe I did something wrong ? I'm still new in modding dst)
So my solution was detecting the action using the component PlayerController, here, I will share the code that worked to me

local ACTIONS = GLOBAL.ACTIONS
local PlayerController = require("components/playercontroller")
local old_GetActionButtonAction = PlayerController.GetActionButtonAction
local old_OnLeftClick = PlayerController.OnLeftClick

local function OverrideGetActionButtonAction()
    function PlayerController:GetActionButtonAction(force_target)
        local OverridenFunction = old_GetActionButtonAction(self, force_target)
        if OverridenFunction and OverridenFunction.action == ACTIONS.PICK then
            print(OverridenFunction)
        end
        return OverridenFunction
    end
end

local function OverrideOnLeftClick()
    function PlayerController:OnLeftClick(down)
        if not self:UsingMouse() then return
        elseif not down then return end
        local OverridenFunction = old_OnLeftClick(self, down)
        local act = self:GetLeftMouseAction()
        if act and act.action == ACTIONS.PICK then
            print(act)
        end
        return OverridenFunction
    end
end

AddPlayerPostInit(OverrideGetActionButtonAction)
AddPlayerPostInit(OverrideOnLeftClick)

This is probably not the best approach, but is what worked for me. Both the functions are called before the action is performed and the function GetActionButtonAction is called twice each spacebar press, (once when pressed down and once when up), is not a problem me, so I didn't tried to fix
also thanks @oregu for your help :D

14 hours ago, Helwdrokin said:

This is probably not the best approach, but is what worked for me. Both the functions are called before the action is performed and the function GetActionButtonAction is called twice each spacebar press, (once when pressed down and once when up), is not a problem me, so I didn't tried to fix
also thanks @oregu for your help :D

I didn't notice it before but you need lag compensation to be enabled in order to have a buffered action as Client. Otherwise, your action is just your action, not a buffered action. 

If you want to overwrite the PICK function, it would be ACTIONS.PICK.fn, right? If you want to overwrite all ACTION fns, maybe you'd have to iterate across them all and insert your function. Not sure what the most intuitive way of doing that is if not in pairs. I don't see a way to do that clientside though. It prints in the server log for some reason. So maybe it's not a client thing?

Spoiler
PICKfn_old = GLOBAL.ACTIONS.PICK.fn
GLOBAL.ACTIONS.PICK.fn = function(action, ...) -- prints to the server log
	print("pre", action, ...)
	local result = PICKfn_old(action, ...)
	print("post", action, ...)
	return result
end

This wont do, because you can only modify the stategraph. It wont  print the action.

Spoiler
local NewPickHandler = GLOBAL.ActionHandler(GLOBAL.ACTIONS.PICK, function(inst, action)
	print(inst, action)
end)
AddStategraphActionHandler("wilson", NewPickHandler)
Edited by oregu

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