How to create memory function

Hey,
So my app does basically a fingerscan, going to a new scene with an image randomly selected from an array. And then it has a try again so the user does it again to get a new one.

How can I create a memory function so that when he does it for the third time, a native alert pops out for rating? [import]uid: 95495 topic_id: 18462 reply_id: 318462[/import]

Set a variable and keep count, and when the count is at set level/number call a function to trigger the pop-up. [import]uid: 96411 topic_id: 18462 reply_id: 70827[/import]

can you give me a hand with the code? [import]uid: 95495 topic_id: 18462 reply_id: 70831[/import]

Anyone can tell me how to create a variable that keeps count?

I also need to know how to stop the rating pop-ups once the user already rated! [import]uid: 95495 topic_id: 18462 reply_id: 71307[/import]

[lua]Local counter = 0 – reserve memory space for counter variable and initialize it to zero

local function theWhatEverFunction ()

counter = counter +1
– do stuff in code

if counter < 3 then
– do stuff in code
else counter = 0 --reset counter back to Zero
– maybe do more stuff in code
end

end

[import]uid: 96411 topic_id: 18462 reply_id: 71310[/import]

I did that, but the counter always go back to 0 when it restarts the scene! [import]uid: 95495 topic_id: 18462 reply_id: 71314[/import]

Do this,

remove the local on counter.

OR TRY
maybe pass it to your function.
like this

local counter = 666

local function theWhatEverFunction(counter)

print (type(counter))
print ("counter = ", counter)

return TRUE
end

when you run the “OR TRY” code,
you should get the following in the debug terminal window

number
counter = 666

i use this code(print { type(variable)) } a lot to make sure I can access a variable
before I start mucking about with any “passed” variable

hope this helps :slight_smile:

[import]uid: 11094 topic_id: 18462 reply_id: 71318[/import]

I didn’t understand how this helps not reseting my counter!

I think it happens because it always reads local counter = 0 at the beginning of the code. It adds one count during the scene, but when I hit “try again” and then go back there, it reads that my count is 0 in the first place, ignoring what it was before. [import]uid: 95495 topic_id: 18462 reply_id: 71344[/import]

if you can pass into a function, you can then affect it. thus you can reset it.

when you start a new function it is considered a NEW block of code OR
what the programmers call a BLOB of code. All variables created in that function
are LOCAL to that function. And if you make any changes to these variables
that will not affect variables outside the function.

now you can create a GLOBAL variable or a table. like this, a1={}

a1 is a table at this point AND if you use print(type(a1))
you will see TABLE in the the DEBUG console.

now also try to print(a1), you will see the output in a DEBUG console
say something like “nil”

this means the TABLE a1 has nothing assigned to it.

So try assigning a value to it BEFORE entering the function. like so,

a1[1] = 12345

now use the debug statements print(a1[1]) to see if you echo that value out to
the DEBUG console. btw, you should see 12345 if you used the above print command.
you have got to grasp this programming concept.

however, you should only use GLOBAL variable(tables) sparingly.

buy the book programming in LUA(2nd edition) and actually try to copy the code
into a main.lua program and try to replicate the books results.

try to write simple functions and learn how to get variables/table into and out of functions.

learn all the different techniques and functionaity of lua.

now some people will give you code. but unless you take it apart and learn for it
and realise the intraciries of functions and how to use them, your just gonna get blurred eyed
everytime you wanna do something in corona. even the simple stuff like setting
a variable.

im not pretend to know everything. and god know I have asked for code and people have
giving me code thanks peach and al the others !!! but until you try to you will never truly learn. [import]uid: 11094 topic_id: 18462 reply_id: 71353[/import]

I appreciate your help, but it isn’t working…

I don’t think it’s a table matter. I put my counter inside the function to add 1 to it. But to set a counter, it has to be outside the function, and it has to be set to 0, so it can grow as the user goes to and from the scene. But it all comes back to the local counter = 0, and my counter is always printing 0, then 1 at the end of the scene. When I try again, it is 0 once again, and it turns into 1 and so on…my problem is to make it stay at 1, and then 2, and 3, etc [import]uid: 95495 topic_id: 18462 reply_id: 71412[/import]

Are you using Director? When the user hits “try again” what happens? Does it call a function that is in the same lua file?

If your are using director or multiple lua files, the simplest solution would be to use the “_G” variable. Don’t use local on it, just define something like “_G.counter=0” in your main.lua file and then use the if/then code david97 mentioned above. [import]uid: 94868 topic_id: 18462 reply_id: 71415[/import]

Unless you post your code, it’s difficult to help you.

-David
[import]uid: 96411 topic_id: 18462 reply_id: 71417[/import]

simple and perfect. Thanks, _G.counter works!

The alert i’m poping is to rate, so my last question is for when the user already rated it, how do I cancel this alert?

[import]uid: 95495 topic_id: 18462 reply_id: 71428[/import]

If you are just trying to close a web pop-up, just use native.cancelWebPopup().

If you are talking about only closing the popup after a user has rated the game, I’m not sure if that is possible. I don’t know if you can actually tell if a user has actually rated the app or not. If it is possible to get that information I do not know how to do it. [import]uid: 94868 topic_id: 18462 reply_id: 71449[/import]

Hm I’ve heard it is possible!! something about a boolean variable…but i’m not sure [import]uid: 95495 topic_id: 18462 reply_id: 71453[/import]