Yue Posted October 1, 2021 Posted October 1, 2021 CWorld= { world=nil, } function CWorld:Create() self.world = World:Create() local this={} this.world = nil World:SetCurrent(self.world) function this:GetCurrent() return(World:GetCurrent()) end function this:GetWorld() return(CWorld.world) end return(this) end menu = CWorld:Create() map = Map:Load("Maps/menu.map") mars = CWorld:Create() map2 = Map:Load("Maps/testMap.map") System:Print(mars:GetWorld()) -- x085x System:Print(menu:GetWorld()) -- x085x I am trying to get this to work, but it turns out that when I try to retrieve the world, it returns two identical values, even though I have created two worlds. what am I doing wrong? Quote
Yue Posted October 1, 2021 Author Posted October 1, 2021 CWorld= { worldx=nil, } function CWorld:Create() self.worldx = World:Create() return(self) end mundo1 = CWorld:Create() mundo2 = CWorld:Create() System:Print(mundo1.worldx) System:Print(mundo2.worldx) Something simpler. Quote
Genebris Posted October 3, 2021 Posted October 3, 2021 How do you know they are the same world? Maybe System:Print gives you the same output for different worlds. Try to spawn something in one world and render another. Also, what if you do this? mundo1 = CWorld:Create() System:Print(mundo1.worldx) mundo2 = CWorld:Create() System:Print(mundo2.worldx) 1 Quote
Yue Posted October 3, 2021 Author Posted October 3, 2021 I'm trying to understand this Lua class thing, apparently it's a static variable. Evidently it's the same world because I can't find a way to change it. So what I am doing is looking at the Lua documentation regarding tables, where although you can create objects, the term class does not exist, and what you do is to use a prototype of an object to create others. Quote
Yue Posted October 3, 2021 Author Posted October 3, 2021 CWorld= { w =nil, Create = function(self,wc) this={} setmetatable(this,self) self.__index = self self.w = wc return(this) end } local world1 = CWorld:Create(World:Create()) local world2 = CWorld:Create(World:Create()) System:Print(world1.w) System:Print(world2.w) I can't understand this, I go back to the same world, when they should be two different worlds. This has a problem because I am dealing with the OOP paradigm, that is to say that when I try to change the rendering world I will always have the same one, no matter the object. Quote
Yue Posted October 3, 2021 Author Posted October 3, 2021 Ok, solved here. CWorld= { world =nil, Create = function(self) this={} setmetatable(this,self) self.__index = self function this:Start() self.world = World:Create() end this:Start() return(this) end } local world1 = CWorld:Create() local world2 = CWorld:Create() System:Print(world1.world) System:Print(world2.world) Quote
Yue Posted October 3, 2021 Author Posted October 3, 2021 A more correct approach to object programming. CWorld= { world =nil, Create = function(self) this={} setmetatable(this,self) self.__index = self function this:Start() self.world = World:Create() end function this:GetWorld() return(self.world) end this:Start() return(this) end } local world1 = CWorld:Create() local world2 = CWorld:Create() System:Print(world1:GetWorld()) System:Print(world2:GetWorld()) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.