How to call and get return value from external modules in Corona

Hey
I am creating a huge app in Corona SDK and I need to create a lot of external modules for different purposes like Urban Airship, connections to our .NET Server Library and so on… One of my greatest issues now is that I need to be able to call a function inside an external module and have that listener inside that function return the value back to the calling script.

This is my code…

main.lua and here I include the smart library as global, correct?
[lua]smart = require(“smartfunctions”)[/lua]

products.lua and here I need to present some strings that should be retrieved from network and displayed correctly…
[lua]local headline = display.newRetinaText(smart.getText(“proHeader”,“SE”), 10, 35, native.systemFontBold, 16)[/lua]

So I made a function called getText and that function is placed inside the smartfunctions.lua as below and it takes two arguments, textID and langaugeID and should return the string I get back from the server.

smartfunctions.lua
[lua]module(…, package.seeall)

– smartfunctions.lua
– version 1.0
– Updated by Andreas Kviby, 10Fingers AB
– *********************************************************

function netTextListener( event )
if ( event.isError ) then
returnString = “”
else
returnString = event.response
end

print ("The returning text should be " … returnString)
return returnString
end

function getText(textID, languageID)
print("get network text: " … textID … " in language " … languageID)

if (languageID == nil) then
languageID = app.languageID
end

– Get the text requested and return it through the listener
network.request( app.netWorkTextURL … “?languageID=” … languageID … “&textID=” … textID… “&appWebID=” … app.appWEBID, “GET”, netTextListener )
end[/lua]

The print line inside the networklistener is printing the correct value from our servers but I can’t make the getText function to return that string because it ends inside the listener. Please help me get this work. [import]uid: 22737 topic_id: 20989 reply_id: 320989[/import]

Looking at the code, should you not have a return line in the get function?

[code]function getText(textID, languageID)
print("get network text: " … textID … " in language " … languageID)

if (languageID == nil) then
languageID = app.languageID
end

– Get the text requested and return it through the listener
network.request( app.netWorkTextURL … “?languageID=” … languageID … “&textID=” … textID… “&appWebID=” … app.appWEBID, “GET”, netTextListener )

return textID --or return whichever string you’re after
end[/code]

As you say anything which is local to this function will go out of scope once the function ends, but seeing as this is a get function I would expect it to return a value of some kind at the end.

Or is this all the other way round? I’m not 100% sure exactly where you’re struggling since you’ve said you can’t get a function to return, yet you have a listener which is returning a string. [import]uid: 84115 topic_id: 20989 reply_id: 82908[/import]

Hey
The problem is that your line which returns textID will be executed before the correct string is returned inside the networklistener so somehow I need to get the string returned from network listener to be return from the getText function. Understand? [import]uid: 22737 topic_id: 20989 reply_id: 82914[/import]

In a case like this I would use an event listener.

  
smart = require("smartfunctions")  
  
-- Create the display object to be updated from the net  
local headline = display.newRetinaText("Loading...", 10, 35, native.systemFontBold, 16)  
  
-- Create an event listener for the downloaded text from the net  
local textUpdateFunction = function(e)  
headline.text = e.newText;  
end;  
  
-- Register a custom event called 'updateText' which will call 'textUpdateFunction'  
runtime:addEventListener("updateText", textUpdateFunction)  
  
-- Call function to get the new text from the net  
smart.getText("proHeader","SE");  
--  
-- smartfunctions.lua  
--  
module(..., package.seeall)  
   
-- smartfunctions.lua  
-- version 1.0  
-- Updated by Andreas Kviby, 10Fingers AB  
-- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
   
function netTextListener( event )  
 if ( event.isError ) then  
 returnString = ""  
 else  
 returnString = event.response  
 end  
  
 print ("The returning text should be " .. returnString)  
  
 -- Now dispatch the event that will update the text  
 runtime:dispatchEvent({name = "updateText", newText = returnString});  
  
 return returnString  
end  
   
function getText(textID, languageID)  
 print("get network text: " .. textID .. " in language " .. languageID)  
   
 if (languageID == nil) then  
 languageID = app.languageID  
 end  
  
 -- Get the text requested and return it through the listener  
 network.request( app.netWorkTextURL .. "?languageID=" .. languageID .. "&textID=" .. textID.. "&appWebID=" .. app.appWEBID, "GET", netTextListener )  
end  

I put this together real quick and hope I didn’t miss anything.

Jeff

[import]uid: 14119 topic_id: 20989 reply_id: 82936[/import]

Sorry, forgot to capitalize Runtime.

smart = require("smartfunctions")  
   
-- Create the display object to be updated from the net  
local headline = display.newRetinaText("Loading...", 10, 35, native.systemFontBold, 16)  
   
-- Create an event listener for the downloaded text from the net  
local textUpdateFunction = function(e)  
headline.text = e.newText;  
end;  
   
