RadinQue Posted April 10, 2020 Share Posted April 10, 2020 Hello! 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? Thanks in advance! Link to comment https://forums.kleientertainment.com/forums/topic/117192-help-referencing-to-the-player-who-activated-an-activatable/ Share on other sites More sharing options...
CarlZalph Posted April 11, 2020 Share Posted April 11, 2020 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 1 Link to comment https://forums.kleientertainment.com/forums/topic/117192-help-referencing-to-the-player-who-activated-an-activatable/#findComment-1323335 Share on other sites More sharing options...
RadinQue Posted April 11, 2020 Author Share Posted April 11, 2020 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 ^^ Link to comment https://forums.kleientertainment.com/forums/topic/117192-help-referencing-to-the-player-who-activated-an-activatable/#findComment-1323391 Share on other sites More sharing options...
CarlZalph Posted April 11, 2020 Share Posted April 11, 2020 (edited) 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 April 11, 2020 by CarlZalph 1 Link to comment https://forums.kleientertainment.com/forums/topic/117192-help-referencing-to-the-player-who-activated-an-activatable/#findComment-1323435 Share on other sites More sharing options...
RadinQue Posted April 11, 2020 Author Share Posted April 11, 2020 Thank you, now it's clear. You are the best! Link to comment https://forums.kleientertainment.com/forums/topic/117192-help-referencing-to-the-player-who-activated-an-activatable/#findComment-1323451 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now