Using a substack as a library stack

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
scruffycoder
Posts: 3
Joined: Fri Jun 21, 2019 1:25 pm

Using a substack as a library stack

Post by scruffycoder » Fri Jun 21, 2019 1:35 pm

Hi All,

Sorry if this is a stupid question, but I’ve really been struggling to get a library stack working within a main stack. Basically I want to add some code to a substack and call it from the main stack, Here’s what I did based on what I’ve been able to find so far.

1. Created a new stack called MainApp
2. Added a substack this this called ScriptStack
3. In the script of ScriptStack I created a simple function like this:

Code: Select all

function sayHelloWorld
put "Hello World"
end sayHelloWorld
4. In the UI of MainApp I added a button and added this code to its script:
on mouseUp pButtonNumber

Code: Select all

start using stack "ScriptStack"
sayHelloWorld
end mouseUp
I thought that I’d be able to access the sayHelloWorld function like I have tried to do here, but I keep getting this error in the button script:

Code: Select all

button "Button": execution error at line 4 (Handler: can't find handler) near "sayHelloWorld", char 1
I’m pretty sure I must be doing something daft, but I have read a few posts and articles on the web which suggest that this should work.

Any help which can be offered would be gratefully received.

Many thanks

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

Re: Using a substack as a library stack

Post by Klaus » Fri Jun 21, 2019 1:56 pm

Hi scruffycoder,

welcome to the forum!

Well you wrote "function" but in fact this is more a command!
You can either write:

Code: Select all

command sayHelloWorld
   put "Hello World"
end sayHelloWorld
Of if you really want to use it as a function you have to use the "function" syntax in your "mouseup" handler:

Code: Select all

function sayHelloWorld
   put "Hello World"
end sayHelloWorld

Code: Select all

on mouseUp pButtonNumber
   get sayHelloWorld()
   ## or
   ## put sayHelloWorld() into the_dummy_var
  
   ## A function is meant to RETURN something, even if it actually doesn't 8-)
   ## So this syntax is neccessary.
end mouseUp
Best from germany

Klaus

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Using a substack as a library stack

Post by bogs » Fri Jun 21, 2019 2:01 pm

scruffycoder wrote:
Fri Jun 21, 2019 1:35 pm
Sorry if this is a stupid question,
I don't think it was stupid at all, I think it was an excellent question, and it gave Klaus a chance to clearly outline the differences between a function and a command.

It is almost always the nuances like this that make programming interesting :D
Image

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

Re: Using a substack as a library stack

Post by dunbarx » Fri Jun 21, 2019 4:05 pm

Hi.

Welcome.

There are no stupid questions. Only stupid answers.

Craig

scruffycoder
Posts: 3
Joined: Fri Jun 21, 2019 1:25 pm

Re: Using a substack as a library stack

Post by scruffycoder » Fri Jun 21, 2019 6:08 pm

Thanks so much for the warm welcome and encouragement everyone and thanks Klaus for the excellent explanation. I’ve had quite a few abortive attempts at switching to Livecode but this kind of thing has always got in my way and forced me to switch back to more familiar languages.

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

Re: Using a substack as a library stack

Post by dunbarx » Fri Jun 21, 2019 6:48 pm

Hi.

Know you can return a value from a command call as well as from a function call, but this is rarely done. The "return" control structure works in both. In a function:

Code: Select all

on mouseUp
   answer ultimateAnswer (6,9)
end mouseUp

function UltimateAnswer a,b
   put a * b into irrelevant
   return 42
end UltimateAnswer
In a command:

Code: Select all

local lifeTheUniverseAndEveryThing

on mouseUp
   put 6 * 9 into ultimateQuestion
   ultimateAnswer ultimateQuestion
   answer lifeTheUniverseAndEveryThing
end mouseUp

on UltimateAnswer irrelevant
   put 42 into lifeTheUniverseAndEveryThing
   return lifeTheUniverseAndEveryThing
end UltimateAnswer
The return here is not internally connected to its calling handler, as it is in a function call, so more stuff is required, like a script local variable so LC knows what is where. Just trivia...

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Using a substack as a library stack

Post by jacque » Sat Jun 22, 2019 5:28 pm

The return here is not internally connected to its calling handler, as it is in a function call, so more stuff is required, like a script local variable so LC knows what is where.
Actually, when used in a command handler, the caller can get the value in the result. That way you don't need a storage variable or property.

Code: Select all

on mouseUp
   put 6 * 9 into ultimateQuestion
   ultimateAnswer ultimateQuestion
   put the result into tAnswer
   answer tAnswer
end mouseUp

on UltimateAnswer irrelevant
   put 42 into lifeTheUniverseAndEveryThing
   return lifeTheUniverseAndEveryThing
end UltimateAnswer 
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Using a substack as a library stack

Post by dunbarx » Sat Jun 22, 2019 8:45 pm

Jacque.

True, certainly.

But the extra line to access the function "the result" is still an extra step, just like having to declare a local variable.

My point being that a function call needs neither. :wink:

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”