10 things you didnt know about LiveCode…well maybe.

by Steven Crighton on March 12, 2014 2 comments

Hi, I’m Steven. When I decided to write this blog post, I started to think … what can I write about? I had a browse through the great blog posts that came before me and It appeared obvious, you love the tech talk.

Being in Digital Marketing, what could I offer you in terms of tech talk that rivals what our incredible development team are talking about? It was at this point I got smart. My marketing genius shone through!

I challenged the dev team to come up with 10 things that they think are very useful in LiveCode but most users might not know about it.

A tricky challenge, considering that the experts in our community can likely show us a thing or two on how to use the LiveCode product, but the challenge was accepted.

Did I get 10 things from the team? Sure. Did I get 10 things that things that you don’t know about LiveCode? I guess that’s up to all of you, from beginners to experts, to let me know in the comments.

 

1 – Simple commands

Want to launch a web browser with a certain webpage? A single line is all you need “launch url “http://www.google.com”

 


2 – Variable checking is pretty cool

You can turn it on from the script editor “edit” menu.

It puts LiveCode into a stricter parsing mode requiring you to declare variables. For example

put 1 into tCount

Will now throw an error when you apply the script. You need to declare the variable before using it:

local tCount
put 1 into tCount

That might seem like extra work but once you start writing longer scripts you’ll love it! It will pick up on times when you miss-spell a variable name when you apply a script. Rather than spending ages debugging an app only to realise a miss-spelt variable was causing your issue, you’ll catch it right away.

 


3 – matrixMultiply

here is a LiveCode example of “matrixMultiply”

local tA1, tA2, tA3
put 1 into tA1[1,1]
put 2 into tA1[1,2]
put 3 into tA1[1,3]
put 4 into tA1[2,1]
put 5 into tA1[2,2]
put 6 into tA1[2,3]
put 7 into tA2[1,1]
put 8 into tA2[1,2]
put 9 into tA2[2,1]
put 10 into tA2[2,2]
put 11 into tA2[3,1]
put 12 into tA2[3,2]
put matrixMultiply (tA1, tA2) into tA3

This fundamentally replicates the example on the following link under “Multiplying a Matrix by Another Matrix”:
http://www.mathsisfun.com/algebra/matrix-multiplying.html
That page describes what a matrix multiplication is and provides some real world examples.

tA1 contains the information for the first matrix
tA2 contains the information for the second matrix
tA3 contains the result matrix

 


4 – Breakpoint command

In a script you can conditionally trigger the debugger using the breakpoint command. For example, you might want to pause execution if a particular variable is not within an expected range as follows:

if tVar < tMinValue or tVar > tMaxValue then
breakpoint
end if

If the breakpoint is triggered then you can check some of the values of variables to see what has caused tVar to have the incorrect value.

 


5- Move Command for Animation

A quick and easy way to animate your stacks is to use the move command. You can use the move command to move objects along a path or to various points on your stack.

 


6 – executionContexts

here is the information about the executionContexts:

I added the following code to the script of a button:

on mouseUp
command_one
end mouseUp

on command_one
command_two
end command_one

on command_one_and_a_half
command_two
endcommand_one_and_a_half

on command_two
put the executionContexts
end command_two

And this produced the following output in the message box:

button id 1004 of card id 1002 of stack “Untitled 1”,mouseUp,2
button id 1004 of card id 1002 of stack “Untitled 1”,command_one,6
button id 1004 of card id 1002 of stack “Untitled 1”,command_two,10

Notice that “command_one_and_a_half” was not called and is not listed.

From a developer point of view, this is very useful, as it tells you what path the code took to get to “the executionContexts”.
This can help you, especially in large applications, when trying to identify from where in the code something was called.
Think of a generic text file writing wrapper, that may be called to write log files or text files for user output. Using the executionContexts allows you to trace exactly what was parts of the code were called to get to the text file writing wrapper.

 


7 – export snapshot

The “export snapshot” command can also be used to output a raw bitmap image of the target – you do this by specifying “raw” as the output format. This will create a block of binary data similar to that used by the “imagedata” property of an image object. The data is composed of 4 bytes for every pixel in the image, in ARGB order – Alpha (opacity), Red, Green, and Blue. The “raw” format will be in ARGB order but you can also use the following – “argb, rgba, abgr, bgra” to specify the order of color channels in the data.

This means you can access the individual pixel data of a snapshot without first having to create an image object.

The following example takes a snapshot of a graphic object then creates an image from the raw data:

export snapshot from graphic 1 to timg as argb
create image
set the rect of the last image to the rect of graphic 1
set the imagedata of the last image to timg

// extract the alpha data from the raw data – every 4th byte starting from the 1st
local talpha
repeat with i = 1 to the number of bytes in timg step 4
put byte i of timg after talpha
end repeat

set the alphadata of the last image to talpha

 


8 – Improve performance of moving objects

If you have a lot of moving objects, improve performance by setting the acceleratedRendering to true, remembering to set the layerMode of any moving objects to dynamic.

 


9 – Shortcuts

  • Create a new card quickly with the keyboard shortcut command/ctrl + N
  • In the IDE, use command/ctrl + 3 to navigate to the next card in a stack and command/ctrl + 2 to navigate to the previous
  • Apply and dismiss the code editor using the keyboard shortcuts command/ctrl + return + return
  • Comment/uncomment blocks on code using the shortcuts command/ctrl + “-” and command/ctrl + shift + “-“
  • Debugging shortcuts: step into (command/ctrl + i), step over (command/ctrl + o), step out (command/ctrl + t), stop (command/ctrl + y)

 


10 – Processing Text

Lets say each of these identifiers needed to be turned into a line of the form:
{ @selector(<original>), kMCPlatformTextInputAction<toupper(char 1 of <original>)><char 2 to -2 of original> }
i.e.
capitalizeWord: => { @selector(capitalizeWord:), kMCPlatformTextInputActionCapitalizeWord }

Have the original in a TextEdit document, meaning you just have to write a small piece of code in the multi-line message box:
get the clipboardData[“text”]
put empty into tOutput
repeat for each line tLine in it
put format(“{ @selector(%s), kMCPlatformTextInputAction%s%s },n”, tLine, toUpper(char 1 of tLine), char 2 to -2 of tLine) after tOutput
end repeat
set the clipboardData[“text”] to tOutput

Here it processes the text on the clipboard and replaces it with the modified text which you can then paste directly into the source-file you were working on.

 


 

There you have it. 10 things you didn’t know about LiveCode but should. Were there any you already knew?

I have challenged the LiveCode team, i would now like to put the same challenge to you, the LiveCode community. Tell us something that you find really useful in LiveCode that we all should know about.

Share it in the comments below.

Steven Crighton10 things you didnt know about LiveCode…well maybe.

Related Posts

Take a look at these posts

2 comments

Join the conversation
  • The Boss - March 18, 2014 reply

    You are responsible for the marketing. Then how come, in the 1st example, you launched the web browser to another company’s website instead of to http://www.livecode.com/ ?

    That was a lousy job for someone who is paid to think about all the details. If that is how your marketing genius shines through, then, I am not impressed.

    Steven Crighton - March 19, 2014 reply

    I wouldn’t want you to get stuck in a loop since you’re already on LiveCode.com. Thanks for your comment.

Join the conversation

*