My first Function - but where to put it.

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

My first Function - but where to put it.

Post by bjb007 » Sat Feb 23, 2008 3:00 am

Written my first function and tested it.
However no matter where I put it I
get a message "Can't find function..."

Tried "card id xxxx of stack "....myprog.rev"
and stack "myprog".

Suggestions please.
Life is just a bowl of cherries.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sat Feb 23, 2008 3:09 am

bjb007,

Where did you put the function?

If you want to call the function only from one particular script, I'd put the function into that script (e.g. a button). If you need the function in one substack, you can put the function into the stack script of that substack. If you need it in the entire project, put it into the script of the mainstack. If you need it for many projects, keep it in a library and "start using" the library whenever you need the function.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Library

Post by bjb007 » Sat Feb 23, 2008 3:53 am

Mark

Thanks for the tips.

I like the idea of making a library of functions
which was common in pre-windows days.

Your functions were compiled into a *.lib
file and the lib file "included". The individual
functions were called by name.

The function (or was it the whole *.lib file?) was
included in the compiled prog.

So the questions is how much like the
above is the RR way?

Are functions from a library file
compiled with the *.exe? or appear in
the "Externals" directory?
Life is just a bowl of cherries.

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

My first Function - but where to put it.

Post by bjb007 » Sat Feb 23, 2008 4:58 am

Mark
I have ten buttons and want them all
to execute the function. As an example:

Button01, label 1

In script of "Button01"

code: put label of target into aNumber

OK aNumber = 1

Function name is "SetMoreOrLess"

Still in script of "Button01"

SetMoreOrLess aNumber <--stops on this line
--------------------------------------------------------
Handler: can't find handler
Object: Button01
Line: SetMoreOrLess aNumber
Hint: SetMoreOrLess
---------------------------------------------------------

Debugger doesn't go to the function so either
can't find it (how do I tell?) or some other error.
Don't understand the reference to Button01.
Probably irrelevant?
Life is just a bowl of cherries.

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

Post by Klaus » Sat Feb 23, 2008 4:48 pm

Could you please post the complete scripts of the function itself and the "mouseup" (or whatever) handler that shall trigger it?

Guessing takes too much time ;-)

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sat Feb 23, 2008 5:21 pm

Hi bjb007,

As Klaus says, we will need more info.

All I can so so far is that the offending command is indeed in the script of button "Button01".

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

I would but...

Post by bjb007 » Sun Feb 24, 2008 12:27 am

Would like to post the code but have
run into a problem with Rev when I
started it this morning.

When I tried to open myProg.rev I got
a message that there was already a copy
in memory from the Rev "toolset" directory
named "home.rev".

Don't know how it got there but I deleted
it and restarted Rev. Same result.

Closed Rev, deleted the file, re-booted.
Now I get "Error while initializing
development environment" message.

Before I reinstall Rev does anyone have
an explanation or a quick fix?

PS I could put the code in my "User Space"
if I ever get a response to my request made
some weeks ago ago.
Life is just a bowl of cherries.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sun Feb 24, 2008 12:43 am

bjb007,

Somehow, you deleted a part of the Revolution software, specifically the Home stack. You need to re-install Revolution. Make sure to delete/uninstall the corrupted copy of Revolution first!

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Reinstall

Post by bjb007 » Sun Feb 24, 2008 1:04 am

Reinstalled and find that, as you say, "home.rev"
is part of the system.

Wonder why I had a message that "myProg.rev"
from the "Toolset" directory and named "home.rev"
was already in memory?

I backed up all my files. Rev won't install
in the same directory over an existing copy.
Would have made things simpler. Many
progs. allow a reinstall in an existing
directory without affecting files other than
its own.
Life is just a bowl of cherries.

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Test Stack

Post by bjb007 » Sun Feb 24, 2008 2:14 am

Mark and Klaus - and everyone else too.


I've done a small prog using
the code - RKsubTest.rev

The link is "snipped" and will take
you to Wikifortio.com.

Download it here in a zip file...

http://snipurl.com/209qh

and follow along!
Life is just a bowl of cherries.

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

Post by Klaus » Sun Feb 24, 2008 12:21 pm

Hi bjb,

hmm, strange code going on ;-)

OK, I put this into the stackscript, please see my annotations:

Code: Select all

function SetOddAndEven aNumber
  if aNumber is among the items of "1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32" then
  ## You should QUOTE strings like this item list.
  ## ALWAYS quote NAMES of objects!

  ## No need for an if else, since you will add 1 to the fields anayway :-)
      add 1 to field "lblOdd"
      add 1 to field "lblEven"
end SetOddAndEven

And here is how to call it from your buttons:

Code: Select all

on mouseUp
  ## put label of target into aNumber 
  ## I would rather:
  put the label of me into aNumber

  ## You have to get or put the results of functions into something!
  get SetOddAndEven(aNumber)
end mouseUp
Will work with every button on every card in your stack, as long as it has a number in its label
and there are fields named "lblOdd" and "lblEven" on the current card!

Hint:
In the script of your button you use:

Code: Select all

 
   ...
   if aNumber = 1 then
   ## Here you sure mean FIELD "lblOdd" but write it without quotes,
   ## so the engine thinks you mean a variable, wich does not exist an so the evaluation will
   ## always end in false: if lblOdd = 0
    if lblOdd = 0 then
      put 1 into field lblOdd
   ...
...
Best

Klaus

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sun Feb 24, 2008 12:52 pm

Code: Select all

  end if
end if
;-)

The way it effectively works now, no if-statement is necessary at all.
Just to avoid confusion, an if-statement should finished with "end if":

Code: Select all

if <condition> then
  -- do something
end if

if <condition> or <condition> and <condition> then
  -- do something
else if <condition> or <condition> then
  -- do something else
else -- all other conditions
  -- do a default thing
end if

Note that <condition> must always evaluate to true or false.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

First Function

Post by bjb007 » Sun Feb 24, 2008 4:45 pm

Klaus and Mark

I was looking for something to put
in front of the subroutine name - but
didn't try "get" so expect that was
part of the problem

The amended prog (with more buttons!)
is here...

http://snipurl.com/20aqd
Life is just a bowl of cherries.

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”