-- Register a custom event called 'updateText' which will call 'textUpdateFunction'  
Runtime:addEventListener("updateText", textUpdateFunction)  
   
-- Call function to get the new text from the net  
smart.getText("proHeader","SE");  
   
   
--  
-- smartfunctions.lua  
--  
module(..., package.seeall)  
   
-- smartfunctions.lua  
-- version 1.0  
-- Updated by Andreas Kviby, 10Fingers AB  
-- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
   
function netTextListener( event )  
 if ( event.isError ) then  
 returnString = ""  
 else  
 returnString = event.response  
 end  
  
 print ("The returning text should be " .. returnString)  
  
 -- Now dispatch the event that will update the text  
 Runtime:dispatchEvent({name = "updateText", newText = returnString});  
  
 return returnString  
end  
   
function getText(textID, languageID)  
 print("get network text: " .. textID .. " in language " .. languageID)  
   
 if (languageID == nil) then  
 languageID = app.languageID  
 end  
  
 -- Get the text requested and return it through the listener  
 network.request( app.netWorkTextURL .. "?languageID=" .. languageID .. "&textID=" .. textID.. "&appWebID=" .. app.appWEBID, "GET", netTextListener )  
end  

Jeff

[import]uid: 14119 topic_id: 20989 reply_id: 82938[/import]

I have several controls that needs to use this functions but your solution seems smart but the text doesn’t get updated. Is it something with the text object that needs to be refreshed in some way to get the updated text? [import]uid: 22737 topic_id: 20989 reply_id: 83343[/import]

So I got the function to work properly with display.newText and scaling but not with retinaText…

[lua] local headline = display.newText(“Loading…”, 10, 35, native.systemFontBold, 32)
headline:setTextColor(255, 255, 255)
headline.xScale = headline.xScale*0.5
headline.yScale = headline.yScale*0.5
headline:setReferencePoint(display.TopLeftReferencePoint);

print(“set headline text”)

– Create an event listener for the downloaded text from the net
local textUpdateFunction = function(e)
print(“update the named text” … e.newText)
headline.text = e.newText;
headline:setReferencePoint(display.TopLeftReferencePoint);
headline.x = -100
print("object name: " … e.objectName)
–headline.y = 35
end;

– Register a custom event called ‘updateText’ which will call ‘textUpdateFunction’
Runtime:addEventListener(“updateText”, textUpdateFunction)

– Call function to get the new text from the net
smart.getText(“proHeader”,“SE”);[/lua]

So now the tricky issue here is to make the function more general so I can apply it on all text objects without the need to create separate functions for all textobjects in my app. Any ideas? [import]uid: 22737 topic_id: 20989 reply_id: 83349[/import]

If I understand your question correctly, you just need to create a listener table using something like the textID to index the table.

smart = require("smartfunctions")  
  
-- Create the display objects to be updated from the net  
local headline = display.newRetinaText("Loading...", 10, 35, native.systemFontBold, 16)  
local someOtherTextObject = display.newRetinaText("Loading...", 10, 35, native.systemFontBold, 16)  
  
-- Create an event listener for the downloaded text from the net  
local textUpdateFunction = function(e)  
 if (e.textID == "proHeader") then headline.text = e.newText;  
 elseif (e.textID == "text2") then someOtherTextObject.text = e.newText;  
 end;  
end;  
  
-- Register a custom event called 'updateText' which will call 'textUpdateFunction'  
Runtime:addEventListener("updateText", textUpdateFunction)  
  
-- Call function to get the new text from the net  
smart.getText("proHeader","SE");  
--  
-- smartfunctions.lua  
--  
module(..., package.seeall)  
  
-- smartfunctions.lua  
-- version 1.0  
-- Updated by Andreas Kviby, 10Fingers AB  
-- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
  
local function netTextListener( event, textID )  
 local returnString  
  
 if ( event.isError ) then  
 returnString = ""  
 else  
 returnString = event.response  
 end  
  
 print ("The returning text should be " .. returnString)  
  
 -- Now dispatch the event that will update the text  
 Runtime:dispatchEvent({name = "updateText", textID = textID, newText = returnString});  
  
 return returnString  
end  
  
local netTextListenerTable = {  
 ["proHeader"] = function(event) netTextListener(event,"proHeader"); end;  
 ["text2"] = function(event) netTextListener(event,"text2"); end;  
}  
  
function getText(textID, languageID)  
 print("get network text: " .. textID .. " in language " .. languageID)  
  
 if (languageID == nil) then  
 languageID = app.languageID  
 end  
  
 -- Get the text requested and return it through the listener  
 network.request( app.netWorkTextURL .. "?languageID=" .. languageID .. "&textID=" .. textID.. "&appWebID=" .. app.appWEBID, "GET", netTextListenerTable[textID] )  
end  

I haven’t tested for syntax errors but you should get the idea.

Jeff
[import]uid: 14119 topic_id: 20989 reply_id: 83403[/import]