Page 1 of 1

I'm not understanding the Exists function

Posted: Wed Mar 04, 2015 7:23 pm
by DavJans
exists(img "ncImg" of card "NonC")
if it is true then
delete img "ncImg"
end if

I get this error before it even gets to the if/then

button "Close": execution error at line 3 (Chunk: no such object), char 1

Re: I'm not understanding the Exists function

Posted: Wed Mar 04, 2015 7:27 pm
by sefrojones
try this:

Code: Select all

if exists(img "ncImg" of card "NonC")then
delete img "ncImg"
end if
--Sefro

EDIT: As far as I understand it, the exists() function will return true or false, the engine has no idea what to do with this information. In addition to the first example, you could also:

Code: Select all

put exists(img "ncImg" of card "NonC") into MyVar
if MyVar is true then
delete img "ncImg"
end if
or

Code: Select all

get exists(img "ncImg" of card "NonC")
if it is true then
delete img "ncImg"
end if)
 

Re: I'm not understanding the Exists function

Posted: Wed Mar 04, 2015 9:19 pm
by dunbarx
Hi.

What sefroJones said. But do you see what you did wrong in the beginning? You did not "do" anything with the function. You thought that it would go into the local variable "it", but never put it there. In fact, your line of code is only a fragment, since the engine could not understand what you intended, and reported so.

No different than if you asked:

"loc(field 1}"

Instead of, say, "answer loc (fld )" or "get loc (fld 1)"

Craig Newman

Re: I'm not understanding the Exists function

Posted: Thu Mar 05, 2015 4:13 pm
by DavJans
Thank you guys, great help. I think maybe the dictionary on this one is a little too vague for my understanding at the least.

Re: I'm not understanding the Exists function

Posted: Thu Mar 05, 2015 4:33 pm
by Klaus
Hi DavJans,

rule of thumb: A function always needs to be GET or PUT... INTO...:

Code: Select all

on mouseup
   put MyFunction() into myVar
   put tVar into fld X
end mouseup
A handler/command can "stand alone":

Code: Select all

on mouseup
  aHandler
end mouseup
Best

Klaus

Re: I'm not understanding the Exists function

Posted: Thu Mar 05, 2015 5:19 pm
by FourthWorld
Unlike C, Java, JavaScript, and some others, LiveCode has a distinction between commands and functions, similar to Pascal, SmallTalk, and a few other languages.

In LiveCode, every statement that isn't a control structure ("if", "else", "repeat", etc.) begins with a command.

Comnands perform actions, and are usually verbs ("get", "put", etc.), and they often operate on values.

Functions are sources of values, along with string literals, variables, or the values returned from querying properties. All sources of value will occur within the statement after the command operating on them.

I like to think of functions as placeholders, distinguished by their parentheses as though reminding us that something will be returned and put in that place.

As a simple example, here "AddTwoRandomNumbers" is the command, and "RandomNumber" is a function that returns a value for the command to operate on:

Code: Select all

on AddTwoRandomNumbers
  put RandomNumber() + RandomNumber() into fld "Display"
end AddTwoRandomNumbers

function RandomNumber
  return random(100)
end RandomNumber
That example is so simple it's almost silly (you could more easily just call the random function within the command), but hopefully it helps illustrate the distinction between commands and functions in LiveCode.

Re: I'm not understanding the Exists function

Posted: Thu Mar 05, 2015 6:45 pm
by jiml
Even if you do this:

Code: Select all

if exists(img "ncImg" of card "NonC") then
delete img "ncImg"
end if
you could still get your original error
execution error ... (Chunk: no such object),
if you are not currently on card "NonC" and there is no image 'ncImg" on the current card.

Try:

Code: Select all

if exists(img "ncImg" of card "NonC") then
delete img "ncImg" of card "NonC"
end if

Re: I'm not understanding the Exists function

Posted: Thu Mar 05, 2015 7:06 pm
by DavJans
You guys are all awesome, I learned a lot from this question.