LUA Questions

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

LUA Questions

by basil60 » Thu Sep 29, 2016 07:11

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

I've got:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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: 15
Joined: Mon Sep 19, 2016 09:00
In-game: titimoby

Re: LUA Questions

by TitiMoby » Thu Sep 29, 2016 07:23

is it the whole code ?
because I see

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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 » Thu Sep 29, 2016 08:11

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: 1874
Joined: Mon Jul 29, 2013 13:38
GitHub: tenplus1

Re: LUA Questions

by TenPlus1 » Thu Sep 29, 2016 09:18

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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)
 

Hybrid Dog
Member
 
Posts: 2460
Joined: Thu Nov 01, 2012 12:46

by Hybrid Dog » Thu Sep 29, 2016 09:56

Here's your code with indentations:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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 » Sun Oct 02, 2016 21:43

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


Return to Modding Discussion

Who is online

Users browsing this forum: Bing [Bot] and 4 guests

cron