Page 1 of 1

Items producing light

PostPosted: Wed May 25, 2016 00:22
by midimaze
How do I make an item produce light while held in hand?
I've been looking all over tutorials and can't figure this out.
(I hope this belongs here)

Re: Items producing light

PostPosted: Wed May 25, 2016 00:48
by ExeterDad
There's been a walking light that you can wield around for a while. It's known to be a bit laggy and requires shaders I believe.
Sofar has about the most updated code I believe in his torch replacement mod.
viewtopic.php?f=11&t=14359&p=213834#p213834
Study the mod to figure it.

Re: Items producing light

PostPosted: Wed May 25, 2016 01:35
by midimaze
Thanks, I'll go check that out.

Re: Items producing light

PostPosted: Fri May 27, 2016 08:28
by AiTechEye
The light is produced by a node/block that replacing/updating it self.

this is a bit remade but fully working mod taken from the vaxcazer.
It works in water & turns of automacly in day light + after a while inside a wall.

vexcazer_flashlight.zip
(1.1 KiB) Downloaded 164 times


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
vexcazer_flashlight={users={},timer=0}

minetest.register_on_leaveplayer(function(player)
   local name=player:get_player_name()
   if vexcazer_flashlight.users[name]~=nil then vexcazer_flashlight.users[name]=nil end
end)


minetest.register_tool("vexcazer_flashlight:fl", {
   description ="flashlight (USE = on .. PLACE = off .. It works in water and turns off in light)",
   inventory_image = "default_stick.png",
   on_use=function(itemstack, user, pointed_thing)
      local name=user:get_player_name()
      local index=user:get_wield_index()
      vexcazer_flashlight.users[name]={player=user,slot=index,inside=0,item=user:get_inventory():get_stack("main", index):get_name()}
   end,
   on_place=function(itemstack, user, pointed_thing)
      local name=user:get_player_name()
      vexcazer_flashlight.users[name]=nil
   end,
})

minetest.register_node("vexcazer_flashlight:flht", {
   description = "Flashlight source",
   light_source = 12,
   paramtype = "light",
   walkable=false,
   drawtype = "airlike",
   pointable=false,
   buildable_to=true,
   sunlight_propagates = true,
   groups = {not_in_creative_inventory=1},
   on_construct=function(pos)
      minetest.env:get_node_timer(pos):start(1.5)
   end,
   on_timer = function (pos, elapsed)
      minetest.set_node(pos, {name="air"})
   end,
})

minetest.register_node("vexcazer_flashlight:flhtw", {
   description = "Water light",
   drawtype = "liquid",
   tiles = {"default_water.png"},
   alpha = 160,
   light_source = 12,
   paramtype = "light",
   walkable = false,
   pointable = false,
   diggable = false,
   drop = "",
   liquid_viscosity = 1,
   liquidtype = "source",
   liquid_alternative_flowing="vexcazer_flashlight:flhtw",
   liquid_alternative_source="vexcazer_flashlight:flhtw",
   liquid_renewable = false,
   liquid_range = 0,
   drowning = 1,
   sunlight_propagates = true,
   post_effect_color = {a = 103, r = 30, g = 60, b = 90},
   groups = {water = 3, liquid = 3, puts_out_fire = 1},
   on_construct=function(pos)
      minetest.env:get_node_timer(pos):start(1.5)
   end,
   on_timer = function (pos, elapsed)
      minetest.set_node(pos, {name="air"})
   end,
})

minetest.register_globalstep(function(dtime)
   vexcazer_flashlight.timer=vexcazer_flashlight.timer+dtime
   if vexcazer_flashlight.timer>1 then
      vexcazer_flashlight.timer=0
      for i,ob in pairs(vexcazer_flashlight.users) do
         local name=ob.player:get_inventory():get_stack("main", ob.slot):get_name()
         local pos=ob.player:getpos()
         pos.y=pos.y+1.5
         local n=minetest.get_node(pos).name
         local light=minetest.get_node_light(pos)
         if light==nil then
            vexcazer_flashlight.users[i]=nil
            return false
         end
         if ob.inside>10 or name==nil or name~=ob.item or minetest.get_node_light(pos)>12 then
            vexcazer_flashlight.users[i]=nil
         elseif n=="air" or n=="vexcazer_flashlight:flht" then
            minetest.set_node(pos, {name="vexcazer_flashlight:flht"})
         elseif minetest.get_node_group(n, "water")>0 then
            minetest.set_node(pos, {name="vexcazer_flashlight:flhtw"})
         else
            ob.inside=ob.inside+1
         end
      end
   end
end)

Re: Items producing light

PostPosted: Fri May 27, 2016 11:26
by oleastre
Another example is the torches mod: viewtopic.php?f=11&t=14359