*Clone() in Lua

Writing and using plugins for Version 5 and above.
Kim Travis
Platinum
Posts: 30
Joined: 04 Jan 2018 21:17
Family Historian: V6

Clone() in Lua

Postby Kim Travis » 13 Mar 2018 22:45

Several plugin code examples use some kind of special trick or functionality called clone() to do something useful. I've tried to search for an explanation of what clone() is and what it does but cannot find any information. Must be some kind of copying process. Does someone know where I can find out more. Nothing in the Lua manual I consulted.

Thanks,
Kim

User avatar
tatewise
Megastar
Posts: 14833
Joined: 25 May 2010 11:00
Family Historian: V6.2
Location: Torbay, Devon, UK
Contact:

Re: Clone() in Lua

Postby tatewise » 13 Mar 2018 23:43

See Tools > Plugins > More > How to Write Plugins as advised in Knowledge Base > Getting Started Writing Plugins.
There the The Family Historian API describes all the FH interface commands that are additional to the standard Lua reference commands, and Clone() is part of the FH API not Lua.

That advice is reached in Knowledge Base > Key Features for Newcomers via the Plugins section and the Developer Guide.

You may understand that in programming languages some variables are passed by value and some by reference.
e.g. Most Lua data types are passed by value, but tables are passed by reference.
The FH user data types for pointers and dates are passed by reference.
So if you use ptrB = ptrA then if ptrA subsequently changes then so will ptrB.
To prevent that ptrB = ptrA:Clone() will pass the pointer by value so ptrB is unaffected by ptrA changes.
Mike Tate ~ researching the Tate and Scott family history.

Kim Travis
Platinum
Posts: 30
Joined: 04 Jan 2018 21:17
Family Historian: V6

Re: Clone() in Lua

Postby Kim Travis » 15 Mar 2018 19:32

Thanks - yes I see it there now, and thanks for you helpful explanation. I've worked my way through the various tuition and reference materials, but I was looking in the Lua sections and manual (and Google) for clone() as it seemed to me that it must be a Lua thing. Having helpful information spread between the FH help, knowledge base and Lua manual is starting to be problematic as I can't remember which one I read something useful in, and I bad at guessing what is a Lua thing and what an FH thing - must start taking detailed notes! The transition from 30yr-rusty Fortran programming experience is not easy...

Kim Travis
Platinum
Posts: 30
Joined: 04 Jan 2018 21:17
Family Historian: V6

Re: Clone() in Lua

Postby Kim Travis » 15 Mar 2018 20:15

Today I'm beginning to appreciate the depth of my lack of understand of Lua. I've read explanations of Lua's generic For statement and Lua tables half a dozen time and still don't fully understand. I think I will seek out an idiots guide to Lua. Quite happy with patterns though - they seem straightforward at least,

exasperated Kim

User avatar
tatewise
Megastar
Posts: 14833
Joined: 25 May 2010 11:00
Family Historian: V6.2
Location: Torbay, Devon, UK
Contact:

Re: Clone() in Lua

Postby tatewise » 15 Mar 2018 21:06

As I said, a good starting point is in Knowledge Base > Getting Started Writing Plugins where under Lua Language References in addition to the Lua 5.1 Reference Manual there are other guides. In particular Programming in Lua where the First edition is online.

I presume you found Tools > Plugins > More > How to Write Plugins which has some simple example scripts.

Generic for statements can be daunting, but extremely powerful.
Essentially it says for each time round the loop assign a set of variables to the values returned by a special function call that iterates through a set of values.
e.g.
for x, y in func() do
<execute these statements>
end
The 1st thing to recognise is that a func() can return a list of values; two in this example.
The 2nd thing is the most popular iterators are the standard ipairs() and pairs() functions.
e.g.
for x, y in ipairs(t) do
<execute these statements>
end
Where table t has indexed entries like t[1]=23, t[2]=97, t[3]=421 that are iterated over by ipairs().
So x & y on each loop will get the values 1 & 23, 2 & 97, 3 & 421

This illustrates one form of table; the simple integer indexed array.
The other form of table has named values like t["alpha"]=23, t["bravo"]=97, t["delta"]=421
This second form can also be written in short as t.alpha=23, t.bravo=97, and t.delta=421
It is this form that the pairs() function iterator caters for.
So x & y on each loop will get the values "alpha" & 23, "beta" & 97, "delta" & 421

Tables are extremely powerful, because both forms described above can exist together in the same table, and the values can be any of: a number, a string, a function, or even a table. So they can represent data structures that in other languages may be called arrays, linked lists, queues, etc.

The clue to what is an Lua thing and what is an FH thing is that FH API functions all start with fh and FH Objects dates & pointers are defined using fhNew...() and can have FH methods such as Clone() applied to them.
Mike Tate ~ researching the Tate and Scott family history.

Kim Travis
Platinum
Posts: 30
Joined: 04 Jan 2018 21:17
Family Historian: V6

Re: Clone() in Lua

Postby Kim Travis » 16 Mar 2018 21:42

That's a much clearer explanation than I've seen elsewhere Mike - thanks very much as ever. I get it now.

I've got a simplish first version of my Plugin debugged now - reckon I've worked through many beginners mistakes in the process, so it might get easier from now on....

Kim


Return to “Plugin Discussions”

Who is online

Users browsing this forum: No registered users and 5 guests