Message path - why can code on a stack see down?

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
KimD
Posts: 225
Joined: Wed Jul 08, 2015 5:51 am

Message path - why can code on a stack see down?

Post by KimD » Mon Oct 26, 2015 10:04 pm

The code for my project is working, but I don't understand why and its bugging me ;-)

Stack
On PreOpenStack
Set the location of graphic XYZ to 50,50 // with no reference to which card graphic XYZ is on
End PreOpenStack

Card ABC
Graphic XYZ

1) What I don't understand is how the "Set the location" code in the Stack knows where to find graphic XYZ. I thought that the message path only went "up" (object to card to stack), but in this case the code happily finds my object even though it is on a card (one of many) that is below the stack.

2) Related question - I've noticed that I seem to only be able to add Custom Functions at the stack level. If I try to cut and paste the same code onto a card then the compiler just ignores the code and the function does not appear in the handler list. Is it true that custom functions must be defined on a stack and not a card? Again, they seem to be working fine in the stack, although again I don't understand how they are finding the variables that they are working with (which are defined on a card below the stack).

Thanks in advance

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

Re: Message path - why can code on a stack see down?

Post by dunbarx » Mon Oct 26, 2015 11:09 pm

Hi.

1- Some things in LC happen by default. For example, when you go to (or open) a stack, unless you explicitly say otherwise, LC goes to the first card. So if your graphic is indeed on that first card, all runs smoothly. If you want to test this, make another card, and put your graphic there instead. Now try it. You will get a runtime error.

You can be as explicit as you want to, or you can manage a bit of laziness and shoot, exultant on.

2- Now I am sure that laziness is biting you somewhere behind. You can place custom functions anywhere you like, but those functions must be at or above the calling handler in the hierarchy. I bet you a dollar yours is not. Make a card. Make a button.In the button script:

Code: Select all

on mouseUp
   answer doubleUp("5")
end mouseUp

function doubleUp var
   return var * 2
end doubleUp
Now move that function to the card script. (Delete it in the button script). No problem, right? Now go find out why your function was out of the message path. It is, you know...

Craig Newman

KimD
Posts: 225
Joined: Wed Jul 08, 2015 5:51 am

Re: Message path - why can code on a stack see down?

Post by KimD » Tue Oct 27, 2015 12:02 am

Thanks Craig

Ahhh

I can see now that I got a bit lucky (by having the object that I was referencing on the 1st card)

You are right - I will need to re-work my code to avoid needing to rely on luck.

Regards

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

Re: Message path - why can code on a stack see down?

Post by dunbarx » Tue Oct 27, 2015 1:46 pm

Good.

You may need to know that you can send a message "down" the hierarchy with the "send" or "dispatch" commands. This would be a good lesson. Make a card with a mouseUp handler in its script. Make a button with a function in its script. Maybe use the handlers I suggested. What would you put in that card-level handler?

Craig

Post Reply