How to place saplings on chunk generation?

User avatar
scifiboi
Member
 
Posts: 96
Joined: Wed Jul 18, 2012 21:28

How to place saplings on chunk generation?

by scifiboi » Thu Feb 21, 2013 00:52

I am trying to get saplings to place randomly on dirt with grass blocks when the chunk is generated in the world. No exception is being thrown, but no saplings are being placed. Here is my code:

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.register_on_generated(
    function(minp, maxp)
        local dirtPositions = minetest.env:find_nodes_in_area(minp, maxp, {"default:dirt_with_grass"})
        local i = 0
        while (dirtPositions[i] ~= nil) do
            local willDirtHaveSapling = math.random(1,2)
            if (willDirtHaveSapling == 1) then
                local saplingPos = dirtPositions[i]
                saplingPos.y = saplingPos.y + 1
                minetest.env:add_node(saplingPos, "industry:rubbersapling")
            end
            i = i + 1
        end
    end
)


Please don't scold me if it's really simple. I'm a total noob at Lua. The reason why I'm using a while loop instead of a for loop is because it would freeze up minetest and make it close without any error. All help is much appreciated!
This is a signature virus. Add me to your signature so that I can multiply.
My mod: Thaumtest
 

lkjoel
Member
 
Posts: 778
Joined: Wed Feb 29, 2012 19:27

by lkjoel » Thu Feb 21, 2013 01:07

scifiboi wrote:
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
-snip-
        local i = 0
        while (dirtPositions[i] ~= nil) do
-snip


Lua arrays are 1-based, not 0-based (took me some time to understand that actually), so dirtPositions[0] will be nil. Change local i = 0 to local i = 1, and all should be fine :)
My mods: The Nether | Doctor Who (WIP)

I have quit minetest ... again. I am heavily unimpressed by both the game and the community.
 

User avatar
scifiboi
Member
 
Posts: 96
Joined: Wed Jul 18, 2012 21:28

by scifiboi » Thu Feb 21, 2013 01:12

I changed it, and now the game is doing the same thing it did when I used the for loop - it is crashing and the console says 'Aborted (Core Dump)' with a bunch of other stuff above it. Not sure what this means, but obviously it's not good.

EDIT: Okay, so I commented out the add_node function and it didn't crash that time. Not sure why this was making it crash.
Last edited by scifiboi on Thu Feb 21, 2013 01:16, edited 1 time in total.
This is a signature virus. Add me to your signature so that I can multiply.
My mod: Thaumtest
 

lkjoel
Member
 
Posts: 778
Joined: Wed Feb 29, 2012 19:27

by lkjoel » Thu Feb 21, 2013 01:27

scifiboi wrote:I changed it, and now the game is doing the same thing it did when I used the for loop - it is crashing and the console says 'Aborted (Core Dump)' with a bunch of other stuff above it. Not sure what this means, but obviously it's not good.

EDIT: Okay, so I commented out the add_node function and it didn't crash that time. Not sure why this was making it crash.

Could you post the full error?
My mods: The Nether | Doctor Who (WIP)

I have quit minetest ... again. I am heavily unimpressed by both the game and the community.
 

User avatar
scifiboi
Member
 
Posts: 96
Joined: Wed Jul 18, 2012 21:28

by scifiboi » Thu Feb 21, 2013 01:32

I will edit this post with it when I get back. Had to leave my computer for a bit.

EDIT: Looks like a bunch of gibberish to me. Here is the 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
19:01:01: ERROR[EmergeThread]: ERROR: An unhandled exception occurred: LuaError: error: ...me/blake/.minetest/mods/minetest/industry/rubber.lua:145: bad argument #-2 to 'add_node' (string expected, got nil)
19:01:01: ERROR[EmergeThread]: stack traceback:

In thread ab763b40:
/build/buildd/minetestc55-201302152341/src/server.cpp:411: virtual void* EmergeThread::Thread(): Assertion '0' failed.
Debug stacks:
DEBUG STACK FOR THREAD a8e60b40:
#0  virtual void* MeshUpdateThread::Thread()
DEBUG STACK FOR THREAD ab763b40:
#0  virtual void* EmergeThread::Thread()
(Leftover data: #1  virtual MapBlock* ServerMap::emergeBlock(v3s16, bool): p=(28,3,-27), create_blank=0)
(Leftover data: #2  ServerMapSector* ServerMap::createSector(v2s16): p2d=(28,-27))
(Leftover data: #3  void ServerMap::loadBlock(std::string*, v3s16, MapSector*, bool))
DEBUG STACK FOR THREAD ae8f9b40:
#0  virtual void* ServerThread::Thread()
#1  void Server::AsyncRunStep()
#2  virtual void ServerEnvironment::step(float)
(Leftover data: #3  void BlockEmergeQueue::addBlock(irr::u16, v3s16, irr::u8))
(Leftover data: #4  void BlockEmergeQueue::addBlock(irr::u16, v3s16, irr::u8))
DEBUG STACK FOR THREAD b70a2940:
#0  int main(int, char**)
(Leftover data: #1  void ClientMap::renderMap(irr::video::IVideoDriver*, irr::s32))
(Leftover data: #2  virtual void ClientEnvironment::step(float))
(Leftover data: #3  void Client::Receive())
(Leftover data: #4  void Client::ProcessData(irr::u8*, irr::u32, irr::u16))
(Leftover data: #5  void MeshUpdateQueue::addBlock(v3s16, MeshMakeData*, bool, bool))
Aborted (core dumped)



EDIT #2: I may have solved it. It seems to have been a very, very shameful issue of me putting 'minetest.env:add_node(pos, "industry:rubbersapling")' instead of 'minetest.env:add_node(pos, {name="industry:rubbersapling")'.
Last edited by scifiboi on Thu Feb 21, 2013 02:24, edited 1 time in total.
This is a signature virus. Add me to your signature so that I can multiply.
My mod: Thaumtest
 

User avatar
scifiboi
Member
 
Posts: 96
Joined: Wed Jul 18, 2012 21:28

by scifiboi » Thu Feb 21, 2013 02:34

Yes! I have gotten the code to work. I forgot that add_node required a table instead of a string as the second parameter. At the beginning there were trees on every single grass block, but that was because I had 'i = i + 1' inside the if statement. Thanks for your help, ikjoel!
This is a signature virus. Add me to your signature so that I can multiply.
My mod: Thaumtest
 

lkjoel
Member
 
Posts: 778
Joined: Wed Feb 29, 2012 19:27

by lkjoel » Thu Feb 21, 2013 02:37

scifiboi wrote:Yes! I have gotten the code to work. I forgot that add_node required a table instead of a string as the second parameter. At the beginning there were trees on every single grass block, but that was because I had 'i = i + 1' inside the if statement. Thanks for your help, ikjoel!

Awesome, good for you! And you're welcome :)
My mods: The Nether | Doctor Who (WIP)

I have quit minetest ... again. I am heavily unimpressed by both the game and the community.
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 16 guests