Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How do I get the memory address of a variable, rather than the variable? — Gideros Forum

How do I get the memory address of a variable, rather than the variable?

Tom2012Tom2012 Guru
edited October 2012 in General questions
Hello

On my scene I have local score = 0;

On a class I would like to change the score to say 1.

If I run say Score:updateScore(score) it passes the value of score rather than the memory address.

Basically I'm trying to update score without using a global.

Thanks!!

Tom

Comments

  • MellsMells Guru
    edited October 2012
    I believe you should be more specific and show more of your code so forum members could help you.
    What you are trying to achieve doesn't sounds complex and should work with a quick fixes.

    Is that what you are trying to achieve?
    -- main.lua
    local score = 1
    print (score) -- 1
     
    local scoreObj = Score.new(score)
    score = scoreObj:updateScore(3)
     
    print (score) -- 4
    -- Score.lua
     
    Score = Core.class(Sprite)
     
    function Score:init(initScore)
         self.score = initScore
    end
     
    function Score:updateScore(addToScore)
         self.score = self.score + addToScore
         return self.score
    end
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • Tom2012Tom2012 Guru
    edited October 2012
    Hi

    I have a class that spawns characters on screen. Here's some of it...
    function Caveman:onEnterFrame(event)
     
    	if(self:collidesWith(self.hector.headAnimation)) then
     
    -- caveman hits hector
    		if(not self.collided and not self.onGround) then
    			self.collided = true;
    			score = score + 1;
    			self.scoreText:setText(tostring(score)) -- increase score
    			self:setVisible(false)
    			self.hector.headAnimation:setAnimation("HECTOR_HEAD_MUNCH_2")
    			self.blood:start()
    			self.hector.headAnimation:playAnimation()
    			self.munching = true;
    		end
    	end
    I just can't figure out how to get this class to update the score on the scene:

    Thanks for the example but I need to update the scene from the spawn class, not the other way around - if that makes sense.

    Thanks Mells
  • bowerandybowerandy Guru
    edited October 2012 Accepted Answer
    @Tom2012. You can't do it the way you suggest in Lua. You can't pass objects by pointer or by reference (as far as I know). However, what you can do is pass in a table which you can alter the contents of. And, indeed, that table could be the scene containing the score.
    CaveScene=Core.class()
     
    function CaveScene:init()
        self.score=0
    end
     
    ...
     
    function Caveman:bumpScore(scene)
        scene.score=scene.score+1
       ....
    end
    best regards

    Likes: Tom2012

    +1 -1 (+1 / -0 )Share on Facebook
  • MellsMells Guru
    edited October 2012
    I noticed in another discussion (I think it was you) that you said "it's amazing how a few words can make an application work" (you mistook stage for self or the other way around).
    What you are trying to do can be solved but I think you should study more about the value of "self" (like, what is "self" at self.scoreText:setText(tostring(score))) and solve it yourself, so you will be able to solve the same kind of scope issues that you'll have to face in the future.

    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • @bowerandy

    Thank you. That's exactly what I was looking for.

    Can't believe it was so simple. :0)
Sign In or Register to comment.