LUA Questions

Post Reply
basil60
Member
Posts: 54
Joined: Sat Sep 12, 2015 22:07

LUA Questions

by basil60 » Post

Hi
I'm trying to Learn LUA, and programmatically laying out 5 blocks side by side.

I've got:

Code: Select all

minetest.after(20,
function(pos)
pos.x=-27.6
for count = 1, 5, 1 do
minetest.set_node({pos, y=2.5, z=-21.2}, {name="default:dirt"})
pos.x=pos.x + 1
end
end
)
but it gives me an error:

Code: Select all

2016-09-29 17:10:09: ERROR[main]: ServerError: Runtime error from mod 'lua_code'
 in callback ScriptApiEnv::environment_Step(): ...il_3\Desktop\minetest-Beth\bin
\..\mods\lua_code\init.lua:15: attempt to index local 'pos' (a nil value)
2016-09-29 17:10:09: ERROR[main]: stack traceback:
2016-09-29 17:10:09: ERROR[main]:       ...il_3\Desktop\minetest-Beth\bin\..\mod
s\lua_code\init.lua:15: in function 'func'
2016-09-29 17:10:09: ERROR[main]:       ...sil_3\Desktop\minetest-Beth\bin\..\bu
iltin\game\misc.lua:18: in function 'update_timers'
2016-09-29 17:10:09: ERROR[main]:       ...sil_3\Desktop\minetest-Beth\bin\..\bu
iltin\game\misc.lua:50: in function <...sil_3\Desktop\minetest-Beth\bin\..\built
in\game\misc.lua:38>
2016-09-29 17:10:09: ERROR[main]:       ...3\Desktop\minetest-Beth\bin\..\builti
n\game\register.lua:355: in function <...3\Desktop\minetest-Beth\bin\..\builtin\
game\register.lua:335>
I'd appreciate any advice.

TitiMoby
Member
Posts: 17
Joined: Mon Sep 19, 2016 09:00
In-game: titimoby

Re: LUA Questions

by TitiMoby » Post

is it the whole code ?
because I see

Code: Select all

function(pos)
but didn't see any initialisation or call to this function with a value

basil60
Member
Posts: 54
Joined: Sat Sep 12, 2015 22:07

Re: LUA Questions

by basil60 » Post

Yep that's it
If I've got it wrong (and I obviously have) please tell me.

Just giving it a bash - and having a go. Not always with success!

User avatar
TenPlus1
Member
Posts: 3715
Joined: Mon Jul 29, 2013 13:38
In-game: TenPlus1
Contact:

Re: LUA Questions

by TenPlus1 » Post

Code: Select all

minetest.after(20, function()
    local pos = {x = -27, y = 3, z = 21}
    for count = 1, 5 do
        minetest.set_node({x = pos.x + count, y = pos.y, z = pos.z}, {name="default:mese"})
    end
end)

User avatar
Hybrid Dog
Member
Posts: 2828
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

by Hybrid Dog » Post

Here's your code with indentations:

Code: Select all

minetest.after(20,
	function(pos)
		pos.x = -27.6
		for count = 1, 5, 1 do
			minetest.set_node({pos, y = 2.5, z = -21.2}, {name = "default:dirt"})
			pos.x = pos.x + 1
		end
	end
)
The parameter pos is nil because you didn't pass something as third argument in the minetest.after function.
So you need to create the pos table yourself:

Code: Select all

minetest.after(20,
	function()
		local pos = {}
		pos.x = -27.6
		for count = 1, 5, 1 do
			minetest.set_node({pos, y = 2.5, z = -21.2}, {name = "default:dirt"})
			pos.x = pos.x + 1
		end
	end
)
The first argument passed in minetest.set_node is not a position ({x = …, y = …, z = …}) but something different ({[1] = {x = …}, y = …, z = …}).
Since y and z is always the same, you can change the x field every time in the loop and pass pos as first argument in minetest.set_node,
and to make the code easier to understand, you can use _ instead of count as name for the unused variable and omit the third number in the for loop because it's 1 by default:

Code: Select all

minetest.after(20,
	function()
		local pos = {x = -27.6, y = 2.5, z = -21.2}
		for _ = 1, 5 do
			minetest.set_node(pos, {name = "default:dirt"})
			pos.x = pos.x + 1
		end
	end
)
The position consists of the x, y and z field, each of these fields is cast (converted) to a 16 bit signed integer when dispatching it to minetest.
So the numbers can be floored:

Code: Select all

minetest.after(20,
	function()
		local pos = {x = -27, y = 2, z = -21}
		for _ = 1, 5 do
			minetest.set_node(pos, {name = "default:dirt"})
			pos.x = pos.x + 1
		end
	end
)
In comparison to 11's code, the 5 nodes are not 1 m offset to x direction.

Anyway, it is more comfortable to execute code when placing a specific node:

Code: Select all

local function mything(pos)
	for _ = 1, 5 do
		minetest.set_node(pos, {name = "default:dirt"})
		pos.x = pos.x + 1
	end
end

minetest.register_node(":msa:blah", {
	description = "testblock",
	tiles = {"default_mese_block.png"},
	groups = {snappy=1,bendy=2,cracky=1},
	sounds = default.node_sound_stone_defaults(),
	on_construct = mything,
})

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

basil60
Member
Posts: 54
Joined: Sat Sep 12, 2015 22:07

Re: LUA Questions

by basil60 » Post

Thanks 10Plus1, thanks Hybrid Dog
I've been away. Appreciate you both taking the time

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests