Jump to content

Help me here Class on Lua.


Yue
 Share

Recommended Posts

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?

 

Mars.jpg.89ab63a64eebc1f5ada0ab82b66a1f8c.jpg

 

 

Link to comment
Share on other sites

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)

 

  • Like 1
Link to comment
Share on other sites

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.

Mars.jpg.89ab63a64eebc1f5ada0ab82b66a1f8c.jpg

 

 

Link to comment
Share on other sites

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.

Mars.jpg.89ab63a64eebc1f5ada0ab82b66a1f8c.jpg

 

 

Link to comment
Share on other sites

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)

 

Mars.jpg.89ab63a64eebc1f5ada0ab82b66a1f8c.jpg

 

 

Link to comment
Share on other sites

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())

 

Mars.jpg.89ab63a64eebc1f5ada0ab82b66a1f8c.jpg

 

 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...