Author Topic: How to save things ?  (Read 9336 times)

0 Members and 1 Guest are viewing this topic.

Offline MacBernick

  • LV3 Member (Next: 100)
  • ***
  • Posts: 93
  • Rating: +3/-0
    • View Profile
How to save things ?
« on: January 06, 2014, 01:52:12 pm »
How do you store high scores, game states, or anything you want to keep safe for an arbitrary amount of time. EXPORTed variable are not reliable for this use, and I can't find a way to create an actual User Variable programmatically. Pre-defined vars are the way ? Sounds odd to me...
Thank you for any advice !

Offline Han

  • LV3 Member (Next: 100)
  • ***
  • Posts: 62
  • Rating: +7/-0
    • View Profile
Re: How to save things ?
« Reply #1 on: January 06, 2014, 07:42:02 pm »
Just remove the "EXPORT" declaration and leave everything as is. The variable will retain its contents, but only the programs within the source file in which the variable was declared will have access to it.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: How to save things ?
« Reply #2 on: January 06, 2014, 09:54:14 pm »
Yup, removing EXPORT before variables makes the variable "super global". I don't know how to say it, but there are three types of variables on the HP Prime:

-Local: Declared at the start of a function, only useable in that function and cleared from memory once returning from the function
-Global: Declared with EXPORT at the start of the program, useable through the entire program. The variable is deleted once quitting the program.
-External: Declared without the EXPORT command, still at the start of the program. This type remains intact once exiting the program and can be used anywhere.

In TI and Casio BASIC, however, we can edit variables as we wish, so people can fake highscores and cheat in long RPGs. Even if only the source HP PPL program has access to a declared variable, can that variable still be edited with an on-calc editor or something? Also another question: When using ON+SYMB or pressing the RESET button, are variables cleared? If so, then is there a way to ensure that variables are reset-proof? Else, this might be a problem for role-playing/adventure games like Legend of Zelda or Final Fantasy. Since the HP Prime isn't that stable, we sometimes have to reset it to bring back normal behavior, and it would suck to have to backup our progress on a computer every two hour or so...
« Last Edit: January 06, 2014, 09:56:23 pm by DJ Omnimaga »

Offline MacBernick

  • LV3 Member (Next: 100)
  • ***
  • Posts: 93
  • Rating: +3/-0
    • View Profile
Re: How to save things ?
« Reply #3 on: January 07, 2014, 08:15:51 am »
I wasn't aware of this 'superglobal' thing, thank you ^^ !

Offline Han

  • LV3 Member (Next: 100)
  • ***
  • Posts: 62
  • Rating: +7/-0
    • View Profile
Re: How to save things ?
« Reply #4 on: January 07, 2014, 09:15:56 pm »
Yup, removing EXPORT before variables makes the variable "super global". I don't know how to say it, but there are three types of variables on the HP Prime:

-Local: Declared at the start of a function, only useable in that function and cleared from memory once returning from the function
-Global: Declared with EXPORT at the start of the program, useable through the entire program. The variable is deleted once quitting the program.
-External: Declared without the EXPORT command, still at the start of the program. This type remains intact once exiting the program and can be used anywhere.

In TI and Casio BASIC, however, we can edit variables as we wish, so people can fake highscores and cheat in long RPGs. Even if only the source HP PPL program has access to a declared variable, can that variable still be edited with an on-calc editor or something? Also another question: When using ON+SYMB or pressing the RESET button, are variables cleared? If so, then is there a way to ensure that variables are reset-proof? Else, this might be a problem for role-playing/adventure games like Legend of Zelda or Final Fantasy. Since the HP Prime isn't that stable, we sometimes have to reset it to bring back normal behavior, and it would suck to have to backup our progress on a computer every two hour or so...

This is incorrect. LOCAL is restricted to inside a BEGIN END; pair. EXPORT makes global variables accessible system-wide. Removing EXPORT creates a variable "global" only to the source file. Programs from a different source file cannot see/use these variables, nor can the user. Whether EXPORT'ed or not, both global types remain and retain their values after programs complete their execution until next compile-by-exiting-the-editor event. An ON-SYMB does not clear global variables (not their existence nor their contents).

http://www.hpmuseum.org/forum/thread-215.html

While a non-EXPORT'ed global variable cannot be directly edited by the user from the command line, there is nothing to prevent the user from manually editing the source to "cheat" in games provided they know how to make the modification.
« Last Edit: January 07, 2014, 09:18:22 pm by Han »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: How to save things ?
« Reply #5 on: January 07, 2014, 10:40:51 pm »
Oh that's actually what I meant by LOCAL. I just thought that BEGIN/END pairs were called "functions", like in most computer languages. I didn't know that non-exported global vars retained their values upon exiting, though. This might explain the issues I had with Supersonic Ball title screen at first.

Offline MacBernick

  • LV3 Member (Next: 100)
  • ***
  • Posts: 93
  • Rating: +3/-0
    • View Profile
