Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Actor names with a for loop? — Gideros Forum

Actor names with a for loop?

NinjadoodleNinjadoodle Member
edited January 2014 in General questions
Hey guys

I'm trying to figure out the syntax for looping through actor name with a FOR loop.
I have - actor1, actor2 and actor3

I want to set some properties, like …
actor1.hungry = false;
actor1:setY(100)
etc.

I don't want to do this over and over for each actor as the properties will be the same every time.

Is there a way I can do something like this?

for i = 1, 3 do
(actor+"i").hungry = false;
(actor+"i"):setY(100)
end

Thanks in advance for any help :)

Comments

  • Do you mean, like an array? I'll have a a look at the documentation now :)
  • MellsMells Guru
    Accepted Answer
    What @chipster123 is suggesting looks like :
    local myArray = {actor1, actor2, actor3}
    then
    for actorId, actor in pairs(myArray) do
        actor.hungry = false
        actor:setY(100)
    end
    or
    for i = 1, #myArray, 1 do
        myArray[i].hungry = false
        myArray[i]:setY(100)
    end
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • NinjadoodleNinjadoodle Member
    edited January 2014
    Thanks guys that helps a lot, the array definitely helps :)

    I'm still wondering (just out of interest) whether there is a way to do something similar to this … (actor+"i"):setY(100) … without using an array?

    The reason I ask, is that I do something similar in AS3 to access instance names.

    example as3 code … (my actors on the stage are name actor1, actor2 …)

    for (i=0; i<4; i++) { this["actor"+i].buttonMode = true; }
  • This should work:
    actor.."1":setY(100)
    actor.."2":setY(100)
    actor.."3":setY(100)
    actor.."4":setY(100)

  • The whole global scope it actually a table, so if you have variable actor1, it is actually a property of table _G.actor1, which means it can be accessed as
    _G["actor"..i]
  • @ar2rsawseen
    on the other hand if actor1 has been declared as local, is there a way to access it?
    _L["actor"..i]?
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • ar2rsawseenar2rsawseen Maintainer
    edited January 2014
    @Mells good one :)
    But not that easy.
    There is a function: debug.getlocal(level, local)
    where level is the number - amount of functions or scopes deep from global scope, and local, is the index (order) when the variable was created
    and it returns variable name and value

    More info: http://pgl.yoyo.org/luai/i/debug.getlocal

    like:
    function test()
    	local actor1 = Sprite.new()
    	print(actor1)
    	print(_G["actor1"])
    	print(debug.getlocal(1, 1))
    end
     
    test()
    will print:
    table: 068B5340
    nil
    actor1 table: 068B5340

    But as you might guess, it should be only used for debugging/profiling and not in the release versions, as it is probably slow as hell :)
  • Thanks again for the help guys!

    So, arrays are the only way to do this efficiently?
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @Ninjadoodle yes, and by definition, arrays are exactly meant for such type of structures, so you won't have to number the variables ;)
  • @ar2rsawseen
    where level is the number - amount of functions or scopes deep from global scope, and local, is the index (order) when the variable was created
    Well, that's almost impossible to use if you haven't saved the level and order somehow (which defeats the purpose).
    My dream solution sure looked sexier :)

    Likes: ar2rsawseen

    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.