Page 1 of 1
Code Organization
Posted: Thu Jan 16, 2020 10:31 pm
by JackieBlue1970
Hi. I'm new to LiveCode but have coded off and on for years in VB (classic and .net), C, Python, and various scripting languages. However, I'm very confused on how you organize your code in LiveCode. Are there no modules or classes? Any way to separate code from user interface? I'd prefer to have my code in separate modules as it makes it easier to share code (such as database access) between projects. The examples I've found so far seem to put everything into a card. Cards appear to me to be just for interface. I know you could put code in the card but often the same code will need to be referenced from other cards. How is this resolved? TIA.
Re: Code Organization
Posted: Thu Jan 16, 2020 10:59 pm
by jmburnod
Hi,
I know you could put code in the card but often the same code will need to be referenced from other cards. How is this resolved?
You may put scripts on stack script too.
This link should be useful
http://lessons.livecode.com/m/4603/l/56 ... ssage-path
Best regards
Jean-Marc
Re: Code Organization
Posted: Thu Jan 16, 2020 11:11 pm
by bogs
I think what JackieBlue1970 is looking for would be more akin to a
library, or
script only stack. Either would work for separating code from the objects in the working stack for re-usability.
You might also want to look into
behaviors, although from my understanding of your question, the first two suggestions would be more likely (i.e., the most like modules).
Re: Code Organization
Posted: Fri Jan 17, 2020 12:17 am
by FourthWorld
The LiveCode IDE itself is a good example of that sort of code style. Scripts are often factored from UI as behaviors, and nearly everything is plain text files so it can all be worked on via GitHub.
Re: Code Organization
Posted: Fri Jan 17, 2020 1:14 am
by dunbarx
Hi.
Welcome to LiveCode.
The whole point is that everything is integrated. You work directly within the IDE, and manage controls, layout and code all at once. It takes a bit of getting used to, but is far easier and faster to learn than any of the other languages you mentioned.
Try a simple task, like a calculator or address book. Use this forum often. Once you get going, you will see how it all works, and then you can determine whether LC is for you.
Craig
Re: Code Organization
Posted: Fri Jan 17, 2020 5:01 pm
by JackieBlue1970
Thanks for your responses everyone. I'll take a look at the suggestions.
Re: Code Organization
Posted: Fri Jan 17, 2020 5:05 pm
by Klaus
Hi Jackie,
you can also put all of your ahndlers/functions into the stack script of your mainstack.
This way all objects on all cards of the stack and its substacks can access them, because they are in the "message path".
Here a very good read about "message hierarchy":
http://www.fourthworld.com/embassy/arti ... _path.html
Best
Klaus
Re: Code Organization
Posted: Fri Jan 17, 2020 7:36 pm
by JackieBlue1970
Ok. I think I understand the concept now. The terminology is very strange for someone that has programmed for years. Correct me if I am wrong:
Stack is a group of related cards. A stack can be an application or an application can consist of multiple stacks. Cards are windows within that application. You can put functions & handlers(I think of them of subroutines) in the top card and can be referenced within in the stack. If you want more than one stack in that application to reference the functions, it would be better to create a "script-only" stack that can be referenced from all the stacks in the application. I'm pretty fuzzy on scope and the lack of typed variables. TIA again for your comments. Jack
Re: Code Organization
Posted: Fri Jan 17, 2020 8:04 pm
by Klaus
Hi Jack,
JackieBlue1970 wrote: ↑Fri Jan 17, 2020 7:36 pm
Ok. I think I understand the concept now. The terminology is very strange for someone that has programmed for years.
yes, it surely is!
JackieBlue1970 wrote: ↑Fri Jan 17, 2020 7:36 pm
Stack is a group of related cards.
A stack can be an application or an application can consist of multiple stacks.
Cards are windows within that application.
Yes to all. And we can only display ONE card of a stack at a time.
JackieBlue1970 wrote: ↑Fri Jan 17, 2020 7:36 pm
You can put functions & handlers(I think of them of subroutines) in the top card and can be referenced within in the stack.
NO!
If you put your "subroutines" into the STACK cript of the Mainstack, then all object on all cards of the mainstack and its substacks can access them!
If you put them into the first card, then only objects on that card can access them -> Message hierarchy
JackieBlue1970 wrote: ↑Fri Jan 17, 2020 7:36 pm
If you want more than one stack in that application to reference the functions, it would be better to create a "script-only" stack that can be referenced from all the stacks in the application.
Not neccessarily, see above.
JackieBlue1970 wrote: ↑Fri Jan 17, 2020 7:36 pm
I'm pretty fuzzy on scope and the lack of typed variables.
Get used to it!
Best
Klaus
Re: Code Organization
Posted: Fri Jan 17, 2020 9:18 pm
by dunbarx
Getting there...
You can put functions & handlers(I think of them of subroutines) in the top card and can be referenced within in the stack.
Klaus is correct, in that the hierarchy strictly controls traffic from one level to another. But know that you can bypass the hierarchy with either the "send' or "dispatch" commands, allowing you to send a message from any level to any other level explicitly.
See this:
This is the built-in message path. You can subvert it as much as you want, but if you do not, it works rigidly and automatically.
Know also that LiveCode is ALL about messages. You have to understand that first and foremost. All else are details, really.
When you click on a button, several messages are generated, originating from that button. They are then open to be "trapped". They are trapped by you, when you write a handler with the name of one of those messages. "MouseUp" is a very common one, and it is pretty self-explanatory. You can trap it anywhere, though usually this is done in the target button.
Read about the message hierarchy in the User Guide. Start at page 100. There is a more detailed diagram there, and the next handful of pages get right to the heart.
Craig
Re: Code Organization
Posted: Fri Jan 17, 2020 9:32 pm
by bogs
jmburnod wrote: ↑Thu Jan 16, 2020 10:59 pm
Hi,
I know you could put code in the card but often the same code will need to be referenced from other cards. How is this resolved?
You may put scripts on stack script too.
--------
Best regards
Jean-Marc
Klaus wrote: ↑Fri Jan 17, 2020 5:05 pm
you can also put all of your ahndlers/functions into the stack script of your mainstack.
This way all objects on all cards of the stack and its substacks can access them, because they are in the "message path".
Hmm, maybe Jean-Marc had it right, and I completely missed the target

but I still say that the closest translation to .vb modules in Lc would be libraries, or script only stacks, especially if code re-use is the primary goal.
Re: Code Organization
Posted: Fri Jan 17, 2020 9:43 pm
by dunbarx
Bogs.
The OP is a bit away from those ideas. He is not yet even a beginner.
But he sounds like he soon might be.
Craig
Re: Code Organization
Posted: Tue Jan 21, 2020 4:11 am
by JackieBlue1970
Thanks everyone. Will be doing some more tomorrow now that I'm back in town. Jack