Re: How to save things ?
« Reply #6 on: January 08, 2014, 11:31:41 am »
Looks like non-exported globals works like exported ones. It stores values for some time but just entering and exiting program code reset them. That's not a very reliable way to store things.

EDIT : in fact you can read an modify a user var in a program, but not create one, or I didn't find how at least...
« Last Edit: January 08, 2014, 12:33:23 pm by MacBernick »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: How to save things ?
« Reply #7 on: January 08, 2014, 01:48:23 pm »
Is there a way to prevent an user from accidentally entering the program editor? When exiting programs I often found myself landing there and I often hit ENTER instead of Run by mistake to run a program. This can be a serious problem for highscores and save files, because the user is often at risk of opening the program editor by mistake, resulting in losing all his highscores/saved game progress.

Is there an alternative or will HP role playing games, for example, have to rely on password saves like in some NES and SNES games (which can be easy to hack if not programmed correctly)?
« Last Edit: January 08, 2014, 01:50:00 pm by DJ Omnimaga »

Offline MacBernick

  • LV3 Member (Next: 100)
  • ***
  • Posts: 93
  • Rating: +3/-0
    • View Profile
Re: How to save things ?
« Reply #8 on: January 08, 2014, 02:10:42 pm »
It should be possible by making an App maybe, as apps have their own variable name space and settings for each app can be stored (according to inline help).
« Last Edit: January 08, 2014, 02:11:23 pm by MacBernick »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: How to save things ?
« Reply #9 on: January 10, 2014, 12:02:21 am »
Ok, but if someone accidentally opens the code in the editor (from the App thing there) will they still be wiped out?

Offline MacBernick

  • LV3 Member (Next: 100)
  • ***
  • Posts: 93
  • Rating: +3/-0
    • View Profile
Re: How to save things ?
« Reply #10 on: January 10, 2014, 10:13:01 am »
I don't know, didn't test it yet. I first thought that any program could be turned into an app but it looks a bit more complicated than that.

For me the best solution would be to allow User vars creation programmatically.

Offline Han

  • LV3 Member (Next: 100)
  • ***
  • Posts: 62
  • Rating: +7/-0
    • View Profile
Re: How to save things ?
« Reply #11 on: January 10, 2014, 11:51:33 am »
Ok, but if someone accidentally opens the code in the editor (from the App thing there) will they still be wiped out?

Haven't given it much thought, but one way is to put the save code in a different source file. The way the program catalog works is that whatever source file is run will get placed toward the top of the program catalog. If you keep the scoring parts separate it is less likely going to get opened up.

The best way is to have HP have an "archive" ability to prevent source files from being opened easily. That way, if you want to view it, you have to unarchive it first like on the TI's
« Last Edit: January 10, 2014, 11:54:58 am by Han »

Offline MacBernick

  • LV3 Member (Next: 100)
  • ***
  • Posts: 93
  • Rating: +3/-0
    • View Profile
Re: How to save things ?
« Reply #12 on: January 10, 2014, 12:58:10 pm »
In my very humble opinion, trying to prevent people to look at programs code they installed on their calc only for this reason seems a bit disproportionate. I think the way the OS works is broken at the moment, and finding workarounds now is just like putting bandages on a wooden peg (if that makes any sense in english ^^ ).

Reading other's code (and tweak it) is the best way to learn. People will want to read the code, and putting a sword of Damocles saying "Don't open or you'll loose your score/progression/precious data" is not the way I want to make programs.

Prime is young enough and HP people is still working on it, let's report and claim, I'm sure a solution will be found (I'll stick with Users vars, until something better is suggested :p ).

For my part I'll use a Home List for now, with a warning at first launch. I know it's not ideal at all, but at least user can save their list in a user variable and restore it at will. Best compromise I found.
« Last Edit: January 10, 2014, 01:03:41 pm by MacBernick »

Offline Han

  • LV3 Member (Next: 100)
  • ***
  • Posts: 62
  • Rating: +7/-0
    • View Profile
Re: How to save things ?
« Reply #13 on: January 10, 2014, 04:54:47 pm »
My suggestions were merely so that users do not end up resetting stored data. Cheaters will always find ways to cheat in games. I'm all for learning by viewing other people's code. Likewise with the archive idea. It is way to easy to delete the contents of a program at the moment, not to mention the entire list of programs in the catalog. At least with the archive feature it prevents accidents like this. The archive feature does not prevent users from viewing code -- they just have to take a few extra steps to do it so that they don't end up with a null-content program.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Re: How to save things ?
« Reply #14 on: January 10, 2014, 05:21:57 pm »
Yeah I don't like the idea of closed-source myself since it prevents people from learning as much. Such password protection would only be a las resort solution to the accidental openings of source. I prefer that HP just fixes super global vars so they remain intact until reformatting the calc.

Right now, a Zelda game for the Prime is impossible without weird workarounds