Save/Load Data Issue

Hello,

I’m trying to use Peach Pellen’s Save/Load Data Tutorial Code to implement my own system of Locking/Unlocking levels. However I keep getting the following error.

Project/Version1/levelselect.lua:142: attempt to perform arithmetic on field ‘twolock’ (a nil value)

I tried everything I could possibly think of and then out of curiosity, I copied the tutorial code into my file exactly as Peach had it. I still got the error. The only difference between the example and mine, is that the example Peach gave went main.lua --> menu.lua. My project goes main.lua–> menu.lua–> modeselect.lua–> levelselect.lua. All the code for saving data is inside levelselect.lua for me while it is inside menu.lua in Peach’s example. Does that make a difference? Line 2 below is the line that the error is referencing to. Any help on this would be appreciated.

[blockcode]local function seticontwo (event)
if _G.twolock-0 == 0 then
local leveltwoicon = display.newImageRect (“images/levelblocks/level2locked.png”, 45, 43)
leveltwoicon.x = 130
leveltwoicon.y = 40
localGroup:insert(leveltwoicon)
elseif _G.twolock-0 == 1 then
local leveltwoicon = display.newImageRect (“images/levelblocks/level2.png”, 45, 43)
leveltwoicon.x = 130
leveltwoicon.y = 40
localGroup:insert(leveltwoicon)
leveltwoicon:addEventListener(“tap”, gototwo)
end
end
seticontwo()
[/blockcode]

Thanks. [import]uid: 47722 topic_id: 13715 reply_id: 313715[/import]

Exactly, it says that there is no variable that is of type global and called twolock.

If you have a local twolock, remove the local from there,

alternatively, you can use the PropertyBag to lock and unlock your levels.

cheers,

?:slight_smile:
[import]uid: 3826 topic_id: 13715 reply_id: 50371[/import]

Really I should have been using tonumber() rather than -0 there as well >.
Make sure, as Jayantv said more or less, that )G.twolock is declared somewhere first and isn’t local, just like in the sample :slight_smile:

Peach [import]uid: 52491 topic_id: 13715 reply_id: 50392[/import]

Still not working. There are no locals in front of anything and I’m not sure what you mean by declaring the variable first. [import]uid: 47722 topic_id: 13715 reply_id: 50415[/import]

Isn’t twolock being declared in line 19 of the code below?

[blockcode]
local function resumeStart()
local path = system.pathForFile( “ourdata.txt”, system.DocumentsDirectory )
local file = io.open( path, “r” )

if file then
print(“Loading our data…”)
local contents = file:read( “*a” )
– Loads our data

local prevState = explode(", ", contents)

_G.onelock = prevState[1]
_G.twolock = prevState[2]

io.close( file )

else
_G.onelock = 1
_G.twolock = 0
end
end
– Separates the variables into a table
[/blockcode] [import]uid: 47722 topic_id: 13715 reply_id: 50416[/import]

A classic example why _G should not be meddled with. @peach YouTube want to get *that* particular example/tutorial off your site. A beginner should never play with _G for that matter unless really required, no one must reference the _G directly.

Try the PropertyBag, easier to use and understand.

Cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13715 reply_id: 50487[/import]

Jayant,

I know you LOVE your PropertyBag class - you did a great job with it and it’s well worth using, certainly.

That said, I’m not sure if “YouTube” was a typo or not, but I missed what you meant there.

As to beginners playing with _G. - playing is a good way to learn. It’s not a tutorial aimed at total newbies, either, as you’ll note by the fact it is not in “Newbie Tutorials” :wink:

I agree PropertyBag is a FAR easier way for newbies to handle this sort of thing - but I will (of course) be leaving my tutorial where it is; a lot of what I write is about playing around, learning and experimenting and I like it that way :slight_smile:

Peach :slight_smile: [import]uid: 52491 topic_id: 13715 reply_id: 50562[/import]

It was the iPhone that autocorrected you might to you tube. Crazy stuff autocorrect on the iPhone.

And how Unmindful of me requesting you to remove some code from your site, sorry that was not part of your official work at Ansca but more of your own as you said result of your experimenrations and on your own site. It is definitely up to the users to choose what they attempt to use.

Many search for things but ignore the level and end up with more questions than solutions.

I suggest propertyBag not because I love it or the fact that I wrote it, but for the fact that it works without complications. Similar to that OS the crawlspace library, they have some save load functions too.

At the end, there is all types of info available on the Internet and if it weren’t, would we get to learn? Absolutely not.

?:slight_smile: [import]uid: 3826 topic_id: 13715 reply_id: 50591[/import]

Hey Jayant,

I didn’t mean that you love PropertyBag because you wrote it, sorry if that wasn’t clear.

As I said, I agree in this case that PropertyBag is a perfect solution :slight_smile:

I also agree with you that newbies probably shouldn’t go trying to use _G. without knowing what it does, but that playing around with it is OK.

Techority is in no way affiliated with Ansca (beyond being a huge supporter of Corona ;)) and more about learning, experimenting and having fun with it.

No offense taken, anyway :slight_smile:

Peach [import]uid: 52491 topic_id: 13715 reply_id: 50706[/import]

Hello guys,

I was just catching up on my email and saw how interesting this post got. I want to address a few things.

@Jayantv I ended up using propertyBag. It’s very simple and easy to use. It allows me to do everything I was trying to do. So thank you very much for that.

@Peach Your tutorials are incredible. I can’t say enough good things about them. Please don’t stop doing them and certainly do not remove the Save/Load data tutorial. Trust me, your Save/Load Data tutorial made perfect sense to me when I was thinking it through. Its just for some reason that when I tried to code it, it gave a persistent error. Without your tutorials, my app wouldn’t be where it is today and I thank you very much for your hard work. Are there any future tutorials coming soon?

Thanks again to both of you. I really appreciate it.

Thanks,
Dale [import]uid: 47722 topic_id: 13715 reply_id: 51051[/import]

Thanks Dale,

Don’t worry - I’ll always make tutorials for Corona, even though lately I haven’t made as many :wink: (I’ve been busy!)

I can’t say for sure why you got an error without combing through all your code, but I’m pleased PropertyBag has been able to make it easier.

Honestly, if I wasn’t so set in my ways I’d probably be using it too :wink:

Peach :slight_smile: [import]uid: 52491 topic_id: 13715 reply_id: 51081[/import]