project bild

Post Reply
crlyttle
Member
Posts: 64
Joined: Tue Jun 29, 2021 19:28
In-game: crl

project bild

by crlyttle » Post

I would like to break my large project into smaller files to aid in version control. I'm writing a geometry mod that will have many functions such as draw_line, draw_circle, etc. The "function draw_line(p1,p2,name)" should be in a file draw_line.lua and callable from other functions both inside and outside of the geometry mod.

How can this be done?

User avatar
cx384
Member
Posts: 653
Joined: Wed Apr 23, 2014 09:38
GitHub: cx384
IRC: cx384

Re: project bild

by cx384 » Post

with dofile(minetest.get_modpath("geometry") .. "/my_second_file.lua")
https://rubenwardy.com/minetest_modding ... ua-scripts

you probably want to make your functions globally accessible so write for example in the three files init.lua, file1.lua and file2.lua.

init.lua:

Code: Select all

geometry = {}
local path = minetest.get_modpath("geometry")
dofile(path .. "/file1.lua")
dofile(path .. "/file2.lua")
file1.lua:

Code: Select all

function geometry.myfunc(text)
	print(text)
end
file2.lua:

Code: Select all

geometry.myfunc("hello file2")

(moved this topic from General Discussion to Modding Discussion)
Can your read this?

crlyttle
Member
Posts: 64
Joined: Tue Jun 29, 2021 19:28
In-game: crl

Re: project bild

by crlyttle » Post

Thanks for the reply, but that is what I trying to avoid.
I have a mod named geometry.
The directory has (at least) the following files. each containing one function:
init.lua
draw_line.lua --function draw_line(P1,P2,name)
draw_circle.lua --function draw_circle(Center, Radius, name)
draw_rectangle.lua --function draw_rectalgle(P1,P2,P3,P4, name)
draw_triangle.lua ...
etc.
I would like to make the functions available inside or outside the geometry mod, perhaps by calling
something like

geometry.draw_line(p1,p2,"default:dirt")

The reason for doing this is to aid in development, testing, and version control. If I can structure directories like this, I can checkout and work on one file without disturbing the entire program and assign individual files to other developers.

User avatar
cx384
Member
Posts: 653
Joined: Wed Apr 23, 2014 09:38
GitHub: cx384
IRC: cx384

Re: project bild

by cx384 » Post

crlyttle wrote:
Wed Sep 15, 2021 15:35
Thanks for the reply, but that is what I trying to avoid.
I have a mod named geometry.
The directory has (at least) the following files. each containing one function:
init.lua
draw_line.lua --function draw_line(P1,P2,name)
draw_circle.lua --function draw_circle(Center, Radius, name)
draw_rectangle.lua --function draw_rectalgle(P1,P2,P3,P4, name)
draw_triangle.lua ...
etc.
I would like to make the functions available inside or outside the geometry mod, perhaps by calling
something like

geometry.draw_line(p1,p2,"default:dirt")

The reason for doing this is to aid in development, testing, and version control. If I can structure directories like this, I can checkout and work on one file without disturbing the entire program and assign individual files to other developers.
Sorry I was editing my post and probably already answered your question.
(if you want to use them inside other mods you have to add your mod to the mod.conf depends list)

Also look here if you are unsure how to get a good code architecture:
https://rubenwardy.com/minetest_modding ... _arch.html
Can your read this?

crlyttle
Member
Posts: 64
Joined: Tue Jun 29, 2021 19:28
In-game: crl

Re: project build

by crlyttle » Post

I found the following on https://www.tutorialspoint.com/lua/lua_modules.htm discussing the require function:
<q>
In order to run this code, we need to place the two Lua files in the same directory or alternatively, you can place the module file in the package path and it needs additional setup. When we run the above program, we will get the following output.
</q>

Does anyone know what additional setup is required?

User avatar
Blockhead
Member
Posts: 1602
Joined: Wed Jul 17, 2019 10:14
GitHub: Montandalar
IRC: Blockhead256
In-game: Blockhead Blockhead256
Location: Land Down Under
Contact:

Re: project build

by Blockhead » Post

crlyttle wrote:
Thu Sep 16, 2021 12:59
I found the following on https://www.tutorialspoint.com/lua/lua_modules.htm discussing the require function:
<q>
In order to run this code, we need to place the two Lua files in the same directory or alternatively, you can place the module file in the package path and it needs additional setup. When we run the above program, we will get the following output.
</q>

Does anyone know what additional setup is required?
If you want a concrete example of how to use require in minetest, see the serialize_lib code in advtrains.

Basically, at the bottom of a file you just need to put a return statement that returns a table. That table is the public API of your module. Calling require() on that file then returns that table back to the caller, giving you access to the functions and data.

It's a good way to avoid polluting the global namespace with mod-internal code because if you did local foobaz = require("foobaz") then you have access to the functions in that other file, but your local foobaz won't be visible to other mods and possibly cause trouble. If you do want visibility to other mods, you still want to make a global table that holds your mod's public API.

Also wanted to say that you don't need to worry about multiple people working on the same file in git, unless they both intend to modify the same function or someone does something out of the ordinary like moving a function up/down the file. If two different functions are modified by two different people, git can merge both changes automatically in most cases.
/˳˳_˳˳]_[˳˳_˳˳]_[˳˳_˳˳\ Advtrains enthusiast | My map: Noah's Railyard | My Content on ContentDB ✝️♂

Astrobe
Member
Posts: 568
Joined: Sun Apr 01, 2018 10:46

Re: project bild

by Astrobe » Post

crlyttle wrote:
Wed Sep 15, 2021 15:35
The reason for doing this is to aid in development, testing, and version control. If I can structure directories like this, I can checkout and work on one file without disturbing the entire program and assign individual files to other developers.
Kids, don't do this at home.

Checking out one file and leaving the rest intact will cause you troubles as soon as you start having direct or tacit dependencies between the files. BTW The thing you describe resemble a style that was abandoned a long, long time ago for good reasons: it's a hucking PITA after all.

Moreover, I can very much guarantee you that it is not the way developers worth their salt usually work. You will just strike them as someone doing "funny" things for weird reasons (well, that's the family-friendly version; the actual thing has the F word every three words in each sentence, and that's the basic decoration level).

Look at what others do and what tools encourage you to do, that's usually the right way to do things (avoid Lua mods for that though, as it's on the contrary not unusual to see trainwrecks caused by false good ideas like yours).

This means just using Git's "branches and merges" model, and just put related functions in the one file.

crlyttle
Member
Posts: 64
Joined: Tue Jun 29, 2021 19:28
In-game: crl

Re: project bild

by crlyttle » Post

I've been doing things that way for almost 60 years. GE, Raytheon, Microsoft, and every other big outfit I've worked with has done it that way at least from the mid 1970's. After the advent of Object Oriented programming, it became one-class-one-file.
Basic, csh, bash, perl, and python are examples of interpreted languages that now support that style. LUA claims to be object oriented, but I haven't found that yet. It is just an observation, not a criticism, that LUA programmers seem to put everything into one big program. That won't work for the project I have in mind.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests