Passing a graphic to a function

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
sumpm1
Posts: 19
Joined: Tue May 21, 2013 2:35 am

Passing a graphic to a function

Post by sumpm1 » Fri May 31, 2013 5:08 am

Hi. I am trying to write some simple functions to get the x and y coordinate of an input, and then eventually find the distance between two inputs. Here is the script. The script is doing other things. But it is moving a graphic, and then getting reporting the x and y coordinate.

Code: Select all

on moveRectDist x
   set movespeed to 300
   move the target to the loc of grc ("Rectangle" & x)
   put getX(the target) into field "Field1"
   put getY(the target) into field "Field2"
end moveRectDist

function getX input
   return item 1 of the loc of input
end getX

function getY input
   return item 2 of the loc of input
end getY
This script runs fine.

But if instead of using the target, I use the explicit graphic in question, I get an error.

Code: Select all

on moveRectDist x
   set movespeed to 300
   move the target to the loc of grc ("Rectangle" & x)
   put getX(grc "Rectangle1") into field "Field1"
   put getY(the target) into field "Field2"
end moveRectDist
I get the following error:

Code: Select all

Type	Chunk: source is not a container
Object	card id 1002
Line	put getX(graphic "Rectangle1") into field "Field1"
Hint	moveRectDist
Of course I want to know where I went wrong. Because the target evaluates to graphic "Recangle1" in this case?

But I am passing an object, a graphic, to a function. And it worked as expected when using "the target." And I didn't give it a second thought. Until I tried to pass a graphic using different syntax.

I would like to know what is the proper way to pass objects such as graphics to a function? And can you recommend any resources that address this?

Muchas Gracias.

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

Re: Passing a graphic to a function

Post by dunbarx » Fri May 31, 2013 6:02 am

Hi.

You are digging into the depths of the evaluation of arguments and parameters.

Good.

Try this instead, and write back with your thoughts:

 put getX("grc rectangle1") into field "Field1"

Craig Newman

icouto
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 91
Joined: Wed May 29, 2013 1:54 am
Location: Sydney, Australia

Re: Passing a graphic to a function

Post by icouto » Fri May 31, 2013 6:17 am

There is a difference between passing a 'reference to an object', and passing 'the contents of the object' itself. Using a field as an example makes it clearer:

When I want to put the contents of a field into a variable myVar, I use:

Code: Select all

put field "myField" into myVar
When I want to put a reference to a field into the variable, I can do it like this:

Code: Select all

put the long id of field "myField" into myVar
Putting 'the field' directly will read the text content of the field and put that into the variable. The 'long id' function, on the other hand, will generate a string like "field 1 of card 1 of stack "myStack" - or something similar - and put that into the variable.

The function "the target", like "id", will generate a string which is a reference to the object. That is what your functions "getX" and "getY" seem to be expecting: a reference to the graphics object. But instead of passing that, you seem to be trying to pass the content of the graphic object itself. You can probably fix that by doing one of the 2 things below:

1) Use the "long id":

Code: Select all

put getX(the long id of grc "rectangle1") into field "Field1"
2) Build a string that is an object reference:

Code: Select all

put getX("grc" && quote & "rectangle1 " & quote) into field "Field1"
I hope this helps!

sumpm1
Posts: 19
Joined: Tue May 21, 2013 2:35 am

Re: Passing a graphic to a function

Post by sumpm1 » Fri May 31, 2013 5:51 pm

dunbarx wrote:You are digging into the depths of the evaluation of arguments and parameters.

 put getX("grc rectangle1") into field "Field1"
Thank you, this worked.

So now I see that:

1. "graphic Rectangle1" evaluates to an object, or a reference to that object, or the entire container. And
2. graphic "Rectangle1" evaluates to THE CONTENTS of the container.

Would this be a correct interpretation? I think I get it, and it works. Again, I would like to read more on this. Is there something I can search the documentation for to read specifically on this?
There is a difference between passing a 'reference to an object', and passing 'the contents of the object' itself. Using a field as an example makes it clearer:

When I want to put the contents of a field into a variable myVar, I use:

CODE: SELECT ALL
put field "myField" into myVar

When I want to put a reference to a field into the variable, I can do it like this:

CODE: SELECT ALL
put the long id of field "myField" into myVar


Putting 'the field' directly will read the text content of the field and put that into the variable. The 'long id' function, on the other hand, will generate a string like "field 1 of card 1 of stack "myStack" - or something similar - and put that into the variable.

The function "the target", like "id", will generate a string which is a reference to the object. That is what your functions "getX" and "getY" seem to be expecting: a reference to the graphics object. But instead of passing that, you seem to be trying to pass the content of the graphic object itself. You can probably fix that by doing one of the 2 things below:

1) Use the "long id":
CODE: SELECT ALL
put getX(the long id of grc "rectangle1") into field "Field1"

2) Build a string that is an object reference:
CODE: SELECT ALL
put getX("grc" && quote & "rectangle1 " & quote) into field "Field1"
Thank you so much, I think that pretty much explains it all. I love this forum. And learning LiveCode is awesome. I would be stuck or bogged down with most other compiled languages by now, and I would still probably not be at the point that I could yet see the program run, if I could even build an interface.

I cannot describe enough how useful LiveCode will be (hopefully) to keep youngsters interested and progressing in the learning process.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”