"local" question

Hello everyone! I feel like this is probably a dumb question… but I don’t know the solution. :confused:

When I create an image within a function I don’t know how to refer to it later on without getting an error that says, “attempt to index global ‘whatever.’” How is this worked out? Thanks!

Nathan [import]uid: 39302 topic_id: 26274 reply_id: 326274[/import]

Hi

Declare your locals on the top of your module.

:slight_smile: [import]uid: 122802 topic_id: 26274 reply_id: 106482[/import]

Do you create a lot of images in this way? Like spawning many? If so, adding them to a table is the best way. Of course, then you need a way to locate them in that table. You can index them by name, but then you might need to remove them by name (index) later.

If you just do a few images this way, you can set up-value indexes for them… just declare the local variable above as a “holder” and make it into an image in the function.

Does this help?

Brent Sorrentino [import]uid: 9747 topic_id: 26274 reply_id: 106483[/import]

I tried declaring the locals at the top, but it didn’t seem to work.

In my current project I only use the same image once. I’m not spawning the same image multiple times.

[import]uid: 39302 topic_id: 26274 reply_id: 106552[/import]

There’s a few things that might be going on here:

[code]local myImage

function showImage()
myImage = display.newImage(“heyo.png”)
end

showImage()
myImage.x = 10
[/code]

This is fine, it’s garden variety pre-declaring your variables. But there are two popular ways in which users get this wrong:

[code]-- [1]
local myImage

function showImage()
local myImage = display.newImage(“heyo.png”)
end[/code]

Here, you’ve redeclared. The myImage at the top is not the same as the myImage inside the function, so your first myImage is still == nil. You need to drop “local” from inside the function.

[code]-- [2]
local myImage

function showImage()
myImage = display.newImage(“heyo.png”)
end

local pic = showImage()
pic.x = 10[/code]

Here, the problem is that you are not returning anything from the function. If you don’t type “return” anywhere, showImage() will return “false”, which obviously can’t set x/y. “pic” is effectively empty. The proper way to do it is something like this:

function showImage() local image = display.newImage("heyo.png") return image end [import]uid: 41884 topic_id: 26274 reply_id: 106555[/import]

So, it should look like this?
[lua]local image

function showImage()
image = display.newImage(“heyo.png”)
return image
end

local pic = showImage()
pic.x = 10[/lua]
Thanks for your help by the way! [import]uid: 39302 topic_id: 26274 reply_id: 106558[/import]

Well you could, I guess. But it seems kinda pointless. What you should be doing it anonymizing the content of the function (so it can exist anywhere) and predeclaring the variable result.

a. In your example, basically you have two variables (“image” and “pic”) that refer to the same image, which is pretty wasteful.

b. Here’s probably what you want to do:

[code]local pic

local function showImage()
local image = display.newImage(“heyo.png”)
– “image” is only accessible here, but that’s OK because we return it!
return image
end

pic = showImage()
pic.x = 10[/code]

So a common thing to do in these functions is to instead declare an image group, and return the group, which works great for things like buttons. That way, if you need to say, change the background color of the button, you refer to the group, eg:

[lua]button[1]:setFillColor(0,0,255)[/lua] [import]uid: 41884 topic_id: 26274 reply_id: 106561[/import]

Okay thanks. I’ll work with this for a while. I’ll probably come back with another question though… :slight_smile: [import]uid: 39302 topic_id: 26274 reply_id: 106567[/import]

If you’re only doing this once (with one image or even a few images), I wouldn’t have the function “return” anything. I’d just directly set it as an image(s) and move on.

local image  
  
local function showImage() --this really should be a local function, unless you need it global for some reason  
 image = display.newImage("heyo.png")  
end  
   
showImage()  
image.x = 10  

[import]uid: 9747 topic_id: 26274 reply_id: 106573[/import]

Only once, then go the easy way

[code]

local image = display.newImage(“heyo.png”)
image.x = 10

[/code] [import]uid: 81188 topic_id: 26274 reply_id: 106580[/import]

You guys are right, but the OP was saying he was having problems with variables, so I’m assuming he was doing some other funny business and attempted to clarify a few scenarios. :slight_smile: [import]uid: 41884 topic_id: 26274 reply_id: 106588[/import]