Questions about Lua scripting

Started by garry23, January 24, 2016, 06:48:32 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

garry23

I appreciate this thread may not even get traction, but it is my humble attempt to see if the 'experts' can support the 'newbies' on Lua scripting.

By copying the scripts of others, I am making slow progress on learning to script in ML-Lua.

But like most programming, I find myself struggling with some basics, as I'm not a programmer.

As an example. To print a message I would use

print "test message"

So far so good. But what if I wish to print a variable, ie for debugging. For example lens.focal_length. If I try

print "lens.focal_length" I simply get the text 'lens.focal_length'.

I've tried using the Lua tostring, but get errors.

Bottom line: if this post gets traction, I hope it can be used by all those interested in learning Lua: supported by those that are prepared to mentor others.

Danne

Good initiative.
I,m a little curious about this stuff too. Could this thread include working scripts that could be tested and maybe a short tutorial on what is needed to get the module and the existing scripts working?

Walter Schulz

And I would be thankful for some directions:
Start here, that's your sandbox where you can't damage that much.
And this is a minefield and you should only enter after gaining some experience.

DeafEyeJedi

Agreed. Even if it was a minefield ... I'd dig through carefully to gain some useful directions.

+1 @garry23 for starting this thread!

[emoji106]
5D3.113 | 5D3.123 | EOSM.203 | 7D.203 | 70D.112 | 100D.101 | EOSM2.* | 50D.109

dmilligan

In Lua and many other programming languages, quotation marks indicate a string literal. It's called a literal because we are telling the compiler/interpreter to stop evaluating and simply take whatever text is inside the quotation marks literally, as a string of text. There are several other ways to define a string literal in Lua: 'string literal' (single quote) and [[string literal]] (double brackets).

You've also found another feature of Lua and that is, you don't have to use () when calling functions:
print("Hello")
and
print "Hello"
are identical.

Typically most programmers would prefer including the () anyway since many languages we are used to require it. It makes it more clear what is going on, that is, we are calling a function.

To get what you want, simply leave off the quotation marks so that the string is actually executed rather than being interpreted as a literal (and I recommend adding some parenthesis for clarity)

print(lens.focal_length)


I realize that without programming background, it may be somewhat difficult to understand this document (for example in the section on string literals, it basically assumes you know what that means), but all of the Lua syntax is clearly defined here:
http://www.lua.org/manual/5.3/manual.html#3

There are also countless programming resources online. You might consider learning some basics in an easier environment, something with a nice IDE, syntax highlighting, debugger, auto-complete, etc. where you don't have to copy your programs to an SD card, load it into and camera, turn it on, and then run them on a tiny screen in an environment that is a reverse engineered, realtime, embedded system. I'm not trying to be mean, I just think you will get to where you want to be faster if you do it like this. Once you get some of the basics down, then try and apply them to ML/Lua.

garry23

@dmilligan

Many thanks for taking the time to 'kick start' this resource. I've downloaded the Lua link to my iPad so I can read and browse at my leisure.

Once again: thanks for taking the time to start the education process  :)

Cheers

Garry


garry23

A question for one of the Lua experts, ie willing to help newbies like me  :)

I think I understand the role and power of the Lua tables, but I have got myself confused by trying to reverse engineer one of the Lua examples: http://davidmilligan.github.io/ml-lua/examples/calc.lua.html

I can not see how the calc. table becomes self.

Hope someone can educate me.

Cheers

Garry

dmilligan

Quote from: garry23 on January 29, 2016, 09:17:37 AM
I can not see how the calc. table becomes self.
syntatic sugar

a function definition like this
function foo:bar(p1)
is the same thing as:
foo.bar = function(self, p1)

and when you call the function, it turns from this:
foo:bar("hello")
into this:
foo.bar(foo, "hello")

This is Lua's way of doing object oriented programming.