First off, I like that it reduces brightness the further away it goes (like real life!)
Second, I'd like to see it gain the ability to toggle the flashlight on/off by right click too!
-- Line 141 to line 165 Rewritten
-- A: Make the function that swiches back and forth into a local function
local function flashlight_toggle(stack)
if stack:get_name() == "goodtorch:flashlight_off" then
minetest.sound_play("goodtorch_on")
stack:set_name("goodtorch:flashlight_on")
else
minetest.sound_play("goodtorch_off")
stack:set_name("goodtorch:flashlight_off")
end
return stack
end
-- B: Update the register functions to use on_use (left click/punch) and on_secondary_use (right click/place)
minetest.register_craftitem("goodtorch:flashlight_off", {
description = "Flashlight (off)",
inventory_image = "goodtorch_flashlight_off.png",
on_use = flashlight_toggle,
on_secondary_use = flashlight_toggle,
groups = {
flash_light = 1,
},
})
minetest.register_craftitem("goodtorch:flashlight_on", {
description = "Flashlight (on)",
inventory_image = "goodtorch_flashlight_on.png",
on_use = flashlight_toggle,
on_secondary_use = flashlight_toggle,
groups = {
flash_light = 1,
},
})
-- C: Profit! :)
Thanks a lot for your dandy review and for this code suggestion. I honestly wanted to add a right-click flashlight toggle, but did not know how (I was not aware of on_secondary_use yet) and was quite tired after figuring out why did it crash after shining the flashlight far into the distance that exceeds 100 nodes. The solution was to simply return "goodtorch:light_0" (thank you, illumination, for the idea of light blocks) if i exceeds 100. I can probably optimize it further and just not set any nodes if not required, as 0-light air nodes are essentially equal to air, but waste the performance. I will test your code out tomorrow as I have to go to sleep now, and I shall return with more epic mod ideas at any time soon.
P.S.: I didn't even look at indentation until you noted it in your further comment, so worry not, pal, about this miniscule issue.
Not to promote my site but my site has some good resources I personally use.
https://beanzilla.net/minetest.html (Beanzilla was my old nickname, when I was heavy into just playing Minecraft, then found Minetest then chose a new nickname, TLDR old nickname) :)
Includes direct links to the Lua minetest api docs (just CTRL + F and type functions/whatnot to find)
And a good book by Rubenwardy. (Highly recommend mixing between the 2, like I do)
Limiting light range to 99 or less (100 is not included, change from dist < 100 to dist <= 100 to include 100) is simply:
-- Line 66 (Rewritten)
local function update_illumination(player)
local name = player:get_player_name()
if not player_lights[name] then
return -- Player has just joined/left
end
local player_pos = player:get_pos() -- Grab the user's current position
local pos = get_light_node(player).pos
local old_pos = player_lights[name].pos
local node = get_light_node(player).l
-- minetest.set_node(vector.round(pos), {name = "default:steelblock"})
-- Check if illumination needs updating
if old_pos and pos then
if vector.equals(pos, old_pos) then
return -- Already has illumination
end
end
-- Update illumination
player_lights[name].pos = pos
if node then
local new_pos = get_light_node(player).pos
local dist = vector.distance(player_pos, new_pos) -- Obtain their distance, used below to compare against 99 or less (100 not included)
if new_pos and (minetest.get_node(new_pos).name == "air") and dist < 100 then
minetest.set_node(new_pos, {name = node})
if old_pos and not vector.equals(old_pos, new_pos) then
remove_light(old_pos)
end
player_lights[name].pos = new_pos
return
end
end
-- No illumination
remove_light(old_pos)
player_lights[name].pos = nil
end
If it's alright with you I'd like to release a copy that also works with water nodes (water source, water flowing, river water source, river water flowing).
I'll keep the license the same (mainly because of the license). :)
And attribute it to you, since you first made it possible, then I took a hack at it.
Yes underwater lighting is now possible, and no it actually can drown the player instead of possibly free air. (defines the water nodes into light
water nodes)
Was a bit tricky to get the lighting right as I had a bug where it would shoot thru into caverns instead of just lighting the first non-replaceable node. (but I got it working, and if you don't want me to release it I'll not, and I'd probably not use it either, just to respect your choice)
Edit: If you want to see the mod and try it out it's in a un-released state on github (yes technically it's released there, but not here, at least not without your permission/request/opinion) Here
Edit: Wow, I've done a lot within the last couple hours, and posted a lot too. (One might think I'm super good at modding, oh wait, I'm not really good, it's more that I've done more mods so I kind of know some dos and dont's, like spelling, with no spell check! Nooo, :P )
Aye, I woke up a few hours ago, now it's time to test out all your dandy ideas. I think that there might be an issue with toggling the flashlight with only right mouse button, since one may wish to use this button while pointing on a useable node, and I don't know if the flashlight or the node will be activated first. It's better, I think, to temporarily disable right-click toggling while pointing at doors and/or add an option to change the activating button. That's what I'm going to do now. And thanks for your silent comment-bound effort.
Also note that GPL is free software, and you can change, convey and redistribute changed copies it (and even sell!) in any required way, but just keep the code GPL.
And I begin to think that it's better to just make it always emit light if it's in the inventory and switched on.
After carefully looking through your improved code I have concluded that you're far more experienced than me in lua and minetest modding. Well, not that far, but I have a lot of to learn from you.
I've done up quite a few mods, from item creation/generation to item repair, to even armor and tools. (most of which I use as future references for doing a particular thing)
So the trick is if someone else has done it up, look at their code, see their mistakes (or potential mistakes) how they do particular things and learn from it. (Always learn something each time you look at code or even write code)
I've got Python, C++, a little Java (though only a very little), and thanks to Minetest, Lua. (with my latest craze in Go/Golang)
It helps to know other languages (computer languages that is) too.
Anyway, enough chatting, now on to breaking stuff and figuring out how to make it work again. :P
Ok so I've added multiple games "gamemodes" supporting, and we now support Minetest Game (MTG) and MineClone (MCL).
And not much has broke, well I've tried to make sure everything works with no crashing sounds in the background but meh.
And I've thought about is some, since we can have cool interchangable recipe ingredients perhaps we have the current Minetest Game recipe as a fallback if mesecons isn't installed, else we use cool stuff like lightnodes/lamps and switches/levers. (Kind of more realistic recipes)
I need to dig through technic's code and see about making it so if technic is installed we make our flashlight into a power tool that looses power on use and can eventually turn off due to no power. (or not, I kind of like the unlimited lighting)
Ye, I also wanted to make it sort of use the technic RE batteries for extra realism, that's why 'technic' is in optional dependencies (but never actually used). As rubenwardy's modding book says, I can look at other mods' code, that's why I'll borrow some from headlamp, since it utilizes the battery. I'm slightly busy at the moment with taking care of my sister, but I'll surely continue with making the mod.
By the way, I made my own repository, which I had opened for everyone to contribute and view, and it's also linked to this very contentdb page to release new versions with ease. Check it out (it's basically your repo, but with a few fixes, and also opened for contributions).
Ok no problem, yeah as soon as I saw the <source> option I hopped onto it. (I'm going to delete my old repo which I currently renamed to goodtorch-1 as currently it's now outdated, and as you've mentioned you've already merged into the main code so no need for a 2nd repo, only forks are needed)
Yeah I'm working right now on my own next new mod Chunk Keeper basically a keep alive the current mapblock idea. (But eventually I'll hop back on here and work on adding technic !?! Ah, I see you've got it started, well then I'll not touch it as we had a conflict which ate some of the code, but never fear, git tracks all changes, so we just need to dig back in time to see what happened and copy pasta the stuff I did like getting the player's eye pos via code/dynamically, actually that's all I can think I did, Since it appears you have the multi-game support so hmm.)
First off, I like that it reduces brightness the further away it goes (like real life!)
Second, I'd like to see it gain the ability to toggle the flashlight on/off by right click too!
Thanks a lot for your dandy review and for this code suggestion. I honestly wanted to add a right-click flashlight toggle, but did not know how (I was not aware of
on_secondary_use
yet) and was quite tired after figuring out why did it crash after shining the flashlight far into the distance that exceeds 100 nodes. The solution was to simply return"goodtorch:light_0"
(thank you, illumination, for the idea of light blocks) ifi
exceeds 100. I can probably optimize it further and just not set any nodes if not required, as 0-light air nodes are essentially equal to air, but waste the performance. I will test your code out tomorrow as I have to go to sleep now, and I shall return with more epic mod ideas at any time soon. P.S.: I didn't even look at indentation until you noted it in your further comment, so worry not, pal, about this miniscule issue.Not to promote my site but my site has some good resources I personally use.
https://beanzilla.net/minetest.html (Beanzilla was my old nickname, when I was heavy into just playing Minecraft, then found Minetest then chose a new nickname, TLDR old nickname) :)
Includes direct links to the Lua minetest api docs (just CTRL + F and type functions/whatnot to find)
And a good book by Rubenwardy. (Highly recommend mixing between the 2, like I do)
Limiting light range to 99 or less (100 is not included, change from
dist < 100
todist <= 100
to include 100) is simply:Your welcome. :)
If it's alright with you I'd like to release a copy that also works with water nodes (water source, water flowing, river water source, river water flowing).
I'll keep the license the same (mainly because of the license). :)
And attribute it to you, since you first made it possible, then I took a hack at it.
Yes underwater lighting is now possible, and no it actually can drown the player instead of possibly free air. (defines the water nodes into light water nodes)
Was a bit tricky to get the lighting right as I had a bug where it would shoot thru into caverns instead of just lighting the first non-replaceable node. (but I got it working, and if you don't want me to release it I'll not, and I'd probably not use it either, just to respect your choice)
Oh and also include on_place with the toggle code (lines 154 to 172)
Aye, I woke up a few hours ago, now it's time to test out all your dandy ideas. I think that there might be an issue with toggling the flashlight with only right mouse button, since one may wish to use this button while pointing on a useable node, and I don't know if the flashlight or the node will be activated first. It's better, I think, to temporarily disable right-click toggling while pointing at doors and/or add an option to change the activating button. That's what I'm going to do now. And thanks for your silent comment-bound effort. Also note that GPL is free software, and you can change, convey and redistribute changed copies it (and even sell!) in any required way, but just keep the code GPL. And I begin to think that it's better to just make it always emit light if it's in the inventory and switched on.
After carefully looking through your improved code I have concluded that you're far more experienced than me in lua and minetest modding. Well, not that far, but I have a lot of to learn from you.
I've done up quite a few mods, from item creation/generation to item repair, to even armor and tools. (most of which I use as future references for doing a particular thing)
So the trick is if someone else has done it up, look at their code, see their mistakes (or potential mistakes) how they do particular things and learn from it. (Always learn something each time you look at code or even write code)
I've got Python, C++, a little Java (though only a very little), and thanks to Minetest, Lua. (with my latest craze in Go/Golang)
It helps to know other languages (computer languages that is) too.
Ok so I've added multiple games "gamemodes" supporting, and we now support Minetest Game (MTG) and MineClone (MCL).
And not much has broke, well I've tried to make sure everything works with no crashing sounds in the background but meh.
And I've thought about is some, since we can have cool interchangable recipe ingredients perhaps we have the current Minetest Game recipe as a fallback if mesecons isn't installed, else we use cool stuff like lightnodes/lamps and switches/levers. (Kind of more realistic recipes)
Ye, I also wanted to make it sort of use the technic RE batteries for extra realism, that's why 'technic' is in optional dependencies (but never actually used). As rubenwardy's modding book says, I can look at other mods' code, that's why I'll borrow some from
headlamp
, since it utilizes the battery. I'm slightly busy at the moment with taking care of my sister, but I'll surely continue with making the mod. By the way, I made my own repository, which I had opened for everyone to contribute and view, and it's also linked to this very contentdb page to release new versions with ease. Check it out (it's basically your repo, but with a few fixes, and also opened for contributions).Ok no problem, yeah as soon as I saw the <source> option I hopped onto it. (I'm going to delete my old repo which I currently renamed to goodtorch-1 as currently it's now outdated, and as you've mentioned you've already merged into the main code so no need for a 2nd repo, only forks are needed)
Yeah I'm working right now on my own next new mod Chunk Keeper basically a keep alive the current mapblock idea. (But eventually I'll hop back on here and work on adding technic !?! Ah, I see you've got it started, well then I'll not touch it as we had a conflict which ate some of the code, but never fear, git tracks all changes, so we just need to dig back in time to see what happened and copy pasta the stuff I did like getting the player's eye pos via code/dynamically, actually that's all I can think I did, Since it appears you have the multi-game support so hmm.)