Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Can a class return a value? — Gideros Forum

Can a class return a value?

MoKaLuxMoKaLux Member
edited February 2020 in General questions
I need some explanation please.
PLAYER1 = Core.class()
function PLAYER1:init()
	local obj = 1
	return obj
end
Why does this return a table instead of 1? Is it due to init?
Thank you for your help giderians :) .
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Tagged:

Comments

  • rrraptorrrraptor Member
    edited February 2020 Accepted Answer
    See class function here:
    https://github.com/gideros/gideros/blob/master/luabinding/property.lua

    You need to change it like that in order to get what you want:
    do
    	local c = Object
    	c.__index = c
    	c.__new = function(...)
    		local s1 = {}
     
    		setmetatable(s1, c)
     
    		local init = rawget(c, "init")
    		if type(init) == "function" then
    			init(s1, ...)
    		end
     
    		return s1
    	end
     
    	c.new = function(...)
    		local s1 = c.__new(...)
     
    		local postInit = s1.postInit
    		if type(postInit) == "function" then
    			postInit(s1, ...)
    		end
     
    		return s1
    	end
    end
     
    local __Object = Object
     
    MyClass = function (b,a)
    	if a then
    		assert(type(a)=="function","Second argument to Core.class() should be a function or null")
    	end
    	b = b or __Object
     
    	local c = {}
    	c.__index = c
    	setmetatable(c, b)
     
    	c.super = b
     
    	local v
    	c.__new = function(...)
    		local b = getmetatable(c)
    		local s1
    		if a then
    		  s1 = b.__new(a(...))      
    		else
    		  s1 = b.__new(...)
    		end
     
    		setmetatable(s1, c)
     
    		local init = rawget(c, "init")
    		if type(init) == "function" then
    			v = init(s1, ...)
    		end
     
    		return s1, v
    	end
     
    	c.new = function(...)
    		local s1 = c.__new(...)
    		local postInit = s1.postInit
    		if type(postInit) == "function" then
    			v = postInit(s1, ...)
    		end
     
    		return s1,v
    	end
     
    	return c
    end
     
    local test = MyClass(Sprite)
     
    function test:init()
    	local a = 3123
    	return a
    end
     
    local instance, v = test.new()
    print(instance, v)


    The question is. Why you cant just attach this variable to "self"?
    ---test.lua file
    local ASD = 49864
     
    test = Core.class()
    function test:init()
    	self.ASD = ASD
    end
     
    -- main.lua file
    local obj = test.new()
    print(obj.ASD)
  • hgy29hgy29 Maintainer
    Accepted Answer
    @MoKaLux, can you explain what you are trying to achieve ?

    Likes: oleg

    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited February 2020
    thank you for your feedback.
    I wanted to have access to obj and obj.body inside the init.
    I can have access to those this way:
    PLAYER1 = Core.class()
     
    function PLAYER1:init(xworld, xobjpath, xobjname)
    	self.obj = loadObj(xobjpath, xobjname) -- returns a sprite
    	self.obj.body = xworld:createBody(self.obj:getMatrix())
    --	return self.obj, self.obj.body -- that doesn't work!
    end
     
    function PLAYER1:get()
    	return self.obj, self.obj.body -- that works
    end
     
    -- in the main
    local player1x = PLAYER1.new(world, "path", "name.obj")
    local player1, player1body = player1x;get()
    Is it good or bad?
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • rrraptorrrraptor Member
    edited February 2020 Accepted Answer
    PLAYER1 = Core.class()
     
    function PLAYER1:init(xworld, xobjpath, xobjname)
    	self.obj = loadObj(xobjpath, xobjname)
    	self.body = xworld:createBody(self.obj:getMatrix())
    end
    -- in the main
    local player1 = PLAYER1.new(world, "path", "name.obj")
    player1.obj
    player1.body
    ?

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • thank you very much rrraptor, yes that's the answer I was looking for <3
    very much appreciated :)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Sign In or Register to comment.