Adding function within a node

Post Reply
User avatar
Ta1ls
New member
Posts: 7
Joined: Sat Sep 22, 2018 19:35
In-game: Tails

Adding function within a node

by Ta1ls » Post

what if i want to spawn an object?

Code: Select all

local function spawning_item(pos, pointed_thing)
	minetest.add_particle({
			pos = pos,
			velocity = {x = 0, y = 0, z = 0},
			acceleration = {x = 0, y = 0, z = 0},
			expirationtime = 1.0,
			collisiondetection = false,
			texture = "spawned.png",
			size = math.random(100, 150),
			glow = 15,
		})

		entity_physics(pos, 2)

		minetest.sound_play("beep", {
			pos = pos,
			gain = 1.0,
			max_hear_distance = 25
		})
		
minetest.register_tool("noob:spawner", {
	description = "item summoner",
	image = "sppawner.png",
	inventory_image = "spawner_block.png",
	
	tool_capabilities = {
		full_punch_interval = 0.8,
		max_drop_level=1,
	}
})

User avatar
TenPlus1
Member
Posts: 3728
Joined: Mon Jul 29, 2013 13:38
In-game: TenPlus1
Contact:

Re: Adding function within a node

by TenPlus1 » Post

minetest.add_item(pos, item_name) ..or.. minetest.add_entity(pos, entity_name)

e.g.

minetest.add_item(pos, "default:wood") ..and.. minetest.add_entity(pos, "mobs_animal:cow")

User avatar
Ta1ls
New member
Posts: 7
Joined: Sat Sep 22, 2018 19:35
In-game: Tails

Re: Adding function within a node

by Ta1ls » Post

and where would i put that in the code? :) sorry i just started minetest and i am learning how to code in Lua.

User avatar
AiTechEye
Member
Posts: 1000
Joined: Fri May 29, 2015 21:14
GitHub: AiTechEye
Location: Sweden

Re: Adding function within a node

by AiTechEye » Post

try this


then when you understands how this works, this page can be useful: https://dev.minetest.net/Category:Methods

and check how other people are doing their mods

Code: Select all

minetest.register_tool("noob:spawner", {
	description = "item summoner",
	image = "sppawner.png",
	inventory_image = "spawner_block.png",
	tool_capabilities = {
		full_punch_interval = 0.8,
		max_drop_level=1,
	}
 -- when the item is used
	on_use=function(itemstack, user, pointed_thing)
		minetest.chat_send_all("item used")

		print(dump(pointed_thing))

		if pointed_thing.type=="node" then
			minetest.chat_send_all("item used")
			local pos=pointed_thing.above
			minetest.add_entity(pos, "noob:object")
		end
	end,

 -- when the item is placed
	on_place=function(itemstack, user, pointed_thing)
		print(dump(pointed_thing))
		minetest.chat_send_all("item placed")
		minetest.add_item(pointed_thing.above, "default:wood")
	end,
})


minetest.register_entity("noob:object",{
	hp_max = 1,
	visual = "sprite",
	textures ={"default_stone.png"},
	on_activate=function(self, staticdata)
		minetest.chat_send_all("object spawned")
		self.more_text=" ...and more text"
	end.
	on_step=function(self, dtime)
		self.time=self.time+dtime
		if self.time<5 then
			minetest.chat_send_all(self.some_text_to_say .. self.more_text)
			self.object:remove()
			return
		end
	end,
	time=0,
	some_text_to_say="object removed",
})


minetest.register_node("noob:block", {
	description = "a block",
	stack_max=500,
	tiles = {"default_stone.png","default_wood.png","default_dirt.png"},
	groups = { choppy = 2, oddly_breakable_by_hand = 1, flammable = 1},
	on_construct=function(pos)
		minetest.get_node_timer(pos):start(1)
	end,
	on_timer = function (pos, elapsed)
		minetest.chat_send_all("timer")
		return true
	end
})
The console is very usefull for moders, but it has been disabled by default.

enable it by go to the minetest main manu --> settings tab --> advanced settings --> graphics --> advanced --> enable console window --> restart the game

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 24 guests