UnLimiTeD^ Posted July 24, 2014 Share Posted July 24, 2014 Hello, I wanna write a lobby system based on elements. My Problem is that he tells me that self is an userdata value in the constructor. ( if not self.m_Lobby[element] then ) Here's my code: function Lobby:new() local obj = setmetatable({}, {__index = self}) if obj.constructor then addEventHandler("onPlayerResourceStart", root, obj.constructor ) end obj.m_Lobby = {} obj.maps = { -- int posX, int posY, int posZ, int Dimension [1] = {0, 0, 3, 1} } return obj end function Lobby:constructor(element) if not self.m_Lobby[element] then self.m_Lobby[element] = {} local mL = self.m_Lobby[element] mL.public = false mL.name = nil mL.maxPlayers = nil mL.map = nil end end Thats my trigger: triggerServerEvent("onPlayerResourceStart", localPlayer, localPlayer) -- clientside /e the onPlayerResourceStart shall triggered from a client. Link to comment
RottenFlesh Posted July 25, 2014 Share Posted July 25, 2014 What do you get from getElementType(self) ? BTW, when i do something in oop i do it like this: MyClass = {} MyClass.__index = MyClass function MyClass.new(name) self = setmetatable({}, MyClass) self.name = name return self end Correct me if i'm wrong. Link to comment
UnLimiTeD^ Posted July 25, 2014 Author Share Posted July 25, 2014 He returns "player" But I really dont know why .__. Anyone an idea how i Could fix it? Link to comment
cheez3d Posted July 25, 2014 Share Posted July 25, 2014 local Lobby = {}; Lobby.__index = Lobby; function Lobby:Constructor(player) if not self.m_Lobby[player] then self.m_Lobby[player] = {}; end; local mL = self.m_Lobby[player]; mL.public = false; mL.name = nil; mL.maxPlayers = nil; mL.map = nil; end; function Lobby:New() local Object = setmetatable({ m_Lobby = {}; maps = { {0,0,3,1}; }; },self); -- create our object; if Object.Constructor then addEventHandler("onPlayerResourceStart",root,function() Object:Constructor(client); -- use syntactic sugar; end); end; return Object; end; I think this is what you wanted. Link to comment
ixjf Posted July 25, 2014 Share Posted July 25, 2014 Hello, I wanna write a lobby system based on elements. My Problem is that he tells me that self is an userdata value in the constructor. ( if not self.m_Lobby[element] then ) Here's my code: function Lobby:new() local obj = setmetatable({}, {__index = self}) if obj.constructor then addEventHandler("onPlayerResourceStart", root, obj.constructor ) end obj.m_Lobby = {} obj.maps = { -- int posX, int posY, int posZ, int Dimension [1] = {0, 0, 3, 1} } return obj end function Lobby:constructor(element) if not self.m_Lobby[element] then self.m_Lobby[element] = {} local mL = self.m_Lobby[element] mL.public = false mL.name = nil mL.maxPlayers = nil mL.map = nil end end Thats my trigger: triggerServerEvent("onPlayerResourceStart", localPlayer, localPlayer) -- clientside /e the onPlayerResourceStart shall triggered from a client. obj.constructor ( ... ) isn't the same as obj:constructor ( ... ). That is syntatic sugar for obj.constructor ( obj, ... ), where the first argument passed is self, but MTA will call obj.constructor ( ... ). If you want it to pass the object, you need to pass it manually, i.e. addEventHandler ( "onPlayerResourceStart", root, function ( ... ) obj.constructor ( obj, ... ) end ) Or: addEventHandler ( "onPlayerResourceStart", root, function ( ... ) obj:constructor ( ... ) end ) Now, if you want to get rid of this messy code, you can use sbx320's class library which addresses class emulation, applying classes to elements, among others. It also provides a bind function which basically allows you to prepend arguments to a function call, so instead of the code above, you would just write this: addEventHandler ( "onPlayerResourceStart", root, bind ( obj.constructor, obj ) ) It's worth mentioning that this library does not work with MTA 1.4 OOP enabled, so you would have to use the 1.4 version, which is only available here, but according to sbx320, there are some minor issues with it, although I didn't come across any while I used it. Link to comment
UnLimiTeD^ Posted July 25, 2014 Author Share Posted July 25, 2014 Works now, thanks Cheez3D. Link to comment
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