Send/Call command Object Referencing

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
sritcp
Posts: 426
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Send/Call command Object Referencing

Post by sritcp » Thu Feb 20, 2020 12:10 am

LC 9.5.1 User Guide p 131-32 gives the following example:

Add to card 1 script:

Code: Select all

command showCard
   answer the number of this card
end showCard
Now, use the following statement from card 2 (card script or button script) to

Code: Select all

send "showCard" to card 1
According to the Guide, "send" command should result in "1" in dialog box, while "call" command would return "2".
I get "2" in both cases.

Anyone?

Thanks,
Sri

sritcp
Posts: 426
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: Send/Call command Object Referencing

Post by sritcp » Thu Feb 20, 2020 2:57 am

So, "this" refers to card 2 (for both "send" and "call") ...

But, "me" refers to card 1 (for both "send" and "call")...

and any reference to a script local variable refers to the one in card 1 (for both "send" and "call")
(to test this, define a script local variable each on card 1 and card 2, both with the same name, say, sCard; assign different values to them independently; reference and retrieve sCard in the test handler and see which one is returned.)

Regards,
Sri

AxWald
Posts: 473
Joined: Thu Mar 06, 2014 2:57 pm

Re: Send/Call command Object Referencing

Post by AxWald » Thu Feb 20, 2020 12:14 pm

Hi,

define & fill a script local variable on all cards:

Code: Select all

Local lNum
on opencard
   put the number of me into lNum
end opencard
Then replace your showCard script with this:

Code: Select all

command showCard
   put "This cd/ Cd Num:" && the number of this card & " ; LNum:" && lNum & CR & \
         "Me/ Cd Num:" && the number of me & " ; LNum:" && lNum
end showCard
Result on cd 2:

Code: Select all

This cd/ Cd Num: 2 ; LNum: 1
Me/ Cd Num: 1 ; LNum: 1
"This card" means "the current card".
"Me" is the card the script is in.
"Script local" relates to "Me".

From the old dictionary:
Use the this keyword to indicate the current card or current stack in an expression.
and:
Use the me keyword within a handler to determine which object's script is executing.
and
Use the local command [...] to define a script local variable that is shared between all the handlers in a script.
Have fun!
Livecode programming until the cat hits the fan ...

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1095
Joined: Thu Apr 11, 2013 11:27 am

Re: Send/Call command Object Referencing

Post by LCMark » Thu Feb 20, 2020 2:20 pm

I think the user guide is perhaps slightly wrong here...

When you 'send' a message `the defaultStack` changes to be the stack of the object you are sending the message to *and* `the target` changes to the object you are sending the message too.

When you 'call' a message both `the defaultStack` and `the target` remain the same.

Importantly, neither change the current card of the stack.

sritcp
Posts: 426
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: Send/Call command Object Referencing

Post by sritcp » Thu Feb 20, 2020 3:14 pm

Thanks Axwald and LCMark!
LCMark wrote:
Thu Feb 20, 2020 2:20 pm
......... neither change the current card of the stack.
That explains (and clears) my confusion!

Regards,
Sri

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”