JokerPublic Posted May 2, 2019 Posted May 2, 2019 Hi. I want a function such as: function Entity:setData(...) -- end and i want to this function run if i call localPlayer:setData or localPlayer.vehicle:setData() btw i don't know how can i do this
Moderators Patrick Posted May 2, 2019 Moderators Posted May 2, 2019 https://wiki.multitheftauto.com/wiki/OOP https://wiki.multitheftauto.com/wiki/SetElementData Quote OOP Syntax Method: element:setData(...) Counterpart: getElementData
Awang Posted May 2, 2019 Posted May 2, 2019 (edited) You need to understand first of all the MTA's element tree. Object's, vehicle's, player's, ped's objects are inherited from Element class. If you want to override the setData for all of these classes, you should override in the Element class. Element._setData = setElementData function Element:setData(name,data) self:_setData(name,data) outputDebugString("Element Data `"..name.."` changed to `"..tostring(data).."` for the element: "..tostring(self)) end Self element will be the element, what you called before the function. Important thing, that you need to call with ':', like: localPlayer:setData("source.inheritance",localPlayer) These classes, which are in the element tree are automaticly declaired by MTA, so you do not need to declair them. But if you want for example an other specific override for just players, you can do it: Player._setData = setElementData -- Unfortunatelly, if you want it to replace it to the parent class, for a real inheritance, it will cause stack owerflow... function Player:setData(name,data) self:_setData(name,data) -- this will be the original setData function, not the function of the parent class... outputDebugString("Element Data `"..name.."` changed to `"..tostring(data).."` for the player: "..self.name) end https://wiki.multitheftauto.com/wiki/Element Edited May 2, 2019 by Awang
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