I'm not understanding the Exists 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: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
DavJans
Posts: 273
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

I'm not understanding the Exists function

Post by DavJans » Wed Mar 04, 2015 7:23 pm

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
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: I'm not understanding the Exists function

Post by sefrojones » Wed Mar 04, 2015 7:27 pm

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)
 

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

Re: I'm not understanding the Exists function

Post by dunbarx » Wed Mar 04, 2015 9:19 pm

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

DavJans
Posts: 273
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

Re: I'm not understanding the Exists function

Post by DavJans » Thu Mar 05, 2015 4:13 pm

Thank you guys, great help. I think maybe the dictionary on this one is a little too vague for my understanding at the least.
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: I'm not understanding the Exists function

Post by Klaus » Thu Mar 05, 2015 4:33 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9867
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: I'm not understanding the Exists function

Post by FourthWorld » Thu Mar 05, 2015 5:19 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: I'm not understanding the Exists function

Post by jiml » Thu Mar 05, 2015 6:45 pm

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

DavJans
Posts: 273
Joined: Thu Dec 12, 2013 4:21 pm
Location: Spokane, WA USA

Re: I'm not understanding the Exists function

Post by DavJans » Thu Mar 05, 2015 7:06 pm

You guys are all awesome, I learned a lot from this question.
"Det bästa stället att hitta en hjälpande hand är i slutet av din egen arm" förutom här

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”