What happens with group when I return it from the function?

So I have such function

local function makeIndicator (frame, text)
    local group
    group = display.newGroup()
    
    --some code here

    return group
end

--later I have such code:

local indicatorGroup, indicator1
indicator1= display.newGroup()
indicatorGroup = display.newGroup()

indicator1 = makeIndicator (frame, text)
indicatorGroup:insert (indicator1)

But did I really need to directly declare

indicator1= display.newGroup()
?

Does code without declaration indicator1 as newGroup () work the same way that with declaration?

local indicatorGroup, indicator1
indicatorGroup = display.newGroup()

indicator1 = makeIndicator (frame, text)
indicatorGroup:insert (indicator1)

I’m not exactly sure of what you’re trying to accomplish with that, but you don’t need to first declare indicator1 = display.newGroup(). The code below works as you’ve stated.

1 Like

In fact, in the first block of code, that line would actually leak the first group, since although indicator1 no longer has a reference to it (conventionally we assign nil, but it only needs to be a different value), Solar’s resources still need to be removeSelf()'d away.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.