Where is the best general storage for variables?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Jonathan Mayshar
Posts: 2
Joined: Thu Dec 28, 2017 3:01 am

Where is the best general storage for variables?

Post by Jonathan Mayshar » Thu Dec 28, 2017 3:06 am

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?

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Where is the best general storage for variables?

Post by bogs » Thu Dec 28, 2017 4:01 am

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.
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Where is the best general storage for variables?

Post by dunbarx » Thu Dec 28, 2017 4:22 am

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

Jonathan Mayshar
Posts: 2
Joined: Thu Dec 28, 2017 3:01 am

Re: Where is the best general storage for variables?

Post by Jonathan Mayshar » Thu Dec 28, 2017 5:12 am

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).

FourthWorld
VIP Livecode Opensource Backer
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?

Post by FourthWorld » Thu Dec 28, 2017 6:18 am

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:

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
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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Where is the best general storage for variables?

Post by bogs » Thu Dec 28, 2017 6:31 am

Jonathan Mayshar wrote:
Thu Dec 28, 2017 5:12 am
Perhaps I don't know enough to ask the right questions.
Boy do I know THAT feeling :wink:

It is no problem though, just expect questions for clarity. I'm still lacking enough to be quite dangerous in my answers :D

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.
Image

FourthWorld
VIP Livecode Opensource Backer
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?

Post by FourthWorld » Thu Dec 28, 2017 7:41 am

bogs wrote:
Thu Dec 28, 2017 6:31 am
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.
Plain text is seductive, because it's easy for us to read and comprehend directly.

And if your values are plain text and have no Return chars, something like this:

Code: Select all

label1=value1
label2=value2
label3=value3
...can be turned into a tidy array in one line:

Code: Select all

split tData with cr and "="
But the moment you have data with a CR in you'll need to rethink things. You can escape the CR of course, provided you find a character not also in use, in which case you'd need to escape the escape, and so on.

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Where is the best general storage for variables?

Post by dunbarx » Thu Dec 28, 2017 3:52 pm

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

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: Where is the best general storage for variables?

Post by ittarter » Tue Jan 02, 2018 3:16 pm

Jonathan Mayshar wrote:
Thu Dec 28, 2017 5:12 am
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).
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,

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 
which would load left hand, right hand, and a hundred other inventory values, e.g. backpack 1,1, stash 5,3,1 (I'm thinking like Diablo or Path of Exile)

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
The reason I sometimes prefer globals is that custom property have to be "set", which is a bit more limiting (for me, anyway) in the livecode environment. They also have to be set to a certain object, so if you're planning on using multiple stacks, it can be messy to type

Code: Select all

the cProperty of card "main" of stack "popup"
if that's where you saved it. Also more opportunities to make mistakes. Global variables, by contrast, are MUCH easier to keep track of, because they're all in one place, and you can see them whenever you open your script editor.

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Where is the best general storage for variables?

Post by dunbarx » Tue Jan 02, 2018 4:08 pm

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

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: Where is the best general storage for variables?

Post by ittarter » Tue Jan 02, 2018 5:24 pm

You're right, Craig. I've updated my post.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Where is the best general storage for variables?

Post by bogs » Tue Jan 02, 2018 8:32 pm

FourthWorld wrote:
Thu Dec 28, 2017 7:41 am
Plain text is seductive, because it's easy for us to read and comprehend directly.
No, it is drop dead sexy, because it never leads you astray! :twisted:

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 :wink:
Image

FourthWorld
VIP Livecode Opensource Backer
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?

Post by FourthWorld » Tue Jan 02, 2018 9:17 pm

bogs wrote:
Tue Jan 02, 2018 8:32 pm
All kidding aside, *if* your good at planning your data out, you shouldn't need to worry too much about escaping stuff...
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

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Where is the best general storage for variables?

Post by bogs » Tue Jan 02, 2018 9:21 pm

All of that is certainly true.
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”