Help with modding: 2 problems

MarkTraceur
Member
 
Posts: 103
Joined: Sat Dec 03, 2011 05:41

Help with modding: 2 problems

by MarkTraceur » Mon Dec 05, 2011 00:22

Hi, all! I'm working on a mod for leaf decay, and it's being quite finicky.

First, I tried using add_luaentity like so:

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.env:add_luaentity(new_p, "leaf_decay:sapling")


That solicited the following 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
minetest.env:add_luaentity(new_p, "leaf_decay:sapling")


Any ideas as to why that happened, and how I should fix it? The entity referenced is declared earlier in the code.

The second problem is that the following code doesn't work as expected:

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
                DISTANCE = 3
                found_tree = 1
                for x=-DISTANCE,DISTANCE do
                   for y=-DISTANCE,DISTANCE do
                  for z=-DISTANCE,DISTANCE do
                     local test_p = {pos.x+x, pos.y+y, pos.z+z}
                     local test_node = minetest.env:get_node(test_p)
                     if test_node.name == "default:tree" or test_node.name == "default:jungletree" then
                    found_tree = 2
                    break
                     end
                  end
                  if found_tree == 2 then break end
                   end
                   if found_tree == 2 then break end
                end
                if found_tree == 1 then
                   minetest.env:remove_node(pos)
                end


The leaves are *always* removed, even if there is a tree near them. So, what am I doing wrong here?

Thanks a bunch, guys.
Last edited by MarkTraceur on Mon Dec 05, 2011 00:25, edited 1 time in total.
Mods: https://gitorious.org/marktraceur-minetest-mods
IRC: marktraceur on freenode
 

kahrl
Member
 
Posts: 236
Joined: Fri Sep 02, 2011 07:51

by kahrl » Mon Dec 05, 2011 02:40

1. add_luaentity has been renamed to add_entity some time ago. (The experimental TNT still uses add_luaentity, that's why punching TNT is currently full of fail.)

If I may ask, what is your entity for?

2. 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
local test_p = {pos.x+x, pos.y+y, pos.z+z}

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
local test_p = {x = pos.x+x, y = pos.y+y, z = pos.z+z}

Does it work after you change that?

3. It might be nice to store the list of tree types in a global variable instead of hardcoding them. Just in case somebody wanted to add a third tree type in a mod, so it could be added to the list (it would have to depend on your mod, of course).
 

randomproof
Member
 
Posts: 214
Joined: Thu Nov 17, 2011 06:31

by randomproof » Mon Dec 05, 2011 02:56

I think the problem might be in this line:

local test_p = {pos.x+x, pos.y+y, pos.z+z}

Should be

local test_p = {x = pos.x+x, y = pos.y+y, z = pos.z+z}

you have to put the field names in the assignment, in case x, y and z, for a position. I would suggest changing the for loop variables to something slightly different such as search_x search_y and search_z so that they are different than the field names.


On another note the add_luaentity function seems more for moving things and isn't attached to a node. (See nodeupdate_single and register_falling_node in default/init.lua) I actually spend a lot of time trying to get my growing mod working with that and it didn't work the way I wanted. Here is some code I wrote to do this. The only thing is that it seems to use a lot of cpu time. I added a check to see if the leaves are in the air because I made a hedge maze out of leaves and I don't want that to disappear.



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_abm({
    nodenames = { "default:leaves" },
    interval = 120,
    chance = 127,
    action = function(pos, node, active_object_count, active_object_count_wider)
        p_bottom = {x = pos.x, y = pos.y - 1, z = pos.z}
        n_bottom = minetest.env:get_node(p_bottom)

        if n_bottom.name == "air" then
            -- check to see if near 'tree' or 'jungletree', if not then remove
            found_tree = false

            for s_x = -3, 3 do
                for s_y = -3, 3 do
                    for s_z = -3, 3 do
                        search_pos = {x = pos.x+s_x, y = pos.y+s_y, z = pos.z+s_z}
                        search_node = minetest.env:get_node(search_pos)
                        if search_node.name == "default:tree" or search_node.name == "default:jungletree" then
                            found_tree = true
                            break
                        end
                    end
                    if found_tree == true then
                        break
                    end
                end
                if found_tree == true then
                    break
                end
            end

            if found_tree == false then
                minetest.env:remove_node(pos)
            end
        end
    end
})
 

MarkTraceur
Member
 
Posts: 103
Joined: Sat Dec 03, 2011 05:41

by MarkTraceur » Mon Dec 05, 2011 04:39

Thanks all, it works now (with some more help from kahrl)!
Mods: https://gitorious.org/marktraceur-minetest-mods
IRC: marktraceur on freenode
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 19 guests

cron