structure question

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
Johan_VH
Posts: 67
Joined: Fri Mar 28, 2014 2:46 pm

structure question

Post by Johan_VH » Mon Oct 20, 2014 9:29 am

Having finished my 7 first livecode-projects in recordtime these past 3 months (with a big Thank You to all of you who helped), I still have a question concerning the structure of my programs.
I just started on my next one, a small webkiosk application, but next week will be a bigger one again, so...:

I now had my main stack, that just held all my general code (database connections, timers, RFID sockets, ...) and 4 substacks: the game, popups , the registration and the screensaver. I ran into some problems however in the testing phase with my timers. Sometimes when the user logged out or timed out just at the moment when a popup was shown an error was thrown. For instance when the user started a math-quiz and left, the quiz continued since the time allotted was short and taking too long to answer should count as wrong. If this happened when the user left on the first question, the timeout that checked for user interaction timed out and the game was reset to the screensaver. When this happened right at the moment that the correct/wrong popup was shown, the program would want to start the next question and not find it anymore since it was in a different substack now. So I had to add /of stack "game"/ to each line of code where it was appropriate.

So my question: is it a bad habit to use substacks, should I just keep everything in one stack and just use cards, should I just pay more attention when handling my timeouts and logouts, or should I just adopt the practice of specifying in minute detail to which card and substack each command refers to?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: structure question

Post by dunbarx » Mon Oct 20, 2014 2:59 pm

Hi.

Firstly, congratulations. That sounds like a lot of work in a short time.

Secondly, and this is a matter of style, I always try to stay in one stack. Substacks or simply other stacks are useful if, say, you need two open at the same time, or perhaps a completely different aspect is required (size, background art, etc.), a separate library stack, a splash stack for a standalone, whatever.

It is important, though, to determine if there is an intrinsic problem with what you have discovered. The IDE is a place where all sorts of conflicts can fester, since it deals with "real" objects in real time. Consider the many threads that deal with the loading of external resources. These talk about using "preOpenWhatever" handlers to allow time for this sort of process, especially including "wait with messages" commands to give the IDE a break. It often needs one.

So is your problem reproducible? If so, there are many interested minds who want to see it.

Craig Newman

Johan_VH
Posts: 67
Joined: Fri Mar 28, 2014 2:46 pm

Re: structure question

Post by Johan_VH » Mon Oct 20, 2014 3:31 pm

Yes, it was reproducible. When the logoutbutton in my game was pressed, the program switches stacks. When this happens when an answer popup was shown, I leave the gamestack but the code kept executing, hence, after the period of time (a few seconds, achieved with the wait-command) the answer was shown, the command executes to hide the answer popup and I got an error. By the way, these errors were shown in an error window, but completely black with black or no text, so I had no idea what the error was in the runtime, I had to reproduce the action in the authoring environment.

The reason why I use separate stacks is because I come from years of programming in Director and Flash, both of which always gave me a lot of trouble with memory leaks. Hence, closing and removing portions of the application from memory has become a tried and proven tactic that I ported over to LiveCode. Maybe unnecessary, but better be safe...

My timeout- and another-user-has-scanned-his-rfid-bracelet-popups are logical to come from a separate stack then, since they used a mask and were shown on top of the game- or registrationscreens.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: structure question

Post by dunbarx » Mon Oct 20, 2014 4:10 pm

Hard to know what is going on without seeing the stacks. But just for ha-ha's, what happens if you "wait with messages" instead of just waiting?

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: structure question

Post by jacque » Mon Oct 20, 2014 8:51 pm

I assume by "answer popup" you mean a stack that overlays the main stack. If the substack is displayed in the default style, all your code will continue running as you describe. I think what you want is to display the stack as a dialog. Dialogs stop all code execution until they are dismissed. Change your script to:

modal "sustackName"

Be sure to include some way to dismiss the dialog (a button that closes the stack, usually) or there's no way out. Your script can't close it because all code stops until it is closed.

Your use of substacks sounds very appropriate to me. I use them frequently, they are convenient to separate activities that naturally form cohesive units. For example, I use a substack as a login to a main stack, for custom dialogs (as per above,) for palettes, etc. As Craig said, it's a matter of style. I have no compunctions about keeping everything in the same stack, and it's often easier to use a substack than to work around a card you don't want in the main stack navigation.

When LC opens a stack file, the entire contents is loaded into RAM at once, including all substacks. Closing a substack doesn't remove it from memory, so it's different from Director that way. You won't change the memory footprint by closing a substack, but it does remove the stack from the message and inheritance paths which is useful.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Johan_VH
Posts: 67
Joined: Fri Mar 28, 2014 2:46 pm

Re: structure question

Post by Johan_VH » Tue Oct 21, 2014 9:51 am

The answer popup is actually just a group that slides in and out, not a separate stack like the timeout popups. So maybe I should change it and move them to a separate stack and use it as a modal dialog. Although this seems more appropriate for just the timeout popups. I'll look into it as soon as this project is finished! I'll also look into the "wait with messages" for the answer popup. Thanks a lot you both!

O, and since you stated that closing a substack doesn't remove it from memory, does the "purge stack on close" option serve another purpose then? I thought it purged it from memory?

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: structure question

Post by jacque » Tue Oct 21, 2014 3:02 pm

Yes, the destroystack property removes a stack from RAM when it's closed, but that actually only applies to mainstacks. Substacks are always in memory, because they're part of the same stackfile.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply