Alerting a series of messages does not work.

What I’m trying to do seems to be very simple, but something is going wrong.

I’m trying to alert (system alert) a few messages to the user, one after the other, from a list of messages, where clicking on the “ok” button on the alert message window would initiate the next message.

The code is very simple, but for some reason it does not work past the second message:

  local messages\_table = {}; table.insert(messages\_table,{"A","AAA",{"OK"}}); table.insert(messages\_table,{"B","BBB",{"OK"}}); table.insert(messages\_table,{"C","CCC",{"OK"}}); table.insert(messages\_table,{"D","DDD",{"OK"}});   local function trace(title,info,buttons)     alert = native.showAlert(title, info, buttons,alert\_listener); end   function alert\_listener(event)     if event.action == "clicked" then         if #messages\_table \> 0 then             table.remove(messages\_table,1);             trace(messages\_table[1][1], messages\_table[1][2], messages\_table[1][3]);         else             print("DONE");         end     end end   trace(messages\_table[1][1], messages\_table[1][2], messages\_table[1][3]);

This code should, by my calculation, alert 4 message, then print “done” when the table of messages is empty.

The first message fires just fine, then the second is fired just fine, and there it stops, no more messages, no “done”, no error message, nothing… almost as if after the second message the listener stops working, or is not taken into account when creating the alert.

Can someone see the problem here and why it does not work??

@formatic

For me, your code showed 4 alerts, then generated an error when trying to run your trace function when the table was empty.  I moved a couple lines around, and now this runs on my sim without errors:

[lua]

local messages_table = {};
table.insert(messages_table,{“A”,“AAA”,{“OK”}});
table.insert(messages_table,{“B”,“BBB”,{“OK”}});
table.insert(messages_table,{“C”,“CCC”,{“OK”}});
table.insert(messages_table,{“D”,“DDD”,{“OK”}});
 
local function trace(title,info,buttons)
    alert = native.showAlert(title, info, buttons,alert_listener);
end
 
function alert_listener(event)
    if event.action == “clicked” then
        
         table.remove(messages_table,1);
         
        if #messages_table > 0 then
              
            trace(messages_table[1][1], messages_table[1][2], messages_table[1][3]);
         
        else
            print(“DONE”);
        end
    end
end
 
trace(messages_table[1][1], messages_table[1][2], messages_table[1][3]);

[/lua]

I’m running the fixed code you posted (noticed the problem which gave you the error, I never seen it, since I never gotten to that point…), but still, when I run it, I get to the second alert and it stops there… I’m running corona on a windows machine, maybe that is the problem? also, when compiling this code, on my device it does the same, only 2 alerts and then nothing.

I am on a Mac using 2013.1031   I’ve experienced differences between simulators, and devices as well. :slight_smile:

A few things to try ?

* make the alert variable a local variable outside your trace function, and set it to nil in your alert_listener function

* pre-declare your alert_listener function before the trace function with an empty shell

[lua]alert_listener = function() end[/lua]

I get a wierd behavior on OSX:  After the first alert, when I click Ok, the simulator screen disappears.  I see the alert for ‘B’, click Ok, the simulator window pops back into existance, with the ‘c’ alert open, i click that, the simulator window dissappears, and I see the ‘D’ window… one last Ok brings the sim back, and ‘DONE’ in the terminal

Hope that helps :slight_smile:

Yeah, I know the issue of calling function where they still don’t exist in the code, I have tried that before, and still, the alert, on my windows machine and android device work until the second alert, then simply goes silent, as a last resort I’ll try it off my mac, but I would love not to go there as my more natural working environment is windows.

Maybe someone from Corona can comment on this issue and what might me the technical problem launching these messages on a windows machine and on android platform?

I believe there are platform-specific limitations on Windows and Android, specifically, when you attempt to display an alert *while* there’s already an alert. This is why we put alerts in the ‘native’ library because there are inevitably minor differences and we try to contain those in this library.

