Where is the best general storage for variables?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 2
- Joined: Thu Dec 28, 2017 3:01 am
Where is the best general storage for variables?
So I've just started to learn LiveCode, but in a different life I was competent with HyperTalk.
I'm trying to write a point and click game. Where is the best place to store my variables? Do I attach them all to a random object as custom properties, and then need to call them all up from that object?
I'm trying to write a point and click game. Where is the best place to store my variables? Do I attach them all to a random object as custom properties, and then need to call them all up from that object?
Re: Where is the best general storage for variables?
Well, I would say it would depend really on what the variable is going to hold. If the data isn't needed past a handler, then creating them on the fly is how I'd go. It needed across a few handlers, local at the top of the script. If it is something you needed stored and easy access to, a cprop will do it, but I wouldn't bother making a separate object for that.
Jacque has some rare insights about this stuff, as do many others, but thats my take on it.
Jacque has some rare insights about this stuff, as do many others, but thats my take on it.
-
- VIP Livecode Opensource Backer
- Posts: 9580
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Where is the best general storage for variables?
Hi.
Several old HC'ers here. But I am not sure what the question is.
LC has local and global variables, just like HC. "Where" did you "store" those?
LC also has script local variables, which are like globals, but restricted to the script in which they are declared. As in a HC global, they must be declared ABOVE any handler that calls them.
A custom property is like a global, but survives sessions, and is called like a property, not like a variable. These are powerful tools indeed.
All that said, what did you mean by your post title? Did you actually mean to say, "What are the nature of variables in LC?
Craig Newman
Several old HC'ers here. But I am not sure what the question is.
LC has local and global variables, just like HC. "Where" did you "store" those?
LC also has script local variables, which are like globals, but restricted to the script in which they are declared. As in a HC global, they must be declared ABOVE any handler that calls them.
A custom property is like a global, but survives sessions, and is called like a property, not like a variable. These are powerful tools indeed.
All that said, what did you mean by your post title? Did you actually mean to say, "What are the nature of variables in LC?
Craig Newman
-
- Posts: 2
- Joined: Thu Dec 28, 2017 3:01 am
Re: Where is the best general storage for variables?
Perhaps I don't know enough to ask the right questions.
When I say I was "competent" in Hypertalk, I mean that I could make it do what I wanted for the most part, but I was self-taught with little documentation. I used to store all the data I needed in hidden fields and then access it by counting words -- I guess a version of an array?
I understand global variables and that's how I started to work with LC, but I see warnings about this, and advice to use custom properties instead.
In my game (which is really only a project for myself to get into LC) I need to keep track of various inventory that the player picks up, as well as the changes that those inventory items make to the player's abilities (keys, "magic swords", etc.). I'm keeping track of the player's health and other stats with global variables, but again, I see warnings that these are to be avoided (and it does get tiresome to declare them over and over again).
When I say I was "competent" in Hypertalk, I mean that I could make it do what I wanted for the most part, but I was self-taught with little documentation. I used to store all the data I needed in hidden fields and then access it by counting words -- I guess a version of an array?
I understand global variables and that's how I started to work with LC, but I see warnings about this, and advice to use custom properties instead.
In my game (which is really only a project for myself to get into LC) I need to keep track of various inventory that the player picks up, as well as the changes that those inventory items make to the player's abilities (keys, "magic swords", etc.). I'm keeping track of the player's health and other stats with global variables, but again, I see warnings that these are to be avoided (and it does get tiresome to declare them over and over again).
-
- VIP Livecode Opensource Backer
- Posts: 9802
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Where is the best general storage for variables?
A custom property will survive across sections IF it's in a separate stack file from the standalone. Modern OSes don't allow apps to modify themselves.
Some of the data storage options we have in LiveCode:
- Text files
- XML
- YAML
- JSON
- Stack files (fields, custom props)
- LSON files (encoded arrays)
- DBs (SQLite, MySQL, postgreSQL in all editions; plus Oracle in Business Edition)
- Any custom text and/or binary format you want to take the time to devise
For small amounts of data to be used in vars, LSON may be a good bet. You could keep all your elements in one array, and then just run that through arrayEncode before writing to disk:
Note that the constant passed to the specialFolderPath function will depend on where you actually want the data saved - see the Dictionary entry for that function for a list of options.
Also note note the error handling after the write attempt, which includes a call to sysError. All I/O (files or network) have many ways they can go wrong, so it's a good habit to check "the result" after such commands; sysError will tell you the specific error number the OS reported back to LiveCode, very useful for diagnosing when things go wrong.
Some of the data storage options we have in LiveCode:
- Text files
- XML
- YAML
- JSON
- Stack files (fields, custom props)
- LSON files (encoded arrays)
- DBs (SQLite, MySQL, postgreSQL in all editions; plus Oracle in Business Edition)
- Any custom text and/or binary format you want to take the time to devise
For small amounts of data to be used in vars, LSON may be a good bet. You could keep all your elements in one array, and then just run that through arrayEncode before writing to disk:
Code: Select all
put "foo" into tMyVar["SomeLabel"]
put "bar" into tMyVar["SomeOtherLabel"]
put arrayEncode(tMyVar) into tMyData
put tMyData into url ("binfile:"& specialFolderPath("desktop")&"/MyDatafFile.lson")
if the result is not empty then
answer "Couldn't save data: "& the resullt &" ("& sysError() &")"
exit to top
end if
Also note note the error handling after the write attempt, which includes a call to sysError. All I/O (files or network) have many ways they can go wrong, so it's a good habit to check "the result" after such commands; sysError will tell you the specific error number the OS reported back to LiveCode, very useful for diagnosing when things go wrong.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Where is the best general storage for variables?
Boy do I know THAT feelingJonathan Mayshar wrote: ↑Thu Dec 28, 2017 5:12 amPerhaps I don't know enough to ask the right questions.
It is no problem though, just expect questions for clarity. I'm still lacking enough to be quite dangerous in my answers
Globals I try to avoid myself, I'm sure you've seen the lesson on livecode's lessons pages, but in case you haven't, it starts here and goes on to here. Every language I've ever used preached the same lesson, use globals sparingly, if at all.
Lc has a good solution in custom properties, you only have to set it up once, you can access it from anywhere. That isn't to say they are the solution for all things, though. Go through those 2 lessons, and you may find new questions to ask.
Richard snuck in while I was composing, but his advice is also dead on. For a simple game that your learning with, if you go to external files, I'd certainly suggest starting out with plain text. If you divide the information into lines, you can refer to that as if it were a field in a card, either using line numbers flat out, or even searching the file using Lc's very good text functions.
-
- VIP Livecode Opensource Backer
- Posts: 9802
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Where is the best general storage for variables?
Plain text is seductive, because it's easy for us to read and comprehend directly.bogs wrote: ↑Thu Dec 28, 2017 6:31 amRichard snuck in while I was composing, but his advice is also dead on. For a simple game that your learning with, if you go to external files, I'd certainly suggest starting out with plain text. If you divide the information into lines, you can refer to that as if it were a field in a card, either using line numbers flat out, or even searching the file using Lc's very good text functions.
And if your values are plain text and have no Return chars, something like this:
Code: Select all
label1=value1
label2=value2
label3=value3
Code: Select all
split tData with cr and "="
Depending on what your app does, it doesn't take long to find the weaknesses in plain delimited text.
With arrays you can key any number of elements, and the values can be anything, a short string, multi-line chunks, even binary data.
The only challenge with arrays is that they're not a contiguous bytestream like text, so they need to be converted to and from a bytestream format. But thankfully LC includes arrayEncode and arrayDecode, making that a one-liner.
There are many other options as well, but lately I find myself using LSON files as my go-to format for their unique balance of ease, efficiency, and flexibility.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 9580
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Where is the best general storage for variables?
Aha.
So the original question was in fact: "What is the best way to store data?", or rather, "What options are available in LC to store data?"
I use custom props almost exclusively, sometimes in array form.
Craig
So the original question was in fact: "What is the best way to store data?", or rather, "What options are available in LC to store data?"
I use custom props almost exclusively, sometimes in array form.
Craig
Re: Where is the best general storage for variables?
I've personally found that I call certain variables so much that I like to keep them as globals. I organize my globals as arrays, which is much tidier once you have hundreds or thousands of variables. Arrays load all at once,Jonathan Mayshar wrote: ↑Thu Dec 28, 2017 5:12 amIn my game (which is really only a project for myself to get into LC) I need to keep track of various inventory that the player picks up, as well as the changes that those inventory items make to the player's abilities (keys, "magic swords", etc.). I'm keeping track of the player's health and other stats with global variables, but again, I see warnings that these are to be avoided (and it does get tiresome to declare them over and over again).
For example you could have a gCharInventory array variable with keys like gCharInventory["left hand"] , gCharInventory["right hand"], and so on. You could load this global variable from a custom property
Code: Select all
put the cSavedCharacters["Example User 1"]["Inventory"] of this stack into gCharInventory
Then you set the custom property to that array when ending the session or using a new character, so that the information is stored between sessions. You have to set the entire custom property at once -- I don't believe you can modify specific lines or parts of a custom property without first loading it as a variable, then making your changes, and then setting it -- as below:
Code: Select all
global gCharInventory
put the cSavedCharacters of this stack into x
put gCharInventory into x["Example User 1"]["Inventory"]
set the cSavedCharacters of this stack to x
Code: Select all
the cProperty of card "main" of stack "popup"
The reason you often see advice to avoid globals is because people often lose track of them, and because the values need to be saved at the end of a session (or they'll be lost). You should ONLY use global variables for values you might call anywhere, and you should work hard to keep them organized. Do yourself a favor and learn how to use arrays. I used to do the invisible field thing and arrays are a million times better because of how much easier it is to organize large amounts of data. I'm still an amateur (2 1/2 years on LC, minimal prior coding experience) but this was one of my big discoveries last year.
EDITED: custom properties, not custom variables
Last edited by ittarter on Wed Jan 03, 2018 5:47 pm, edited 4 times in total.
-
- VIP Livecode Opensource Backer
- Posts: 9580
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Where is the best general storage for variables?
Just a comment, ittarter.
You several times use the term "custom variables" when you meant "custom properties". I understand you know the difference, but want to make sure the OP is not confused.
Craig
You several times use the term "custom variables" when you meant "custom properties". I understand you know the difference, but want to make sure the OP is not confused.
Craig
Re: Where is the best general storage for variables?
You're right, Craig. I've updated my post.
Re: Where is the best general storage for variables?
No, it is drop dead sexy, because it never leads you astray!FourthWorld wrote: ↑Thu Dec 28, 2017 7:41 amPlain text is seductive, because it's easy for us to read and comprehend directly.
All kidding aside, *if* your good at planning your data out, you shouldn't need to worry too much about escaping stuff, but, if you find yourself having to come up with successively more complicated escape routines, then what Richard said for sure
-
- VIP Livecode Opensource Backer
- Posts: 9802
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Where is the best general storage for variables?
I think the difference as to when plain text is useful and when a more robustly-segmented format may be useful boils down to the needs of the application at hand.
For example, a great many programs store config info in plain text, and as simple name-value pairs in which each value will never be more than one line that works well.
But a large percentage of apps use binary formats, or at least robustly-segmented formats like XML, for their documents, because they need each value in a name-value pair to be able to hold a wide range of info, which may include delimiters.
Even those that use XML for portions of their data do so within a more complex binary format. Consider office documents, where the body of the document may be in XML but it also needs images, style definitions, and more, so they wrap up all those parts into a Zip archive so they can keep all the parts easily obtainable discretely yet maintain the document as a single file on disk.
Many of the benefits of Zip-based formats can be obtained within LC using LSON data (encoded arrays), more simply and more efficiently.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Where is the best general storage for variables?
All of that is certainly true.