Page 1 of 1

GUI on diffrent tabs

Posted: Sun Aug 23, 2020 7:08 pm
by froggy
I have created my first LiveCode project, and I based it on an idea of separating the GUI in different tabs, inspired by this tutorial: lessons[dot]livecode[dot com]/m/4071/l/7574-using-tab-buttons

I have now completed the contents of tab 1, there I have a number of user controls in the form of buttons, switches, label fields etc. The function is basicly setting up a timer, and the main timer code is placed in the stack, where it fire a call to itself every 1 seconds. It all works fine.

Now I moved on to create contents in tab 2. There I basicly only need the present timer value, which I have in a global variable. Based on the present timer value I now want different things to happens when I click different buttons in tab 2.

But as soon as, while running, I switch to tab 2 two things happens:
- The functions I have placed in the scripts of buttons placed on tab 2 can not read the global variable.
- The functions running in the stack, which where working fine as long as I had tab 1 selected, can no longer read the status of any buttons etc.

It's as if nothing of the buttons etc in tab 1 are defined at all, and as the stack script calls itself every 1 seconds and are dependent on these, it all comes crashing down as soon as I click tab 2!

I assume I have totally misunderstood the functionality of how the different objects communicate and I now have the wrong structure of my code. But what should I have done to get this to work, is it possible to fix it?

Re: GUI on diffrent tabs

Posted: Sun Aug 23, 2020 7:24 pm
by Klaus
Hi froggy,

welcome to the forum!

If you use GLOBAL variables in LC, then you need to declare them in EVERY script you use them (NOT in every handler, see below),
that maybe different in other programming languages:

Code: Select all

global a_global_var

on mouseup
  put 42 into a_global_var
end mouseup

command reset_var
  put 0 into a_global_var
end reset_var

## etc.
Best

Klaus

P.S.
Personal note: A little "Hello" or something for the very first posting would not have hurt.

Re: GUI on diffrent tabs

Posted: Sun Aug 23, 2020 8:41 pm
by froggy
Thanks Klaus!

That solved the problem of the unreadable global variable! So half of the problem fixed! Live Code is a bit different language, mostly to the better, it is more simple to use and easy to learn, but still, anything that's different from what you're used to cause headache sometimes.

And, Hello! :D Sorry for jumping straight into the question. Been coding for a good number of hours, frustrated to finding it fails just when I was so happy that my first tab in the project was completed and working on it's own. I just wanted to solve the problem, and I forgot my people's skills... :oops:

Re: GUI on diffrent tabs

Posted: Mon Aug 24, 2020 2:52 am
by dunbarx
Froggy.

What Klaus said.

Know also that you can declare both global and script local variables, but they must be declared ABOVE any handlers that use them. I always declare them all at the very top of the script, but others prefer to declare just above the handlers that will use them. That is up to you.

Craig

Re: GUI on diffrent tabs

Posted: Mon Aug 24, 2020 8:43 pm
by froggy
Thanks Craig, I got a grip of the variables handling now I think. But I still have the issue of the routines in the stack can not access the controls if I'm in another tab/Card.

I have for example this code in the stack, where optionHide is a Switch Button, and timeLeftDisplay is a Label Field

Code: Select all

on displayUpdate
   if the hilited of widget "optionHide" then
      put "HIDDEN" into field "timeLeftDisplay"
   else
      put remainingTime into field "timeLeftDisplay"
   end if
end displayUpdate
As soon as the Button and Label are not visible on the screen because I'm viewing another Card, I just get No Such Object error when the displayUpdate routine is executed. I could do a workaround for the button, having the status of the button in a variable instead, and looking at that. But I can't workaround the Label Field like that, so it's of no use anyway.

Can I set the optionHide and timeLeftDisplay as globals somehow, would that be possible and would that solve anything? Or do I need to figure out when the Card containing the Button and Label is not visible, and then prevent any code that would affect input/output on that Card to be run? Or there is some more correct way? I assume there is... :?

Re: GUI on diffrent tabs

Posted: Tue Aug 25, 2020 4:53 am
by dunbarx
Hi.

If you navigate to another card, LC will lose any local references. But this can be easily fixed (pseudo):

Code: Select all

on displayUpdate
   if the hilited of widget "optionHide" of card "cardWithTheWidget" then
      put "HIDDEN" into field "timeLeftDisplay" of card "cardWithThatField"
   else
      put remainingTime into field "timeLeftDisplay" of card "cardWithThatOtherField" 
   end if
end displayUpdate
LC permits you to extend this concept to other stacks as well (pseudo):

Code: Select all

put "HIDDEN" into field "timeLeftDisplay" of card "cardWithThatField" of stack "stackWithThatCard"
Craig

Re: GUI on diffrent tabs

Posted: Tue Aug 25, 2020 6:02 pm
by froggy
Thanks Craig, you nailed it! This got everything working as intended!

Two out of two for my questions in this thread, I'd call that a perfect score! :D