Page 1 of 1

Using a substack as a library stack

Posted: Fri Jun 21, 2019 1:35 pm
by scruffycoder
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

Re: Using a substack as a library stack

Posted: Fri Jun 21, 2019 1:56 pm
by Klaus
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

Re: Using a substack as a library stack

Posted: Fri Jun 21, 2019 2:01 pm
by bogs
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

Re: Using a substack as a library stack

Posted: Fri Jun 21, 2019 4:05 pm
by dunbarx
Hi.

Welcome.

There are no stupid questions. Only stupid answers.

Craig

Re: Using a substack as a library stack

Posted: Fri Jun 21, 2019 6:08 pm
by scruffycoder
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.

Re: Using a substack as a library stack

Posted: Fri Jun 21, 2019 6:48 pm
by dunbarx
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

Re: Using a substack as a library stack

Posted: Sat Jun 22, 2019 5:28 pm
by jacque
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 

Re: Using a substack as a library stack

Posted: Sat Jun 22, 2019 8:45 pm
by dunbarx
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