You would either have to declare the variable before the function or return the variable from the function.
Here’s the same in pseudo-code:
-- #1: declare first local profilePhoto local function showPhoto() profilePhoto = display.newImage( event.response.filename, event.response.baseDirectory) end showPhoto() -- #2: return the variable local function showPhoto() local profilePhoto = display.newImage( event.response.filename, event.response.baseDirectory) return profilePhoto end local profilePhoto = showPhoto()
Oh, also, when you later edit your posts (and their code contents), you’ll most likely render the subsequent posts that reply to yours inaccurate. I seems to me now that my pseudo-code and I failed to convey the message, so I’d recommend again that you go through the functions tutorial that I posted earlier.
If you run print (“profilePhoto =”, profilePhoto) outside the showPhoto function, nil displays. That’s the whole question why the value is not passed outside the function, and the variable is empty, despite the fact that the function is assigned a value
The console output doesn’t really help all that much as I have no idea what is on any of those lines of code and if we keep this up, then we will be here until spring.
The fact is that you can manage variables as already mentioned, i.e.
-- -- #method 1: local rect local function define() rect = display.newRect( display.contentCenterX, display.contentCenterY, 100, 100) end define() -- #method 2: local function define() local object = display.newRect( display.contentCenterX, display.contentCenterY, 100, 100) return object end local rect = define()
Either way you’d have a variable named “rect” that you could manipulate outside of the function(s) where it is created. You may have the “a nil value” error for several reasons, in this case if the image exists, then your issue may lie with “common.fullw” not being defined. You need to do the debugging yourself now.
network.request is an asynchronous function. What that means is that the function showPhoto is not executed immediately. It waits until the file has been successfully (or unsuccessfully) downloaded, and then runs. It might be very quick for a small image, but equally that request could have been for an mp4 video and taken 30 seconds.
Therefore if you try and access profilePhoto immediately after calling network.request, it will be nil. Therefore any code that needs to act on profilePhoto needs to also wait until the listener showPhoto has executed, and should check to make sure the image exists before doing anything.
I can work with the image in function, it is not difficult. I was more interested to understand why it didn’t work in my example. Thank you for enlightening me! Can you give an example of what you’re talking about?
You would either have to declare the variable before the function or return the variable from the function.
Here’s the same in pseudo-code:
-- #1: declare first local profilePhoto local function showPhoto() profilePhoto = display.newImage( event.response.filename, event.response.baseDirectory) end showPhoto() -- #2: return the variable local function showPhoto() local profilePhoto = display.newImage( event.response.filename, event.response.baseDirectory) return profilePhoto end local profilePhoto = showPhoto()