Uberi / Minetest-WorldEdit

The ultimate in-game world editing tool for Minetest! Tons of functionality to help with building, fixing, and more.
https://forum.minetest.net/viewtopic.php?f=11&t=572
GNU Affero General Public License v3.0
162 stars 81 forks source link

How to use API in mods for dummies #173

Closed makayabou closed 5 years ago

makayabou commented 5 years ago

Hello, I don't know if here is the right place to post my issue or the forum.. I want to use worledit API in my mod, but I have an error about manip that has a nil value.

Code in init.lua of mymod is: local cube = worldedit.cube(def.coords.point1, 500, 500, 500, "air")

with def.coords.point1 being :

print(dump(def.coords.point1)) --> { y = 0, x = 1000, z = 1000 }

Detail of error are

`2019-02-06 23:51:21: ERROR[Main]: ...ames/minetest-garage/mods/WorldEdit/worldedit/common.lua:74: attempt to index local 'manip' (a nil value)

2019-02-06 23:51:21: ERROR[Main]: stack traceback:

2019-02-06 23:51:21: ERROR[Main]: ...ames/minetest-garage/mods/WorldEdit/worldedit/common.lua:74: in function 'init'

2019-02-06 23:51:21: ERROR[Main]: .../minetest-garage/mods/WorldEdit/worldedit/primitives.lua:18: in function 'cube'

2019-02-06 23:51:21: ERROR[Main]: ...t/games/minetest-garage/mods/gnmodpack/gnlevels/init.lua:50: in function 'golevel'

2019-02-06 23:51:21: ERROR[Main]: ...t/games/minetest-garage/mods/gnmodpack/gnlevels/init.lua:57: in main chunk
`

Something else I noticed: in my depends.txt, if I put WorldEdit I got an error saying mod WorldEdit could not be loaded, if I put worldedit, no error ..

I'm sorry if the problem is silly and I didn't see the obvious..

Thanks for help...

sfan5 commented 5 years ago

The error happens because minetest.get_voxel_manip() returned nil, which is never supposed to happen. Which Minetest version are you using?

makayabou commented 5 years ago

I saw that in common.lua... If i use WorldEdit pack mode in a new default game, chat commands like //set work, but if I add a mod with the very single line

local cube = worldedit.cube({x=0,y=0,z=0}, 500, 500, 500, "air")

in init.lua and worldedit in depends.txt, it raises the error again...

I'm using minetest 0.4.17.1 :

➜ ~ minetest --version Minetest 0.4.17.1 (Linux) Using Irrlicht 1.8.4 Build info: VER=0.4.17.1 BUILD_TYPE=None RUN_IN_PLACE=0 USE_GETTEXT=1 USE_SOUND=1 USE_CURL=1 USE_FREETYPE=1 USE_LUAJIT=1 STATIC_SHAREDIR="/usr/share/games/minetest"

Thanks for your help

makayabou commented 5 years ago

Hello, I found a way to solve the problem using minetest.after:

minetest.after(0, function()  
    local cube = worldedit.cube({x=0,y=0,z=0}, 500, 500, 500, "air")  
end)

I guess the problem was that the map was not completely generated? What is the best way to use API in mods ? using minetest.after or is there a better way? Thanks

sfan5 commented 5 years ago

You can't edit the map at the point where mods are just being loaded. You usually do things in response to a player action, e.g. a chatcommand, but if you want to unconditionally do something at startup, minetest.after is the right choice.