Jump to content

Locals


Alienhead
 Share

Recommended Posts

I wasn't for sure if this was a bug or if it is by design so I figured I'd ask here:

Creating anything inside a Require() file or a component with the keyword Local results in the creation not showing. Be it a mesh, sprite, model or whatever.. 

Example:

This is inside a Component script file.

                    -- label
                    if self.ynCreateDebugLabels then
                        local sp = CreateSprite(world, self.font, "This is a sprite !", 14, TEXT_CENTER)
                        sp:SetPosition(self.UAGCSliders[t]:GetPosition())
                    end

It never shows up in the world. But it does exist as the SetPosition command never throws an error.

This does: work as one would expect but only after declaring it as a Global.

                    -- label
                    if self.ynCreateDebugLabels then
                        sp = CreateSprite(world, self.font, "This is a sprite !", 14, TEXT_CENTER)
                        sp:SetPosition(self.UAGCSliders[t]:GetPosition())
                    end

 

 

 

Link to comment
Share on other sites

Makes sense to me. The local variable goes out of scope when the function finishes, and the variable is collected and the value deleted.

In Ultra, we can actually make use the Lua's garbage collection :)

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

One last LUA question, and this one puzzles me.

I have a file Require() I use for general utility stuff..  Inside the file is a bunch of functions.

I can add a sprite to the table like so.. it prints the name fine. 

 

 
function AddSpriteLabelToTable(sprite)
 
    if splabel == nil then splabel = {} end
 
    table.insert ( splabel, {
        sprite = sprite,
    })
 
    for t, s in ipairs ( splabel ) do
        consolepad:addtext(s.sprite,name)
    end
 
end

When calling an update function that access this global table I get all kinds of trouble. Usually always resulting in nil or missing table.

 

 
function UpdateSpriteLabels()
 
    for t, s in pairs ( splabel ) do
        consolepad:addtext(s.sprite,name)
    end
 
end

These functions are located in the same Require() file, but oddly trying to access the table created in the AddSpriteLabelToTable function  results in error.

Do all my tables need to be created on the top level in order for the entire project to access them? be it Require() files or component  scripts? 

LE allowed this kinda of access, I was just wondering if this is something new and if so where do I need to define global tables that all scripts can access?

Link to comment
Share on other sites

I am not sure. Do you know how to set breakpoints in VSCode? That might allow you to view the global vars.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

yes I will attempt to find where it breaks.. But I can tell you now the function does not recognize the table at all.  This holds true when creating a table in a component script as well..

self.atable = {}

table.insert( self.atable, {
somevalue = 10
}

then in a different function of the same script file the table is not recognized

 

Link to comment
Share on other sites

This works:

 

splabel = {}
 
function AddSpriteLabelToTable(sprite)
 
    table.insert ( splabel, {
        sprite = sprite,
    })
 
end


 
function UpdateSpriteLabels()
 
    for t, s in pairs ( splabel ) do
       consolepad:addtext(s.sprite,name)
    end
 
end

 

 

This does not:

 

function AddSpriteLabelToTable(sprite)
 
    if splabel == nil then splabel = {} end 
 
    table.insert ( splabel, {
        sprite = sprite,
    })
 
end


 
function UpdateSpriteLabels()
 
    for t, s in pairs ( splabel ) do    ---- CRASH  NO SUCH TABLE
       consolepad:addtext(s.sprite,name)
    end
 
end

 

 

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...