Saving and reading txt file

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Aduro91
Posts: 56
Joined: Sat Jul 22, 2023 8:49 pm

Saving and reading txt file

Post by Aduro91 » Tue Jan 28, 2025 6:58 pm

Hey everyone,

Working on allow general saved data to be retrieved in my app, both for game saves and storing whether the in-app purchase has been made.

I am following the guidlines here https://lessons.livecode.com/m/4069/l/1 ... e-appstore and have inputted the code as described:

Code: Select all

on preOpenStack
   loadNonConsumablePurchase
end preOpenStack

on loadNonConsumablePurchased
   local tPath
   put specialFolderPath("documents") & "/nonconsumablepurchased.txt" into tPath
   if there is a file tPath then
      set the cNonConsumablePurchased of this stack to url("file:" & tPath)
      put "PURCHASED" into fld "purchased" of cd "playmodes"
   end if
end loadNonConsumablePurchased


setProp cNonConsumablePurchased pValue
   set the cNonConsumablePurchased of this stack to pValue
   if pValue then
      put "PURCHASED" into fld "purchased" of cd "playmodes"
      
   else
      put "NOT PURCHASED" into fld "purchased" of cd "playmodes"
   end if
   saveNonConsumablePurchased
end cNonConsumablePurchased

on saveNonConsumablePurchased
   put the cNonConsumablePurchased of this stack into url("file:" & specialFolderPath("documents") & "/nonconsumablepurchased.txt")
end saveNonConsumablePurchased

on purchaseStateUpdate pPurchaseID, pProductID, pState
   switch pState
      case "paymentReceived" 
         answer "payment received!"
         offerPurchasedProduct pProductID
         mobileStoreConfirmPurchase pProductID
         mobileStoreDisablePurchaseUpdates
         break
      case "error"
         answer "Error occured during purchase handling:" & return & return & mobileStorePurchaseError(pPurchaseID)
         mobileStoreDisablePurchaseUpdates
         break
      case "invalidSKU"
         answer "Invalid SKU."
         mobileStoreDisablePurchaseUpdates
         break
      case "alreadyEntitled"
         answer "Already Owned."
         mobileStoreDisablePurchaseUpdates
         break
      case "restored"
         answer "restored"
         offerPurchasedProduct pProductID
         mobileStoreConfirmPurchase pProductID
         mobileStoreDisablePurchaseUpdates
         break
      case "cancelled"
         answer "Purchase Cancelled:" && pProductID
         mobileStoreDisablePurchaseUpdates
         break     
   end switch
end purchaseStateUpdate

 

on offerPurchasedProduct pProductID
   if pProductID is "FullGame" then
      set the cNonConsumablePurchased of this stack to true
   end if
end offerPurchasedProduct	

It all works to the point of allowing the in-app purchase, I see "purchased" in the field and all the dialogue boxes saying I've purchased etc.

However, when I quit the app and load it back up, I dont see "purchased" as I should, since I've asked it to check for the txt file and then put that into the field once found.

Any ideas?

Aduro91
Posts: 56
Joined: Sat Jul 22, 2023 8:49 pm

Re: Saving and reading txt file

Post by Aduro91 » Tue Jan 28, 2025 8:54 pm

Fixed it, not sure why but its failure in executing 'on openstack'. I put the 'loading' part into a button and then it worked. Shame that on openstack isn't working but I can work around that for now

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Saving and reading txt file

Post by SparkOut » Tue Jan 28, 2025 9:19 pm

Some operations require the engine to have loaded/initialised components before they are available for scripts to use. on preOpenStack is probably a little early for your command.
Maybe try

Code: Select all

on preOpenStack 
   send "loadNonConsumablePurchase" to me in 2 ticks --or 50 milliseconds or whatever works
end preOpenStack
that will give the engine a moment to initialise libraries that your code might require before the call by your command.

Aduro91
Posts: 56
Joined: Sat Jul 22, 2023 8:49 pm

Re: Saving and reading txt file

Post by Aduro91 » Tue Feb 04, 2025 8:44 pm

Thanks SparkOut!

Post Reply