* Clone() in Lua

For users to report plugin bugs and request plugin enhancements; and for authors to test new/new versions of plugins, and to discuss plugin development (in the Programming Technicalities sub-forum). If you want advice on choosing or using a plugin, please ask in General Usage or an appropriate sub-forum.
Post Reply
avatar
Kim Travis
Diamond
Posts: 51
Joined: 04 Jan 2018 21:17
Family Historian: V6

Clone() in Lua

Post by Kim Travis »

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: 28341
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Clone() in Lua

Post by tatewise »

See Tools > Plugins > More > How to Write Plugins as advised in plugins:getting_started|> 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 how_to:key_features_for_newcomers|> 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 ~ tatewise ancestry
avatar
Kim Travis
Diamond
Posts: 51
Joined: 04 Jan 2018 21:17
Family Historian: V6

Re: Clone() in Lua

Post by Kim Travis »

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...
avatar
Kim Travis
Diamond
Posts: 51
Joined: 04 Jan 2018 21:17
Family Historian: V6

Re: Clone() in Lua

Post by Kim Travis »

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: 28341
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Clone() in Lua

Post by tatewise »

As I said, a good starting point is in plugins:getting_started|> 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 ~ tatewise ancestry
avatar
Kim Travis
Diamond
Posts: 51
Joined: 04 Jan 2018 21:17
Family Historian: V6

Re: Clone() in Lua

Post by Kim Travis »

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
Post Reply