window management

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: window management

Post by shaosean » Tue Jul 23, 2013 1:36 am

http://livecode.com/developers/api/6.0.2/keyword/this/ wrote:Indicates the current stack, or the current card of the current stack.
"this" means the current stack/card, so once you change the card/stack "this" means the new card/stack you just switched to

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Re: window management

Post by adventuresofgreg » Tue Jul 23, 2013 1:41 am

adventuresofgreg wrote:
sturgis wrote:Hard to say for sure exactly how to solve your problem without seeing some code, but.. you could use the long id of the card as a reference instead maybe? Though seems like when the code fires off, the "This" reference should work. If the script is on the card, then "This" -should- refer to the containing card. Can you post some code, and/or an example stack that demonstrates the problem?

Someone correct me if i'm wrong.
As a test, I made a script that added 1 to x and wrote it out to a field on a card. As soon as I changed cards, I got an error that LC could not find my the button on "this" card.

Card script:
local x
on turnon
if the hilite of button "turnon" on this card is true then
if x is empty then put 1 into x
put x+ 1 into x
put x into field "turnonfield" on this card
send turnon to me in 5 seconds
end if
end turnon

Also - I can confirm that referring to the card card ID also does not work. The only way to get this to work, is to refer to the UNIQUE card NAME in the card script. And since my card scripts are duplicated from a master card, there would be no way to add a unique card name reference to the new script.... unless,,,, I set the script of the new card script to a slightly custom script when it is being made... But that would only work with the open source LC because there aren't any script modification limits for standalones....

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Re: window management

Post by adventuresofgreg » Tue Jul 23, 2013 1:51 am

oh wait... I jumped the gun on that last post a little bit. I realized that in order to specify the ID, my script took the ID of the card and stuck it into a variable myID. But when I changed cards, that script would just grab the ID of the card I just changed to - so, that's what there was still a problem. So - how can I point to the unique ID of a new card without putting it into a global variable?

For example:

When I copy a master card to a new card, I can grab the ID of the newly created card, and put it into a global variable - say newCardID. Then, in the card script for the new card, I can "put something into field whatever on card ID newCardID". However, when I create a THIRD copy of the master card, the ID for that card will be placed into the newCardID global, and the SECOND copy of the card will stop working because the pointed is now to the THIRD copy.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: window management

Post by sturgis » Tue Jul 23, 2013 1:59 am

Try something like this..

Code: Select all

-- card script on all cards
local sRunning, sCounter,sId -- sRunning to control the loop, sId holds the id of the field, sCounter holds my random number
-- this is run from a grouped, background behavior button so that it appears on all cards along with a field thats in the group
-- and a button to switch between cards
command toggleMe
   if sRunning is empty then put false into sRunning
   put not sRunning into sRunning
   put the long id of field 1 into sId -- store the object id of the field on this card
   startLoop -- start looping
end toggleMe

command startLoop
   if sRunning then
      put random(500) into sCounter
      set the text of sId to sCounter -- sets the text based on the previously stored long id of the field. 
      send startLoop to me in 200 milliseconds
   end if
end startLoop

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Re: window management

Post by adventuresofgreg » Tue Jul 23, 2013 4:33 am

Thank you Sturgis. I think I understood what you were getting at in your sample script.I put the field ID's into local variables when the card is opened, and refer to the ID # vars rather than the field names in the card script.

I tried this and it does work. I guess it works because all field and button ID's are unique across all cards in a stack.

Thanks a lot. I'm sure I'll have more snags as I get further into this massive renovation.

Greg

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: window management

Post by sturgis » Tue Jul 23, 2013 11:00 am

You could also have your scripts in the stack script and use an array variable to manage things.

When a new card is set up to start running add its long id as a toplevel key to the array, with subkeys for any values you need to track for that card.

Then with each loop, cycle through all the main keys of the array and make adjustments to the array and card as necessary.

To remove a card from the loop, delete its key.

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: window management

Post by Klaus » Tue Jul 23, 2013 11:11 am

Hello, hello, this is the syntax police speaking! :-D

Please use the correct decriptor:
...
put x into field "turnonfield" OF this card
## not ON this cd!
...

This is not because I am a pedantic german, of which i already have been accused here in the forum 8-)
No, this comes from my experience over the last 15 years of using MetaCard/Revolution/Livecode!

With every new version the engine is getting a bit LESS forgiving for this sloppiness and one day this currently
working syntax may throw an error and I don't want to have to tell you "I told you so!", because you would
hate that! :-D

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Re: window management

Post by adventuresofgreg » Tue Jul 23, 2013 12:52 pm

[quote="Klaus"]Hello, hello, this is the syntax police speaking! :-D

