Difference between Mainstack and Substack

Graham Samuel livfoss at mac.com
Thu Jan 10 09:31:10 EST 2013


Hi Christophe

I've been using this mainstack/substack thing for so long that I had to think quite hard to give an answer. Others may respond differently, but my take on this is:

Every program in LiveCode has to begin with a mainstack, which is the stack that is loaded when the program starts - this is reflected in the fact that the engine sends a 'startUp' message to the first card of your mainstack on loading.

Think of the mainstack as a window showing objects (buttons, fields, graphics etc) and scripts which respond to user activity. A simple app can contain just one stack, and a very simple one can have just one card (fairly unlikely). But if for example you want to open more than one window at a time in your app, the other windows will be substacks. These can act pretty much the same as a mainstack, but are 'sub' in the sense that the are subsidiary to the mainstack and the message path goes through a substack to the mainstack and not the other way round (I'm not going to explain the message path now!). 

Typical uses of substacks are things like data entry screens that can be invoked from menu items in the mainstack; or specialised displays which just get opened when the user needs them, 'Help' and 'About' screens etc. There are some built-in ways of showing dialogs asking for decisions and small amounts of user input: these aren't substacks, but they are very restricted, just allowing whatever the underlying OS allows. A more generalised version of this kind of thing is a palette, which is a particular flavour of substack.

Substacks then normally add chunks of functionality to your app and exist in separate windows. Quite a lot of stuff that substacks get used for might be done another way (for example a 'Help' screen might just be a hidden field on your mainstack, but it is more powerful and a better structure to make the 'Help' a separate stack). LC often offers several ways of accomplishing the same thing.

Another very important use of substacks is to allow them (or the data in them) to be changed by the user. In all modern operating systems, self-modifying programs are forbidden, which means if the user changes (say) some fields in your mainstack, when the app finishes those changes can't be preserved unless the app intervenes and puts the changes in a file somewhere. From the OS's point of view, one can prepare a standalone program as just a mainstack, with substacks outside it (this is the 'splashscreen' method).  In this structure there is no restriction on modifying a substack and just saving it as a whole whenever you want to preserve the changes. This gives the possibility  of storing data in a highly structured way without going outside the LC environment to a database or some other filing system (of course such external systems are vital in several kinds of applications, but the use of substacks for storage is very powerful for many applications).

So, if I were writing a desktop app, I would have a mainstack which would show the user the opening activity in the program and which would contain common scripts (handlers) to be used by different parts of the app, and substacks which would be invoked to carry out specific functions and which in principle could look quite different from the main stack (different dimensions) and of which several could be open a the same time.

If I were writing a mobile app, the idea of opening more than one window at a time (visible to the user) probably wouldn't apply, but there are many other reasons why you might still use substacks.

I wonder if this is at all clear - perhaps I didn't think about it enough! I've just seen Colin Holgate's one-sentence reply which may help you more than mine. Ah well, HTH anyway.

Graham


On 10 Jan 2013, at 14:01, Christophe Leske wrote:

> Hi,
> 
> this might sound like a beginner's question, but what exactly is a mainstack and what a substack?
> 
> Which one is for what? How do I structure my project around these structures?
> 
> Do I throw my splash screen and opener into a mainstack, and everything else in a substack, or everything in a mainstack?
> 
> 
> Thanks in advance,
> 
> -- 
> Christophe Leske
> multimedial.de
> 
> ----------------------------------------
> www.multimedial.de - info at multimedial.de
> Hohler Strasse 17 - 51645 Gummersbach
> +49(0)2261-99824540 // +49(0)177-2497031
> ----------------------------------------
> 
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list