Jump to content

Recommended Posts

5 hours ago, RadinQue said:

I made an object that has an "activatable" component. I want to reference to the player('s character) who activated it. Is there a way? Or should I use another component?

From the activatable component lua:

function Activatable:DoActivate(doer)
    if self.OnActivate ~= nil then
        self.inactive = false
        return self.OnActivate(self.inst, doer)
    end
	return nil
end

So in your mod after you added the component, you may specify a function as the component's OnActivate variable:

local somecallbackname = function(owner, doer)
    -- owner is yourobject, doer is the player or whatever that activated it.  Differentiate it by checking if doer:HasTag("player").
end




yourobject:AddComponent("activatable")
yourobject.components.activatable.OnActivate = somecallbackname

 

  • Thanks 1

Oh so THIS is what "doer" is. For some reason I thought it's the prefab that was activated.

Just out of curiosity, can you explain when exactly "DoActivate" executes? Is is in the game's source code?

Anyways, many thanks ^^

4 hours ago, RadinQue said:

Just out of curiosity, can you explain when exactly "DoActivate" executes? Is is in the game's source code?

Only reference is in the actions.lua file:

ACTIONS.ACTIVATE.fn = function(act)
    if act.target.components.activatable ~= nil and act.target.components.activatable:CanActivate(act.doer) then
        local success, msg = act.target.components.activatable:DoActivate(act.doer)
        return (success ~= false), msg -- note: for legacy reasons, nil will be true
    end
end

So once something uses the activate action, like the player doing the action, it will call this function eventually.

If the OnActivate function is defined on the component, then it will call it; this is the callback function from above that you can define.

 

Since DoActivate doesn't do anything more than call OnActivate if it exists, it effectively is a wrapper function; you could accomplish the same exact functionality by replacing the component's DoActivate function with your own and calling the original ala:

local DoActivate_old = comp.DoActivate
comp.DoActivate = function(self, doer, ...)
    -- "OnActivate" functionality here with "self.inst" is the owner of the component and "doer" is the activator.
    return DoActivate_old(self, doer, ...)
end

No real benefit for doing it like this, but added here for showcasing a neat feature of LUA.

Edited by CarlZalph
  • Thanks 1

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