Page 1 of 1

Getting the name of the node below a node?

PostPosted: Wed Feb 20, 2013 01:08
by scifiboi
I am trying to get whether or not a node has been placed in a spot where there is either dirt or dirt with grass directly underneath it. Here is the code I currently have, but it doesn't work. It only throws a lua exception.

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 nodePlantedPos = pos
nodePlantedPos.y = nodePlanted.y - 1
local nodePlanted = minetest.env:get_node(nodePlantedPos)
if (nodePlanted.name ~= "default:dirt" or "default:dirt_with_grass") then return end


The code after this is to make my sapling grow into a tree. It works. This is the section that I am having trouble getting to work. I'm not sure what's going wrong. It seems as if I have tried everything but the right way to get this to work. All help is much appreciated!

PostPosted: Wed Feb 20, 2013 01:55
by lkjoel
scifiboi wrote:I am trying to get whether or not a node has been placed in a spot where there is either dirt or dirt with grass directly underneath it. Here is the code I currently have, but it doesn't work. It only throws a lua exception.

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 nodePlantedPos = pos
nodePlantedPos.y = nodePlanted.y - 1
local nodePlanted = minetest.env:get_node(nodePlantedPos)
if (nodePlanted.name ~= "default:dirt" or "default:dirt_with_grass") then return end


The code after this is to make my sapling grow into a tree. It works. This is the section that I am having trouble getting to work. I'm not sure what's going wrong. It seems as if I have tried everything but the right way to get this to work. All help is much appreciated!

Sorry, too lazy to try it out... could you paste the exception?

PostPosted: Wed Feb 20, 2013 02:00
by scifiboi
In the game window:

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
ServerError: LuaError: Error:
rubber.lua:69 Attempt to index


and in the console:

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
ServerError: LuaError: Error:
rubber.lua:69 Attempt to index global 'nodePlanted' (a nil value)

PostPosted: Wed Feb 20, 2013 02:07
by lkjoel
scifiboi wrote:-snip-

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
ServerError: LuaError: Error:
rubber.lua:69 Attempt to index global 'nodePlanted' (a nil value)

Okay, first of all, the game window never says anything useful xD (just look at the console, it says EVERYTHING!)

Second, the error is that it can't find the variable: "nodePlanted", because it doesn't exist when you accessed it on this line:
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
nodePlantedPos.y = nodePlanted.y - 1
--                     ^^^

So fix it by this:
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
nodePlantedPos.y = nodePlantedPos.y - 1

PostPosted: Wed Feb 20, 2013 02:19
by scifiboi
Wow, don't I feel silly right now. :P Okay so the exception is no longer being thrown, but the sapling won't grow even when it is on top of dirt or dirt with grass. :

PostPosted: Wed Feb 20, 2013 02:23
by lkjoel
scifiboi wrote:Wow, don't I feel silly right now. :P Okay so the exception is no longer being thrown, but the sapling won't grow even when it is on top of dirt or dirt with grass. :

Haha, don't, I do the same kinds of mistakes all of the time (I remember I spent a couple hours trying to fix my rather complex algorithms, while the issue was that I had misspelled "brakes" to "breaks" lol)

Could you paste your code?

PostPosted: Wed Feb 20, 2013 02:59
by BrandonReese
This line

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
if (nodePlanted.name ~= "default:dirt" or "default:dirt_with_grass") then return end


Should be

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
if (nodePlanted.name ~= "default:dirt" and nodePlanted.name ~= "default:dirt_with_grass") then return end


In the top code "default:dirt_with_grass" will always be true... there is no real evaluation going on, and since you were using an 'or' either side can evaluate to true and it will execute the code after 'then'... so that line of code will always 'return end'

The bottom code will only 'return end' if both sides of the 'and' are true.


Good catch on the typo, lkjoel. I didn't see that one, I make that mistake a lot too especially with languages where the variables are case sensitive... (ie nodePlanted is not the same as nodeplanted)

PostPosted: Wed Feb 20, 2013 03:26
by scifiboi
Wow! That worked! Thank you bunches! Expect more questions later because I am a total noob with lua. :P By the way, ikjoel, I really like your nether mod! It is awesome, but if I stay there for more than 5 minutes or so, minetest freezes up. I assume it's because of my super-slow computer, though.

PostPosted: Wed Feb 20, 2013 03:29
by lkjoel
scifiboi wrote:Wow! That worked! Thank you bunches! Expect more questions later because I am a total noob with lua. :P By the way, ikjoel, I really like your nether mod! It is awesome, but if I stay there for more than 5 minutes or so, minetest freezes up. I assume it's because of my super-slow computer, though.

Oh sure, that's what this forum is for!

Thanks, hmm... any error logs in the output?

PostPosted: Wed Feb 20, 2013 03:34
by scifiboi
Nope, none. Just the game freezes up.

PostPosted: Wed Feb 20, 2013 03:35
by lkjoel
scifiboi wrote:Nope, none. Just the game freezes up.

Weird... how long did you wait for it to un-freeze?

PostPosted: Wed Feb 20, 2013 03:37
by scifiboi
About 30 minutes. I left the computer to see if anything happened. As far as I could see, nothing on the screen changed. I'm pretty sure it was because of the mod using up so much of my memory.

PostPosted: Wed Feb 20, 2013 03:39
by lkjoel
scifiboi wrote:About 30 minutes. I left the computer to see if anything happened. As far as I could see, nothing on the screen changed. I'm pretty sure it was because of the mod using up so much of my memory.

Yeah, that'd make sense. I'm working on a new version which should be at least 100-200x faster, and use... hmm... around 1000-10,000x less memory. How? I'll use C to do all the heavy work :P

PostPosted: Wed Feb 20, 2013 03:40
by scifiboi
That will be great! I will definitely use it if you can get it to work fast and it will be a great addition to my industry mod with the netherrack and such! :)

PostPosted: Wed Feb 20, 2013 03:45
by lkjoel
scifiboi wrote:That will be great! I will definitely use it if you can get it to work fast and it will be a great addition to my industry mod with the netherrack and such! :)

Awesome! Netherrack being used in industry? O_o. I don't think I want to know what it does :P jk