Difference between revisions of "minetest.spawn tree"

From Minetest Developer Wiki
Jump to navigation Jump to search
m (Make sure the name “Minetest Game” is used consistently throughout this page)
(→‎Example: Removed the old example. It wasn't working if you execute the code at load-time, which most people will do.)
Line 12: Line 12:
  
 
== Example ==
 
== Example ==
This code will spawn an apple tree with its origin at position (0,0,0), assuming that [http://wiki.minetest.net/Subgames/Minetest%20Game Minetest Game] is used:
+
This code adds a chat command that spawns an apple tree with its origin at the current position of the player, assuming that [http://wiki.minetest.net/Subgames/Minetest%20Game Minetest Game] is used:
 
<source>
 
<source>
 
local treedef = {
 
local treedef = {
Line 29: Line 29:
 
}
 
}
  
local pos = { x=0, y=0, z=0 }
+
core.register_chatcommand("spawn_tree", {
 
+
params = "", description = "Spawns tree at player position",
minetest.spawn_tree(pos, treedef)
+
func = function(name, param)
 +
local pos = minetest.get_player_by_name(name):getpos()
 +
minetest.chat_send_player(name, "Spawning tree at " .. minetest.pos_to_string(pos)
 +
.. ", please wait")
 +
minetest.spawn_tree(pos, treedef)
 +
return true, "successfully spawned"
 +
end,
 +
})
 
</source>
 
</source>
  
Line 37: Line 44:
  
 
[[File:Apple Tree.png|420px]]
 
[[File:Apple Tree.png|420px]]
 
  
 
== Notes ==
 
== Notes ==

Revision as of 00:42, 20 November 2015

Syntax

minetest.spawn_tree(pos, tree)

Description

Spawns an L-system tree at position pos, using the tree definition tree.

See Introduction to L-system trees for a description of L-system trees.

Example

This code adds a chat command that spawns an apple tree with its origin at the current position of the player, assuming that Minetest Game is used:

local treedef = {
	axiom="FFFFFAFFBF",
	rules_a="[&&&FFFFF&&FFFF][&&&++++FFFFF&&FFFF][&&&----FFFFF&&FFFF]",
	rules_b="[&&&++FFFFF&&FFFF][&&&--FFFFF&&FFFF][&&&------FFFFF&&FFFF]",
	trunk="default:tree",
	leaves="default:leaves",
	angle=30,
	iterations=2,
	random_level=0,
	trunk_type="single",
	thin_branches=true,
	fruit_chance=10,
	fruit="default:apple"
}

core.register_chatcommand("spawn_tree", {
	params = "", description = "Spawns tree at player position",
	func = function(name, param)
		local pos = minetest.get_player_by_name(name):getpos()
		minetest.chat_send_player(name, "Spawning tree at " .. minetest.pos_to_string(pos)
			.. ", please wait")
		minetest.spawn_tree(pos, treedef)
		return true, "successfully spawned"
	end,
})

The resulting tree may look like this:

Apple Tree.png

Notes

VanessaE's plants_lib mod (github) provides functions to register L-system trees to be spawned during map generation, and finds places for them to spawn based on the map seed and biome.