Jump to content

Recommended Posts

Hello, I have this bit of code :

local NewComponentActions = {
    ["fuel"] = "USEITEM",
    ["tradable"] = "USEITEM",
    ["container"] = "SCENE",
    ["stewer"] = "SCENE"
}

for component, context in pairs(NewComponentActions) do
    AddComponentAction(context, component, function(inst, doer, actions, ...)
        local rider = doer.replica.rider
        if rider then
            local OldIsRiding = rider.IsRiding  -- Store original function
            rider.IsRiding = function() return false end  -- Simulate not riding

            --TODO: call the original component action logic

            rider.IsRiding = OldIsRiding -- Restore original function
        end
    end)
end

Is this even possible ? Do I have to copy paste the original behavior from the componentactions.lua file ?

Thank you in advance for your insights !

Edit: I know i can't access the COMPONENT_ACTIONS table to read the original funcs, but is there like an other way of making some base component action just ignore the riding state ? Without copy  pasting the original code per AddComponentAction ofc.

Edited by goobboy
Spoiler
local assert = GLOBAL.assert
local debug = GLOBAL.debug
upv = {} -- ++ rezecib
local function GetUpvalueHelper(fn, name)
	local i = 1
	while debug.getupvalue(fn, i) and debug.getupvalue(fn, i) ~= name do
		i = i + 1
	end
	local name, value = debug.getupvalue(fn, i)
	return value, i
end

function upv.Get(fn, ...)
	local prv, i, prv_var = nil, nil, "(the starting point)"
	for j,var in ipairs({...}) do
		assert(type(fn) == "function", "We were looking for "..var..", but the value before it, "
			..prv_var..", wasn't a function (it was a "..type(fn)
			.."). Here's the full chain: "..table.concat({"(the starting point)", ...}, ", "))
		prv = fn
		prv_var = var
		fn, i = GetUpvalueHelper(fn, var)
	end
	return fn, i, prv
end

function upv.Set(start_fn, new_fn, ...)
	local _fn, _fn_i, scope_fn = upv.Get(start_fn, ...)
	debug.setupvalue(scope_fn, _fn_i, new_fn)
end

AddPlayerPostInit(function(inst)
	local COMPONENT_ACTIONS = upv.Get(inst.CollectActions, "COMPONENT_ACTIONS")
	print"~~~~~~~~~~ Componentactions ~~~~~~~~~~~"
	GLOBAL.dumptable(COMPONENT_ACTIONS)
	print"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
end)

 

The COMPONENT_ACTIONS table you want in componentactions.lua can be accessed like that. You can also do GLOBAL.EntityScript.CollectActions for pre-init I imagine.

Spoiler
local ridervalidateacts =
{
	"ADDFUEL",
	"RUMMAGE",
	"GIVE",
	"COOK",
}

for k,v in pairs(ridervalidateacts) do
	GLOBAL.ACTIONS[v].mount_valid = true
end

local function RiderValidator(inst)
	print(inst, inst.IsActionValid)
	local _IsActionValid = inst.IsActionValid
	inst.IsActionValid = function(act, ...) -- Now our server thinks the actions we want to do is valid
		local ridervalidating = false
		for k,v in pairs(ridervalidateacts) do
			if act == v then
				ridervalidating = true
			end
		end
		if not ridervalidating then
			return _IsActionValid(act, ...)
		end
		local _IsRiding = (inst and inst.replica and inst.replica.rider and inst.replica.rider.IsRiding) or (inst and inst.components and inst.components.rider and inst.components.rider.IsRiding)
		if inst:_IsRiding() then
			inst.components.rider.IsRiding = function() return false end
		end
		local result = _IsActionValid(act, ...)
		if inst:_IsRiding() then
			inst.components.rider.IsRiding = _IsRiding
		end
		return result
	end
end
AddPlayerPostInit(RiderValidator)

I also made some random code that tries to validate the actions for rider using other means using IsValid, but I think the issue is that playeractionpicker still doesn't think it's an action you can do some reason, haven't tested that theory yet.

i think AddActionComponent handles what you're trying to do, though.

Edited by oregu

I used the first solution with GLOBAL.EntityScript.CollectActions, the second one was not adding the available actions to the player.

I based my mode on a more recent mode, but now, except the onupdate override for the containers (preventing them to close when riding), I have nothing in common, as he was copy pasting behavior from COMPONENT_ACTIONS funcs, and I wanted a more generalized approach. But that's all good now, you can check Beefalo Actions on the workshop, don't hesitate to tell me if you encounter bugs ! :)

Again, thank you to Oregu for his data fetching function !

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