One thing you could try (albeit a little hacky) is to use timer.performWithDelay. 

[lua]

timer.performWithDelay( 1, function()

    trace(messages_table[1][1], messages_table[1][2], messages_table[1][3])

end )

[/lua]

My guess is that on those platforms you cannot show a new alert until the previous one is dismissed. The delay would allow the listener to return, thus ending the alert session, before queuing up the next one. Hope that helps!

Thanks Walter, just got home and was about to write about this workaround I found to be working, which is to use timer to alert my messages with a 1 ms delay, and here I see you beat me to it… and I thought I would wow the world with my creative solution and you stole my fire… :wink:

Thank you for taking the time to respond just the same!!

Great minds think alike :slight_smile:

@formatic

For me, your code showed 4 alerts, then generated an error when trying to run your trace function when the table was empty.  I moved a couple lines around, and now this runs on my sim without errors:

[lua]

local messages_table = {};
table.insert(messages_table,{“A”,“AAA”,{“OK”}});
table.insert(messages_table,{“B”,“BBB”,{“OK”}});
table.insert(messages_table,{“C”,“CCC”,{“OK”}});
table.insert(messages_table,{“D”,“DDD”,{“OK”}});
 
local function trace(title,info,buttons)
    alert = native.showAlert(title, info, buttons,alert_listener);
end
 
function alert_listener(event)
    if event.action == “clicked” then
        
         table.remove(messages_table,1);
         
        if #messages_table > 0 then
              
            trace(messages_table[1][1], messages_table[1][2], messages_table[1][3]);
         
        else
            print(“DONE”);
        end
    end
end
 
trace(messages_table[1][1], messages_table[1][2], messages_table[1][3]);

[/lua]

I’m running the fixed code you posted (noticed the problem which gave you the error, I never seen it, since I never gotten to that point…), but still, when I run it, I get to the second alert and it stops there… I’m running corona on a windows machine, maybe that is the problem? also, when compiling this code, on my device it does the same, only 2 alerts and then nothing.

I am on a Mac using 2013.1031   I’ve experienced differences between simulators, and devices as well. :slight_smile:

A few things to try ?

* make the alert variable a local variable outside your trace function, and set it to nil in your alert_listener function

* pre-declare your alert_listener function before the trace function with an empty shell

[lua]alert_listener = function() end[/lua]

I get a wierd behavior on OSX:  After the first alert, when I click Ok, the simulator screen disappears.  I see the alert for ‘B’, click Ok, the simulator window pops back into existance, with the ‘c’ alert open, i click that, the simulator window dissappears, and I see the ‘D’ window… one last Ok brings the sim back, and ‘DONE’ in the terminal

Hope that helps :slight_smile:

Yeah, I know the issue of calling function where they still don’t exist in the code, I have tried that before, and still, the alert, on my windows machine and android device work until the second alert, then simply goes silent, as a last resort I’ll try it off my mac, but I would love not to go there as my more natural working environment is windows.

Maybe someone from Corona can comment on this issue and what might me the technical problem launching these messages on a windows machine and on android platform?

I believe there are platform-specific limitations on Windows and Android, specifically, when you attempt to display an alert *while* there’s already an alert. This is why we put alerts in the ‘native’ library because there are inevitably minor differences and we try to contain those in this library.

One thing you could try (albeit a little hacky) is to use timer.performWithDelay. 

[lua]

timer.performWithDelay( 1, function()

    trace(messages_table[1][1], messages_table[1][2], messages_table[1][3])

end )

[/lua]

My guess is that on those platforms you cannot show a new alert until the previous one is dismissed. The delay would allow the listener to return, thus ending the alert session, before queuing up the next one. Hope that helps!

Thanks Walter, just got home and was about to write about this workaround I found to be working, which is to use timer to alert my messages with a 1 ms delay, and here I see you beat me to it… and I thought I would wow the world with my creative solution and you stole my fire… :wink:

Thank you for taking the time to respond just the same!!

Great minds think alike :slight_smile: