I have an app with 2 different storyboard scenes that need to have a listener for store transactions.
Is there a way to only have one listener or do I have to call store.init in both scenes?
I have an app with 2 different storyboard scenes that need to have a listener for store transactions.
Is there a way to only have one listener or do I have to call store.init in both scenes?
Hi @bmelke,
First off, I suggest you use Composer, if you’re not already (so, not Storyboard). Next I need to ask you, which store are you using? Apple IAP? Google IAP?
Brent
Thank you for you reply Brent. I am now migrating to composer, just having a little trouble with overlays. I am using the apple store.
OK, thanks. I recommend that you require() the store in main.lua, and in that, you do a store.init() which points to the listener function that will handle store requests/events. Then, in other modules/scenes, you also require() store, and it should internally reference back to the same listener function which you did the initI() on.
You definitely should not do multiple init() calls in different places. Just do that once.
Brent
That is the way I have it now. But because I am using storyboard, the store.init is called in my first scene. Later in a purchase scene I try to do a store.purchase but the store listener is never called. I have not destroyed the main scene. If I do a store.init on both scenes (main and purchase) and I have a listener in both it works. I agree with you that this is not a good idea.
I assume you require() the store in both modules under the same namespace?
Brent
You are correct.
Coming into this a bit late. This is pretty straight forward. You only need one listener… though technically store.loadProducts() uses a different function.
In main.lua:
local store = require(“store”)
local function transactionListener( event )
…
end
store.init( “storename”, transactionListener )
If you need to deal with loading products, you would also do that in main.lua.
Then in any scene that you need to purchase or restore:
local store = require(“store”)
…
store.purchase( { “com.yourdomain.yourapp.yourproduct” } ) – Apple
store.purchase( “com.yourdomain.yourapp.yourproduct” ) – Google, Amazon
or
store.restore()
I would never init the store in a scene. They tend to get deleted and you wouldn’t want that function getting knocked out of memory. Now your purchase may need to make something happen in your current scene (unlock something, add coins, etc.) You have to have a way to communicate from the function in main.lua to the modules that may need the information.
There are multiple ways to manage that: globals (generally bad), a data module to emulate globals in a safe way. See: http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/, use the composer object which is available across any module that requires it. We have setter/getter functions for setting key-value pairs in a safe way.
Rob
Just to be clear… “main.lua” is not a scene, it’s a setup module and you call your first scene from main.lua. If you’re trying to make main.lua a scene, that could create issues.
You should also be looking for errors on your device’s console log. If you need to know how to do that please read:
http://docs.coronalabs.com/guide/basics/debugging/index.html
Rob
Thanks Rob, that is a very clean solution.
Hi @bmelke,
First off, I suggest you use Composer, if you’re not already (so, not Storyboard). Next I need to ask you, which store are you using? Apple IAP? Google IAP?
Brent
Thank you for you reply Brent. I am now migrating to composer, just having a little trouble with overlays. I am using the apple store.
OK, thanks. I recommend that you require() the store in main.lua, and in that, you do a store.init() which points to the listener function that will handle store requests/events. Then, in other modules/scenes, you also require() store, and it should internally reference back to the same listener function which you did the initI() on.
You definitely should not do multiple init() calls in different places. Just do that once.
Brent
That is the way I have it now. But because I am using storyboard, the store.init is called in my first scene. Later in a purchase scene I try to do a store.purchase but the store listener is never called. I have not destroyed the main scene. If I do a store.init on both scenes (main and purchase) and I have a listener in both it works. I agree with you that this is not a good idea.
I assume you require() the store in both modules under the same namespace?
Brent
You are correct.
Coming into this a bit late. This is pretty straight forward. You only need one listener… though technically store.loadProducts() uses a different function.
In main.lua:
local store = require(“store”)
local function transactionListener( event )
…
end
store.init( “storename”, transactionListener )
If you need to deal with loading products, you would also do that in main.lua.
Then in any scene that you need to purchase or restore:
local store = require(“store”)
…
store.purchase( { “com.yourdomain.yourapp.yourproduct” } ) – Apple
store.purchase( “com.yourdomain.yourapp.yourproduct” ) – Google, Amazon
or
store.restore()
I would never init the store in a scene. They tend to get deleted and you wouldn’t want that function getting knocked out of memory. Now your purchase may need to make something happen in your current scene (unlock something, add coins, etc.) You have to have a way to communicate from the function in main.lua to the modules that may need the information.
There are multiple ways to manage that: globals (generally bad), a data module to emulate globals in a safe way. See: http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/, use the composer object which is available across any module that requires it. We have setter/getter functions for setting key-value pairs in a safe way.
Rob
Just to be clear… “main.lua” is not a scene, it’s a setup module and you call your first scene from main.lua. If you’re trying to make main.lua a scene, that could create issues.
You should also be looking for errors on your device’s console log. If you need to know how to do that please read:
http://docs.coronalabs.com/guide/basics/debugging/index.html
Rob
Thanks Rob, that is a very clean solution.