Page 1 of 1

I can insert a lua script in my mod?

PostPosted: Mon Dec 28, 2015 17:49
by MineYoshi
I mean i can insert a little and simple lua program as a calculator in my mod?
insert the script and by example:
i touch a calculator block now i have in minetest a lua calculator!
How i make this!

Re: I can insert a lua script in my mod?

PostPosted: Tue Jan 05, 2016 21:10
by AiTechEye
looks like you need to learn some coding :-)
its easiest way is to look in other people's mods and see how they is doing & try it and much testing

here is a calculator block
the block mod-name is excalc:calcblock you need to change it if you want to use it in another mod

1: http://dev.minetest.net/Intro
2: http://dev.minetest.net/Category:Methods
3: http://dev.minetest.net/formspec
Code: Select all
function excalc_form(pos,text)
local meta = minetest.get_meta(pos)

if not text then               -- if void (nil)
   text=""
end

meta:set_string("formspec",
   "size[8,3]" ..            --the window
   "button[0,0; 1.5,1;but1;+]" ..
   "button[2,0; 1.5,1;but2;-]" ..
   "button[4,0; 1.5,1;but3;x]" ..
   "button[6,0; 1.5,1;but4;/]" ..
   "textarea[0,1;4,1;text1;Calculate;]"..
   "textarea[4.6,1;4,1;text2;with;]"..
   "textarea[0,2.5;9,1;text3;Output; ".. text .."]"   -- ("") text string (..) adding to textstring
)
end


minetest.register_node("excalc:calcblock", {
   description = "Calculator block",
   tiles = {"default_steel_block.png"},
   groups = {dig_immediate = 3,not_in_creative_inventory = 0},
   paramtype2 = "facedir",
   light_source=0,
   sounds=default.node_sound_stone_defaults(),
after_place_node = function(pos, placer, itemstack)
      local meta = minetest.get_meta(pos)
      meta:set_string("infotext", "Calculator block")
      excalc_form(pos)
      end,
on_receive_fields = function(pos, formname, fields, sender)
      local meta = minetest.get_meta(pos)      --get block text / data
      local num1 = tonumber(fields.text1)   -- comverts text to number
      local num2 = tonumber(fields.text2)   -- comverts text to number
      if num1==nil or num2==nil then   -- will crash if someone is empty
         num1=0
         num2=0
      end
      if fields.but1 then
         excalc_form(pos,num1+num2)
      end
      if fields.but2 then
         excalc_form(pos,num1-num2)
      end
      if fields.but3 then
         excalc_form(pos,num1*num2)
      end
      if fields.but4 then
         excalc_form(pos,num1/num2)
      end
end,
})



Re: I can insert a lua script in my mod?

PostPosted: Sat Feb 20, 2016 00:04
by MineYoshi
Thanks! Dude