How to change current stack

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 227
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

How to change current stack

Post by kaveh1000 » Sun Dec 09, 2018 12:10 pm

[indent][/indent]I have a substack that is acting as a dialog box, to return values chosen by the user. Based on those values I call other handlers, all of which are on the main stack.

The problem is that it seems because the handler originates from the substack, the "current stack" is the dialog stack, even when commands in the main stack are being run. This means that even in the main stack I have to specify, e.g.

Code: Select all

get the hilite of btn 1 of stack "main"
I have tried putting "close this stack" in the substack handler, before calling main stack handlers, but it is not helping.

How can I avoid having to put in the name of mainstack?
Kaveh

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

Re: How to change current stack

Post by bogs » Sun Dec 09, 2018 1:12 pm

Without a better description of your stacks setup, or a picture perhaps of the two stacks, it would be tough to diagnose.

For instance, does your substack have a button control (of any type) on it? If so, just using

Code: Select all

button 1
will never work, since your sub stack has a button 1 as well.

You might, *if* that is the case, be able to get by with the short name of the 'button', OR better yet the ID of the button, but that may be harder to remember easily than just adding

Code: Select all

of stack "main"
in the end.
I have tried putting "close this stack" in the substack handler, before calling main stack handlers, but it is not helping.
No, that would not help nor should it be necessary.
Image

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 227
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: How to change current stack

Post by kaveh1000 » Sun Dec 09, 2018 4:53 pm

Thank you Bogs. And sorry I did not give enough info.

I finally found that answer, namely to put a handler in the main stack, then say

Code: Select all

send "myhandler" to stack "main"
So this can be called from the substack, and the "main" will become the main stack.
Kaveh

Klaus
Posts: 10986
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to change current stack

Post by Klaus » Sun Dec 09, 2018 5:17 pm

Maybe you could just:
...
set the defaultstack to "main"
...
?

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

Re: How to change current stack

Post by dunbarx » Sun Dec 09, 2018 5:41 pm

Hi.

This is a common issue with subStacks, knowing where one is at any particular time. Explicit stack references solve the problem. Klaus' suggestion always put the stack of interest in front, but that does not always jibe with what the user is intended to see.

I go with stack references, as Bogs recommends.

And you do not need to explicitly "send" a message to a mainStack from a subStack. The mainStack is above in the hierarchy. Try this. In the stack script of your mainStack,

Code: Select all

on testMessage
   answer random(99)
end testMessage
and in a button in your subStack:

Code: Select all

on mouseUp
testMessage
end mouseUp

Craig Newman
Last edited by dunbarx on Sun Dec 09, 2018 5:50 pm, edited 2 times in total.

Klaus
Posts: 10986
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to change current stack

Post by Klaus » Sun Dec 09, 2018 5:46 pm

Hi Craig,
Klaus' suggestion always put the stack of interest in front, but that does not always jibe with what the user is intended to see.
this is so true, I also recommend the explicit card and stack referencing in cases like this one!

Best

Klaus

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

Re: How to change current stack

Post by bogs » Sun Dec 09, 2018 6:10 pm

Heh, I'm glad you found the answer, because I think I am getting more confuzzled Image

Just so I don't spend a sleepless night, the way I read your first post, I saw something like this in (whats left of) my mind (which aint much) -
Selection_001.png
Button button, whose got the...wait a minute....
So while what you found would *work*, I'm not sure how it is any shorter than adding "of stack "Main" in your first post to tell Lc where to find btn 1 ....oh dear, my eyes have gone crossed again :?

Craig and Klaus both chimed in while I was writing this. Craig added what I was also thinking (about message heiarchy), but I wanted to make sure I had the basics right first. I'm pretty sure his answer is what you need to think along.
Image

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 227
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: How to change current stack

Post by kaveh1000 » Sun Dec 09, 2018 6:59 pm

Hi all

Thank you for your explanations. The handler that needs to operate on my "target" stack, i.e. main stack, needs several parameters and buttons that are on the main stack, e.g. field "text", btn "choice", etc.

With the solution of saying

Code: Select all

...of stack "main"
I had to put this several times, once for each control, which is not really elegant. This is because the defaultStack remained the original substack that called the handler on the main stack.

I have put put in Craig's suggestion of putting

Code: Select all

set the defaultStack to stack "main"
just before calling the handler in the main stack. That seems to have done the trick.
Kaveh

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

Re: How to change current stack

Post by dunbarx » Sun Dec 09, 2018 8:14 pm

Hi.

That was Klaus, not me.

Now then, you wrote:
once for each control, which is not really elegant
But you can pass any number of parameters along with a message. Got some spare time? Make three buttons on card 1 of your mainStack. Label them "A", "B", and "C". It would be nice to have some other buttons as well. In the mainStack script:

Code: Select all

on testMessage a,b,c
   repeat with y = 1 to the paramCount --paramCount is "3"
      put the loc of btn param(y) of stack (the mainstack of this stack) & return after accum
         --param(y) is the current parameter. Param(2) is "b"
   end repeat
   answer accum
end testMessage
In a button on your subStack:

Code: Select all

on mouseup
   testMessage "A","B","C"
end mouseup
Now perhaps this has a couple of unfamiliar terms, such as "param" and "paramCount". But you can see that a message can pass parameters along with it, that can be acted upon in a remote stack. Note that the "mainStack of this stack" is very useful, since you might have multiple subStacks, and this function will always finds the mother ship.

The handler will remotely find the "A", "B" and "C" buttons among others, and return their locs.

And the real lesson is that you must still explicitly designate the mainStack, or LC will throw an error, not knowing where those buttons are.

Craig

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 227
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Re: How to change current stack

Post by kaveh1000 » Sun Dec 09, 2018 8:41 pm

Thanks for that explanation Craig and sorry for mixing up the respondents. ;-)

This has been a very useful lesson for me regarding stacks and main stacks. :-)
Kaveh

SparkOut
Posts: 1934
Joined: Sun Sep 23, 2007 4:58 pm

Re: How to change current stack

Post by SparkOut » Sun Dec 09, 2018 10:17 pm

button 1 of stack "main" would, I presume, resolve to button 1 of card 1 of stack "main" - you can (and probably should) get very explicit about the granularity of object references.
Craig demonstrates a very useful way of passing off the tedious declaration of the object ref to the handler, not the call.

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

Re: How to change current stack

Post by dunbarx » Mon Dec 10, 2018 12:43 am

Sparkout has a point. Card 1 of any stack is the default card, and need not be explicitly referenced. But that leaves room for problems later, if that card changes. Bottom line, make all references fully formed.

Note that I personally am rarely this disciplined. It only falls apart for me now and then. :wink:

Craig

Post Reply

Return to “Talking LiveCode”