Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
pass a variabile by reference as a parameter to a constructor? — Gideros Forum

pass a variabile by reference as a parameter to a constructor?

piepie Member
edited February 2014 in Code snippets
Hi,
I need to pass a variabile by reference as a parameter to a constructor;
as far as I understood googling, in Lua all variables should be passed by reference than by value, but not this time.

I'm trying to build a "button" (sensible area) on which I can "drag" something (a number based on a global var) and apply some maths.
distilled from my code, I have this vartest Sprite, and "money" which is the global variable I'd like to work on.
 
players = {}
players[1] = {}
players[1]["money"] = 2 --this is my global var;
 
vartest=Core.class(Sprite) 
 
function vartest:init(variable) 
	self.refVar = variable
	print( "INIT:", self.refVar, players[1]["money"])
	self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
end
 
function vartest:onMouseDown(event)
	self.refVar = self.refVar+4 
	print( "MOUSE_DOWN:", self.refVar, players[1]["money"]) --those values (in my dreams) should be equal.
end
 
--[[ 
how could I pass via parameters the reference to my variable instead of its value? 
]]
 
local vartest = vartest.new(players[1]["money"])
stage:addChild(vartest)
I'd like that vartest does its edits directly on the provided global var.

Any advice is appreciated, thank you in advance :)

Comments

  • OZAppsOZApps Guru
    Accepted Answer
    @pie,
    a simple way you can do that is
     local vartest = vartest.new()
     vartest.myVar = players[1]["money"]
    Everytime you want to change this, all you need to do is just reference the vartest.myVar to a new global variable and you are all set to go.

    BTW: You can also access the
    players[1]["money"]
    as
    players[1].money
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • Thank you! How did I miss that?! :)
  • @OZApps
    I accepted your answer on my enthusiasm, but as I tried it, it gives me the same behaviour :-/ ..am I still missing something?

    It seems that lua is copying the variable value rather than referencing to the variable itself, either if it's referenced from outside of the constructor.

    You can copy/paste my example in a blank project to see it (not) working -of course if you have time to look for that :)

    Thank you again
    P.
  • ar2rsawseenar2rsawseen Maintainer
    edited February 2014
    @pie I think that tables/objects are passed by reference.

    So if you encapsulate your variable into a table and pass it, the table field should be modified by reference (from Lua Manual)
    There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table. ....
     
    Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.
  • OZAppsOZApps Guru
    Accepted Answer
    @Pie, you need to look at the code a bit more closely....

    What is happening is that you are adding the variable to the instance of Core.class(Sprite) and since you are using the same names for the class and the instance, you are messing the Lua VM a lot.

    the function vartest:init has to be the global function BUT the mouse handler has to be part of the instance,

    change the names of the variables to cVartest for the class and vartest for the instance and then try to print them in both the init and onMouseDown functions to see the difference

    with the functions as cVartest:init and cVartest:onMouseDown

    print(self, vartest, cVartest) --> 0x10a0e6210, 0x10a0e6210, 0x10a0e5e10

    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • john26john26 Maintainer
    Accepted Answer
    In Lua all variables are pass by value.

    BUT, Lua tables are reference object to start with. E.g.

    a={1,2,3}

    means
    1) allocate an anonymous table {1,2,3} in memory
    2) Make "a" point to it.

    If I do

    b=a

    Now, b is pointing to the same table as a. (a shallow copy). The actual value held by a and b is, eg, 0x0BADF00D a memory location. and that's what gets passed through to functions.
Sign In or Register to comment.