Jump to content

Recommended Posts

I have been trying to figure this out for days, but even after all they comparing between other mods I can not for the life of me figure out why this isn't working.

 

I'm trying to create a custom action for an item I made.  It's not a complicated action at all, it's actually just an exact copy of the "read" action that is used on books.  The code for the item works perfectly when I substitute "watch" and "predict" with "reader" and "book".  But I want to figure out how to create a custom action to both change the name of the text that shows up when the player puts their mouse over the item, and to figure out how to create custom actions for the future.

 

Here is the code I have in modmain

 
local Action = GLOBAL.Actionlocal ActionHandler = GLOBAL.ActionHandler	local PREDICT = Action(3)	PREDICT.str = "Predict"	PREDICT.id = "PREDICT"	PREDICT.fn = function(act)		local targ = act.invobject or act.target		if targ.components.watch then			return act.doer.components.predictor:Predict(targ)		end	end	AddAction(PREDICT)	AddStategraphActionHandler("wilson", ActionHandler(PREDICT, "book"))	local function predictactivate(inst, doer, actions, right)        if doer:HasTag("predictor") then            table.insert(actions, GLOBAL.ACTIONS.PREDICT)        endendAddComponentAction("SCENE", "watch", predictactivate)local function predictactivate_inv(inst, doer, actions, right)        if doer:HasTag("predictor") then            table.insert(actions, GLOBAL.ACTIONS.PREDICT)        endendAddComponentAction("INVENTORY", "watch", predictactivate_inv)

Here is the component watch (identical to the component book)

local Watch = Class(function(self, inst)    self.inst = instend)function Watch:OnPredict(predictor)    if self.onpredict then        return self.onpredict(self.inst, predictor)    end    return trueendreturn Watch

and the component predictor (Identical to the component reader)

local Predictor = Class(function(self, inst)    self.inst = inst    inst:AddTag("predictor")end)function Predictor:OnRemoveFromEntity()    self.inst:RemoveTag("predictor")endfunction Predictor:Predict(watch)	if watch.components.watch then		if watch.components.watch:OnPredict(self.inst) then			if watch.components.finiteuses then				watch.components.finiteuses:Use(1)			end			return true		end			endendreturn Predictor

For my Item prefab

--    inst:AddComponent("book") --Using this makes it work fine--    inst.components.book.onread = onpredict    inst:AddComponent("watch") --But this always shows up as "Examine"    inst.components.watch.onpredict = onpredict

for my Character prefab

	inst:AddComponent("predictor")  --Item still shows as "Examine"--	inst:AddComponent("reader")  --Works perfectly

The function that triggers when the item actually gets used works perfectly, but I'm trying to create a custom action for my item for a number of reasons.  Mostly learning purposes.

 

But I don't know what I'm missing here, why does the item only show "Examine" for the right click option?

Edited by Zackreaver

Okay I don't know if this is fate playing some kind of ******* joke on me, but it seems like EVERYTIME I come here and post code and say "This doesn't work"

 

I go back and try it one more time and IT ******* WORKS!!

 

Seriously, this happened so many times now...  I don't even know what I did to make it work, it just suddenly decided to out of nowhere...

 Welcome to coding :razz:

 

But glad you got it working, actions are a little confusing.

 

Yeah, but ya know I was expecting it to be easier since I was BASICALLY just copying an action that already existed.  But this nonsense took me more than a week to figure out.  Now I wanna know why it just suddenly started working out of nowhere, my suspicion is on this.

 

local PREDICT = Action(3)
 
I had that originally, but then I looked at your throwing spears mod
 
local SPEARTHROW = GLOBAL.Action(4,		-- priority								false,	-- instant (set to not instant)								true,	-- right mouse button								10,		-- distance check								false,	-- ghost valid (set to not ghost valid)								false,	-- can force (false)								nil)	-- range check function

I figured maybe that would get it to work since I had never dealt with custom actions before.  And I was swapping them back and forth for a while.  Added in ComponentActions after finally realizing that was a thing but it still didn't work.  referenced a post made on WallGates, kept comparing and contrasting the codes to see whats different.

 

Then right before posting this thread, I went back to

 

local PREDICT = Action(3)

 

And it worked.  Now my question is why did the other fail?  Why did changing this to an action with less arguments make the action suddenly spring up and start working?

 

Also what does the 3 stand for?  Is it priority?  Priority for what exactly?  Priority for when it would show up to the player?

Also what does the 3 stand for?  Is it priority?  Priority for what exactly?  Priority for when it would show up to the player?
 Yes. So when you mouse over an object, there might be four+ valid actions for interacting with it. Let's say you have a spider gland on your cursor, mousing over a spider. The valid actions should be Heal, Give, Attack, and Examine, I think. But there are only two mouse buttons to map actions to, so the two with highest priority get mapped to left and right click. I'm sure there's more nuance to it, but that's basically the idea.

 

As for why it worked when you dropped the extra arguments... One of the extra ones you added must've been messing it up.

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