Please use the correct decriptor:
...
put x into field "turnonfield" OF this card
## not ON this cd!
...

did not know that Klaus. thanks. It will take a while to retrain this old brain to make the switch though! ;-)

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Re: window management

Post by adventuresofgreg » Tue Jul 23, 2013 1:01 pm

sturgis wrote:You could also have your scripts in the stack script and use an array variable to manage things.

When a new card is set up to start running add its long id as a toplevel key to the array, with subkeys for any values you need to track for that card.

Then with each loop, cycle through all the main keys of the array and make adjustments to the array and card as necessary.

To remove a card from the loop, delete its key.
Sturgis: I am having problem (brain related) with nested arrays. Can you help me? For example:

1. I put field "greatBigHugeDataFile" into dataArray
2. split dataArray by return and tab
3. now, how do I stick dataArray array into another array with the name of the card as the key?

Thanks,
Greg

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: window management

Post by sturgis » Tue Jul 23, 2013 1:56 pm

say you have the long id of the card in a variable tCard, then the following should work fine.

Code: Select all

put dataArray into myCardArray[tCard]
Then you can loop through the cards and work with their sub arrays like so:

Code: Select all

repeat for each key tKey in myCardArray --each loop, a new main key will be available in the variable tKey
-- do what you need to with each entry
-- if you then need to loop through the sub keys you can 
repeat for each key tKey2 in myCardArray[tKey]
   put myCardArray[tKey][tKey2]  -- would put the contents of the current card key, and current subkey of that card into the message box
end repeat
end repeat

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

Re: window management

Post by jacque » Tue Jul 23, 2013 6:18 pm

Since the script is now in the card, you can refer to "me" instead of the long ID:

Code: Select all

local x
on turnon
  if the hilite of button "turnon" of me then
    if x is empty then put 1 into x
    put x+ 1 into into field "turnonfield" of me
    send "turnon" to me in 5 seconds
  end if
end turnon
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Re: window management

Post by adventuresofgreg » Tue Jul 23, 2013 7:11 pm

OK, I have been playing around with some of these solutions in a test stack, and I think I found the least painful way of converting my existing code over. It's actually rather simple. Tell me if this is a cop-out, or just a smart way of doing it:

I create a new handler that makes a CLONE of this stack, then changes the stack name to something unique so I can reference it. Then the handler runs through all of the fields, buttons, and graphics in the new stack, and replaces the character "$" with the name of the new stack. Then it opens the scripts for all the fields, buttons, and graphics in the new stack, and replaces all of the "$" chars which precede all of the global variable names, and replaces those with the unique stack name.

And it works perfectly. Every field, button, graphic, global, and local variable has a unique name, so there is no mix up between the running stacks. Plus it solves my problem of referring to the different stack because they are all sub stacks and have unique names. After this single handler is inserted into a button, or the stack script, all I need to do is rename every field, button, graphic by appending "$" to it. And then go through all scripts and append "$" to the variables.

Smart or kludgy?

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: window management

Post by sturgis » Wed Jul 24, 2013 2:37 am

Hmm. Your newest method may be.. well i'm not sure. If you're using commercial, not sure if the scriptlimits are still in place, (think they're gone but not sure) if they're still there they may come back to bite you later.

Have been running for 14 hours straight today, so not thinking clear enough to answer whether the method itself might lead to issues. Pretty sure there are better ways, and as mentioned, "this" and "me" can be very handy, as well as looking into behaviors so you can "assign" a behavior to an object on the fly..

adventuresofgreg
Posts: 349
Joined: Tue Oct 28, 2008 1:23 am
Contact:

Re: window management

Post by adventuresofgreg » Wed Jul 24, 2013 1:05 pm

sturgis wrote:Have been running for 14 hours straight today, so not thinking clear enough
"running" like actual marathon running, or running like working running? ;-)

I am also a bit concerned about the script limits, but I believe they are removed in LC community - but I don't know about commercial distribution. That's not really an issue though for me right now.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: window management

Post by sturgis » Wed Jul 24, 2013 1:51 pm

Running as in busy around town doing jobs and such. Came home, went comatose, better now!

After thinking on your solution, without actually having a concrete reason, I think i'd go another direction. Just a feeling of upcoming problems. As jacque mentioned, "me" might be helpful, or behaviors or.. many many options. Mind if I ask exactly what end result you're going for? If all cards must run exactly the same code and in relation to themselves, setting up 1 template card (or substack or whatever) should work fine if you can get your syntax and references straight. Then clone the card, name the card uniquely and turn it loose. You could even save a copy if your card to a property of the stack, then load it up and "paste" it (which gives you a card named "copy of card....." that you can rename.

Can you send post a sample of your old code that you are converting for multiple card/stack use?

Post Reply