[SOLVED] Arrays and functions formatting

Post Reply
Icalasari
Member
Posts: 166
Joined: Tue Sep 23, 2014 05:29
IRC: Icalasari
In-game: Icalasari

[SOLVED] Arrays and functions formatting

by Icalasari » Post

Converting my bush stuff now. Just a quick question on the formatting (sadly, I don't seem to know what to google for this one. Googling arrays variables functions lua did show me some neat tricks that will be useful for other mod parts, however)

Just going to post the relevant parts as it's specifically a formatting issue

Code: Select all

mindeca.mbush = {
	rasp = {
		bushdesc = "Raspberry Bush Leaves\n\nLeaves that once bore raspberries.",
		seeddesc = "Raspberry Bush Seedling\n\nA bush to grow tart raspberries.",
		fruit = "raspberry",
		bushfdesc = "Raspberry Bush Leaves (Berry Laden)\n\n[Creative]",
		grow = spirit,
		bgrow = rasp,
		light = nil,
		effect = nil},
}

Code: Select all

	local function grow_new_..name.._bush(pos)
		if not mindeca.can_grow_def.grow(pos) then
			-- try a bit later again
			minetest.get_node_timer(pos):start(math.random(240, 600))
			return
		end
		minetest.remove_node(pos)
		minetest.place_schematic({x = pos.x-1, y = pos.y, z = pos.z-1}, minetest.get_modpath("mindeca").."/schematics/mindeca_bush_berry_"..name..".mts", "random", nil, false)
	end
It's giving an error for the line local function grow_new_..name.._bush(pos) saying a ( is expected near .. and I tried following that but it's spitting up new errors that are not helping. So from what I can gather, it's a formatting issue. How is this supposed to be formatted/what should I be googling for when trying to find an explanation on how to format this?
Last edited by Icalasari on Sun Aug 21, 2022 17:31, edited 1 time in total.

User avatar
sorcerykid
Member
Posts: 1841
Joined: Fri Aug 26, 2016 15:36
GitHub: sorcerykid
In-game: Nemo
Location: Illinois, USA

Re: Arrays and functions formatting

by sorcerykid » Post

You should pass "name" as a parameter to the function. This should do what you need:

Code: Select all

local function grow_new_bush(pos, name)

Icalasari
Member
Posts: 166
Joined: Tue Sep 23, 2014 05:29
IRC: Icalasari
In-game: Icalasari

Re: Arrays and functions formatting

by Icalasari » Post

Hmm, stopped the error but it's not growing the seedling (btw, a lot has clicked after you said (pos, name). It never sunk in exactly what was going on there, so it explains a lot now)

Code: Select all

	local function grow_new_bush(pos, name)
		if not mindeca.can_grow(pos, def.grow) then
			-- try a bit later again
			minetest.get_node_timer(pos):start(math.random(1, 1))
			return
		end
		minetest.remove_node(pos)
		minetest.place_schematic({x = pos.x-1, y = pos.y, z = pos.z-1}, minetest.get_modpath("mindeca").."/schematics/mindeca_bush_berry_"..name..".mts", "random", nil, false)
	end
end
A separate file:

Code: Select all

function mindeca.can_grow_spirit(pos)
	local node_under2 = minetest.get_node_or_nil({x = pos.x, y = pos.y - 1, z = pos.z})
	if not node_under2 then
		return false
	end
	if minetest.get_item_group(node_under2.name, "soil") == 0 then
		return false
	end
	local light_level2 = minetest.get_node_light(pos)
	if not light_level2 or light_level < 4 then
		return false
	end
	return true
end
I can tell it's if not mindeca.can_grow(pos, def.grow) then that is the issue here, but trying to figure out what... Tried a few ideas

User avatar
sirrobzeroone
Member
Posts: 593
Joined: Mon Jul 16, 2018 07:56
GitHub: sirrobzeroone
Contact:

Re: Arrays and functions formatting

by sirrobzeroone » Post

Edit: doh writing this and I got beaten to posting - :)

I'll try and help again

table structure looks okay I use the main lua ref + tutorial point and a few other sites eg tables:
https://www.lua.org/pil/2.5.html

ran table through lua demo - i just adjusted spirit and rasp to strings I assume you have those being set somewhere? nils essentially don't exist so no output:

Code: Select all

local mindeca = {}

mindeca.mbush = {
	rasp = {
		bushdesc = "Raspberry Bush Leaves\n\nLeaves that once bore raspberries.",
		seeddesc = "Raspberry Bush Seedling\n\nA bush to grow tart raspberries.",
		fruit = "raspberry",
		bushfdesc = "Raspberry Bush Leaves (Berry Laden)\n\n[Creative]",
		grow = "spirit",
		bgrow = "rasp",
		light = nil,
		effect = nil},
}
output

Code: Select all

for k,v in pairs(mindeca.mbush.rasp) do
print(k..":"..v)
end

fruit:raspberry
bushfdesc:Raspberry Bush Leaves (Berry Laden)

[Creative]
seeddesc:Raspberry Bush Seedling

A bush to grow tart raspberries.
bushdesc:Raspberry Bush Leaves

Leaves that once bore raspberries.
bgrow:rasp
grow:spirit


Function
The function name:
"function grow_new_..name.._bush(pos)"

I don't think that'll work I haven't had much luck myself setting dynamic variable names essentially what that is. There is some info here:
https://stackoverflow.com/questions/506 ... ame-in-lua

So your error might be because essentially the function dosen't exist. To test my theory as it's just that a theory/guess try temporarly adjusting your function name to "grow_new_rasp_bush(pos)" and see if that fixes anything.

Icalasari
Member
Posts: 166
Joined: Tue Sep 23, 2014 05:29
IRC: Icalasari
In-game: Icalasari

Re: Arrays and functions formatting

by Icalasari » Post

sirrobzeroone wrote:
Sat Aug 13, 2022 00:04
So your error might be because essentially the function dosen't exist. To test my theory as it's just that a theory/guess try temporarly adjusting your function name to "grow_new_rasp_bush(pos)" and see if that fixes anything.
That's what it was. I'm trying to convert all the code into one where I just need to plug in a new entry on the table to make a new bush, for the sake of cleaning it all up and making it run better

User avatar
Skamiz Kazzarch
Member
Posts: 613
Joined: Fri Mar 09, 2018 20:34
GitHub: Skamiz
In-game: Skamiz
Location: la lojbaugag.

Re: Arrays and functions formatting

by Skamiz Kazzarch » Post

local function grow_new_..name.._bush(pos)
Yeah, this doesn't make sense, since .. operates on data and you are trying to have it operate on code. That's like you would try this:

Code: Select all

local foo = "bar"
local connected = f .. oo
And expected that connected == "bar". In this example, what actually happens is that f .. oo tries to concatenate the value stored in f with the value stored in oo which are both most likely nil, resulting in an error.
In your case, since the interpreter knows it is looking for a function name it doesn't even try the concatenation and instead thinks that the function is named grow_new_ and since after a function name brackets must follow it's wondering why it's finding .. instead and throws the error you saw.

Sirrobzeroone
You can have variable function names as long as they aren't local. Instead of doing this:

Code: Select all

function foo()
    -- do stuff
end
You can do this:

Code: Select all

_G["foo"] = function()
    -- do stuff
end
Since in the second case the functions name is defined as a string instead of raw code, you can use all the usual string operations. And of course this works as long as the function is in any table, not just global.

Icalasari
Member
Posts: 166
Joined: Tue Sep 23, 2014 05:29
IRC: Icalasari
In-game: Icalasari

Re: Arrays and functions formatting

by Icalasari » Post

So if I'm understanding right:

local _G["grow_new_"..name.."_bush"] = function(pos) instead of local function grow_new_..name.._bush(pos)?

loosewheel
Member
Posts: 155
Joined: Mon Dec 28, 2020 01:19
GitHub: loosewheel
In-game: loosewheel

Re: Arrays and functions formatting

by loosewheel » Post

What Skamiz Kazzarch posted is correct as an example. Static names (such as function and variable names) are assessed at compile time. The string concat operator .. is a runtime operation. Because table key names are actually just strings (tab.key is the same as tab["key"]), these are processed at runtime.

In practice though, its typically not a good idea to create it as a key of _G. This will make a global function that is visible everywhere, even other mods. Placing local before it as in your post would create a local variable _G, and then crash because its used as a table but is nil.

When lua calls a function, or runs a module, it creates a stack (table) for it. Things such as parameters are in this table. The metatable of the local table is set to one of the tables in the calling hierarchy, so everything in _G is accessible. When you use the local keyword lua creates a key in this local table. If the local keyword is omitted then lua creates the key in the _G table. When that function or module returns this local table is destroyed. All the memory it referenced is let go, except for parts that where reference in still existing code.

I don't think there is a way to access this local table by name. You would likely have to create your own local table and use this method to create keys under it. More like:

Code: Select all

local funcs = { }
funcs["grow_new_"..name.."_bush"] = function (pos)
   -- whatever
end

Icalasari
Member
Posts: 166
Joined: Tue Sep 23, 2014 05:29
IRC: Icalasari
In-game: Icalasari

Re: Arrays and functions formatting

by Icalasari » Post

It crashed it so now I'm wondering if I am just too mentally burnt on this (probably). Guess for now, I'll make unique separate functions for each bush and come back to this once I have more experience and my mind is fresh and ready to go at this right

Will probably make a separate debug file I can turn on and off to figure this out, keep asking advice every now and then, and hopefully mark this thread as solved eventually. Going to post the entire code

Code: Select all

mindeca.mbush = {
	--bush = {
		--"Bush Desc",
		--"Seedling Desc",
		--"Fruit",
		--"Berry Laden Bush Desc",
		--"Grow Cycle",
		--"Berry Grow Cycle",
		--"Light Level",
		--"Water Need"},
	--Spring
	--Summer
	rasp = {
		bushdesc = "Raspberry Bush Leaves\n\nLeaves that once bore raspberries.",
		seeddesc = "Raspberry Bush Seedling\n\nA bush to grow tart raspberries.",
		fruit = "raspberry",
		bushfdesc = "Raspberry Bush Leaves (Berry Laden)\n\n[Creative]",
		grow = spirit,
		bgrow = rasp,
		light = nil,
		effect = nil},
	--Autumn
	--Winter
}



for name,def in pairs(mindeca.mbush) do
	minetest.register_node("mindeca:bush_"..name.."_berries", {
		description = def.bushfdesc.."\n\nMindeca",
		drawtype = "allfaces_optional",
		tiles = {"mindeca_bush_"..name.."_leaves.png^mindeca_bush_"..name.."_berries.png"},
		special_tiles = {"mindeca_bush_"..name.."_leaves_s.png^mindeca_bush_"..name.."_berries.png"},
		paramtype = "light",
		light_source = def.light,
		groups = {snappy = 3, flammable = 2, leaves = 1, dig_immediate = 3, spirit = 1},
		drop = "mindeca:item_fruit_"..def.fruit,
		sounds = default.node_sound_leaves_defaults(),
		node_dig_prediction = "mindeca:bush_"..name.."_leaves",
		after_dig_node = function(pos, oldnode, oldmetadata, digger)
			minetest.set_node(pos, {name = "mindeca:bush_"..name.."_leaves"})
			minetest.get_node_timer(pos):start(math.random(300, 1500))
		end,
	})

	minetest.register_node("mindeca:bush_"..name.."_leaves", {
		description = def.bushdesc.."\n\nMindeca",
		drawtype = "allfaces_optional",
		tiles = {"mindeca_bush_"..name.."_leaves.png"},
		special_tiles = {"mindeca_bush_"..name.."_leaves_s.png"},
		paramtype = "light",
		light_source = def.light,
		groups = {snappy = 3, flammable = 2, leaves = 1, spirit = 1},
		drop = {
			max_items = 1,
			items = {
				{items = {"mindeca:bush_"..name.."_seed"}, rarity = 5},
				{items = {"mindeca:bush_"..name.."_leaves"}}
			}
		},
		sounds = default.node_sound_leaves_defaults(),

		on_timer = function(pos, elapsed)
			if minetest.get_node_light(pos) < 3 then
				minetest.get_node_timer(pos):start(200)
			else
				minetest.set_node(pos, {name = "mindeca:bush_"..name.."_berries"})
			end
		end,
	})

	minetest.register_node("mindeca:bush_"..name.."_seed", {
		description = def.seeddesc.."\n\nMindeca",
		drawtype = "plantlike",
		tiles = {"mindeca_item_plant_bush_"..name..".png"},
		inventory_image = "mindeca_item_plant_bush_"..name..".png",
		wield_image = "mindeca_item_plant_bush_"..name..".png",
		paramtype = "light",
		sunlight_propagates = true,
		walkable = false,
		light_source = def.light,
		on_timer = grow_new_bush,
		selection_box = {
			type = "fixed",
			fixed = {-4 / 16, -0.5, -4 / 16, 4 / 16, 7 / 16, 4 / 16}
		},
		groups = {snappy = 2, dig_immediate = 3, flammable = 2, attached_node = 1, sapling = 1},
		sounds = default.node_sound_leaves_defaults(),

		on_construct = function(pos)
			minetest.get_node_timer(pos):start(math.random(1, 1))
		end,

		on_place = function(itemstack, placer, pointed_thing)
			itemstack = default.sapling_on_place(itemstack, placer, pointed_thing,
				"mindeca:bush_"..name.."_seed",
				-- minp, maxp to be checked, relative to sapling pos
				-- minp_relative.y = 1 because sapling pos has been checked
				{x = -2, y = 1, z = -2},
				{x = 2, y = 2, z = 2},
				-- maximum interval of interior volume check
				4)

			return itemstack
		end,
	})

	local function grow_new_bush(pos, name)
		if not mindeca.can_grow(pos, def.grow) then
			-- try a bit later again
			minetest.get_node_timer(pos):start(math.random(1, 1))
			return
		end
		minetest.remove_node(pos)
		minetest.place_schematic({x = pos.x-1, y = pos.y, z = pos.z-1}, minetest.get_modpath("mindeca").."/schematics/mindeca_bush_berry_"..name..".mts", "random", nil, false)
	end
end

Icalasari
Member
Posts: 166
Joined: Tue Sep 23, 2014 05:29
IRC: Icalasari
In-game: Icalasari

Re: Arrays and functions formatting

by Icalasari » Post

After some sleep, I think I'm understanding better what loosewheel is saying?

Something like

Code: Select all

	local grow_minebush = { }
	grow_minebush["grow_new_"..name.."_bush"] = function (pos)
	function (pos)
		if not mindeca.can_grow(pos, def.grow) then
			-- try a bit later again
			minetest.get_node_timer(pos):start(math.random(1, 1))
			return
		end
		minetest.remove_node(pos)
		minetest.place_schematic({x = pos.x-1, y = pos.y, z = pos.z-1}, minetest.get_modpath("mindeca").."/schematics/mindeca_bush_berry_"..name..".mts", "random", nil, false)
	end
end
Or still misunderstanding?

loosewheel
Member
Posts: 155
Joined: Mon Dec 28, 2020 01:19
GitHub: loosewheel
In-game: loosewheel

Re: Arrays and functions formatting

by loosewheel » Post

I don't know why there is a function definition inside a function definition, but as far as custom constructed key names under tables that's the basic idea.

Icalasari
Member
Posts: 166
Joined: Tue Sep 23, 2014 05:29
IRC: Icalasari
In-game: Icalasari

Re: Arrays and functions formatting

by Icalasari » Post

And it broke. I don't know why this is giving me so much trouble

Code

Code: Select all

mindeca.mbush = {
	--bush = {
		--"Bush Desc",
		--"Seedling Desc",
		--"Fruit",
		--"Berry Laden Bush Desc",
		--"Grow Cycle",
		--"Berry Grow Cycle",
		--"Light Level",
		--"Water Need"},
	--Spring
	--Summer
	rasp = {
		bushdesc = "Raspberry Bush Leaves\n\nLeaves that once bore raspberries.",
		seeddesc = "Raspberry Bush Seedling\n\nA bush to grow tart raspberries.",
		fruit = "raspberry",
		bushfdesc = "Raspberry Bush Leaves (Berry Laden)\n\n[Creative]",
		grow = spirit,
		bgrow = rasp,
		light = nil,
		water = nil},
	--Autumn
	--Winter
}



for name,def in pairs(mindeca.mbush) do
	minetest.register_node("mindeca:bush_"..name.."_berries", {
		description = def.bushfdesc.."\n\nMindeca",
		drawtype = "allfaces_optional",
		tiles = {"mindeca_bush_"..name.."_leaves.png^mindeca_bush_"..name.."_berries.png"},
		special_tiles = {"mindeca_bush_"..name.."_leaves_s.png^mindeca_bush_"..name.."_berries.png"},
		paramtype = "light",
		light_source = def.light,
		groups = {snappy = 3, flammable = 2, leaves = 1, dig_immediate = 3, spirit = 1},
		drop = "mindeca:item_fruit_"..def.fruit,
		sounds = default.node_sound_leaves_defaults(),
		node_dig_prediction = "mindeca:bush_"..name.."_leaves",
		after_dig_node = function(pos, oldnode, oldmetadata, digger)
			minetest.set_node(pos, {name = "mindeca:bush_"..name.."_leaves"})
			minetest.get_node_timer(pos):start(math.random(300, 1500))
		end,
	})

	minetest.register_node("mindeca:bush_"..name.."_leaves", {
		description = def.bushdesc.."\n\nMindeca",
		drawtype = "allfaces_optional",
		tiles = {"mindeca_bush_"..name.."_leaves.png"},
		special_tiles = {"mindeca_bush_"..name.."_leaves_s.png"},
		paramtype = "light",
		light_source = def.light,
		groups = {snappy = 3, flammable = 2, leaves = 1, spirit = 1},
		drop = {
			max_items = 1,
			items = {
				{items = {"mindeca:bush_"..name.."_seed"}, rarity = 5},
				{items = {"mindeca:bush_"..name.."_leaves"}}
			}
		},
		sounds = default.node_sound_leaves_defaults(),

		on_timer = function(pos, elapsed)
			if minetest.get_node_light(pos) < 3 then
				minetest.get_node_timer(pos):start(200)
			else
				minetest.set_node(pos, {name = "mindeca:bush_"..name.."_berries"})
			end
		end,
	})

	local grow_minebush = { }
	grow_minebush["grow_new_"..name.."_bush"] = function (pos)
	function (pos)
		if not mindeca.can_grow(pos, def.grow) then
			-- try a bit later again
			minetest.get_node_timer(pos):start(math.random(1, 1))
			return
		end
		minetest.remove_node(pos)
		minetest.place_schematic({x = pos.x-1, y = pos.y, z = pos.z-1}, minetest.get_modpath("mindeca").."/schematics/mindeca_bush_berry_"..name..".mts", "random", nil, false)
	end

	minetest.register_node("mindeca:bush_rasp_seed", {
		description = "Raspberry Bush Seedling\n\nA bush to grow tart raspberries.\n\nMindeca",
		drawtype = "plantlike",
		tiles = {"mindeca_item_plant_bush_rasp.png"},
		inventory_image = "mindeca_item_plant_bush_rasp.png",
		wield_image = "mindeca_item_plant_bush_rasp.png",
		paramtype = "light",
		sunlight_propagates = true,
		walkable = false,
		on_timer = grow_minebush
		selection_box = {
			type = "fixed",
			fixed = {-4 / 16, -0.5, -4 / 16, 4 / 16, 7 / 16, 4 / 16}
		},
		groups = {snappy = 2, dig_immediate = 3, flammable = 2, attached_node = 1, sapling = 1},
		sounds = default.node_sound_leaves_defaults(),

		on_construct = function(pos)
			minetest.get_node_timer(pos):start(math.random(240, 600))
		end,

		on_place = function(itemstack, placer, pointed_thing)
			itemstack = default.sapling_on_place(itemstack, placer, pointed_thing,
				"mindeca:bush_rasp_seed",
				-- minp, maxp to be checked, relative to sapling pos
				-- minp_relative.y = 1 because sapling pos has been checked
				{x = -2, y = 1, z = -2},
				{x = 2, y = 2, z = 2},
				-- maximum interval of interior volume check
				4)
			return itemstack
		end,
	})
end
Error

Code: Select all

ModError: Failed to load and run script from D:\Users\Vee\Documents\!!!Games\Open World\minetest-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main\init.lua:
...-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main/bushes.lua:74: '<name>' expected near '('
stack traceback:
	[C]: in function 'dofile'
	...st-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main\init.lua:17: in main chunk
Check debug.txt for details.
And removing line gets this instead:

Code: Select all

ModError: Failed to load and run script from D:\Users\Vee\Documents\!!!Games\Open World\minetest-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main\init.lua:
...-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main/bushes.lua:93: '}' expected (to close '{' at line 83) near 'selection_box'
stack traceback:
	[C]: in function 'dofile'
	...st-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main\init.lua:17: in main chunk
Check debug.txt for details.
Because clearly I want to close line 83 at the selection box info >.>

loosewheel
Member
Posts: 155
Joined: Mon Dec 28, 2020 01:19
GitHub: loosewheel
In-game: loosewheel

Re: Arrays and functions formatting

by loosewheel » Post

line 74
function (pos)
the compiler is expecting a name between function and (, but this line shouldn't be there at all. Its on the line before it.

line 93
on_timer = grow_minebush
doesn't have a comma after it. The compiler is saying its expecting a } to close the table, but it needs a comma. It also doesn't reference the function, just the table. I assume it should be this
on_timer = grow_minebush["grow_new_"..name.."_bush"],

The table definition should be before the for loop.

Code: Select all

local grow_minebush = { }
for name,def in pairs(mindeca.mbush) do
   ...
end
That would compile but loops have their own scope (local table that gets destroyed at the end of every iteration and a new one created on the next). The function will still exist because it is referenced to on_timer, but each iteration will create a new table with one function which wont be accessible outside of the loop. If you want a single table with all the functions that can be accessed later, it needs to be defined outside of the for loop.

Icalasari
Member
Posts: 166
Joined: Tue Sep 23, 2014 05:29
IRC: Icalasari
In-game: Icalasari

Re: Arrays and functions formatting

by Icalasari » Post

Of course I missed a comma. The more I try at this, the more frustrated I get even when coming back days later and the less able I am to think >.<

Code:

Code: Select all

mindeca.mbush = {
	--bush = {
		--"Bush Desc",
		--"Seedling Desc",
		--"Fruit",
		--"Berry Laden Bush Desc",
		--"Grow Cycle",
		--"Berry Grow Cycle",
		--"Light Level",
		--"Water Need"},
	--Spring
	--Summer
	rasp = {
		bushdesc = "Raspberry Bush Leaves\n\nLeaves that once bore raspberries.",
		seeddesc = "Raspberry Bush Seedling\n\nA seedling to grow a bush of tart raspberries.",
		fruit = "raspberry",
		bushfdesc = "Raspberry Bush Leaves (Berry Laden)\n\n[Creative]",
		grow = spirit,
		bgrow = rasp,
		light = nil,
		water = nil},
	--Autumn
	--Winter
}



local grow_minebush = { }
for name,def in pairs(mindeca.mbush) do
	minetest.register_node("mindeca:bush_"..name.."_berries", {
		description = def.bushfdesc.."\n\nMindeca",
		drawtype = "allfaces_optional",
		tiles = {"mindeca_bush_"..name.."_leaves.png^mindeca_bush_"..name.."_berries.png"},
		special_tiles = {"mindeca_bush_"..name.."_leaves_s.png^mindeca_bush_"..name.."_berries.png"},
		paramtype = "light",
		light_source = def.light,
		groups = {snappy = 3, flammable = 2, leaves = 1, dig_immediate = 3, spirit = 1},
		drop = "mindeca:item_fruit_"..def.fruit,
		sounds = default.node_sound_leaves_defaults(),
		node_dig_prediction = "mindeca:bush_"..name.."_leaves",
		after_dig_node = function(pos, oldnode, oldmetadata, digger)
			minetest.set_node(pos, {name = "mindeca:bush_"..name.."_leaves"})
			minetest.get_node_timer(pos):start(math.random(300, 1500))
		end,
	})

	minetest.register_node("mindeca:bush_"..name.."_leaves", {
		description = def.bushdesc.."\n\nMindeca",
		drawtype = "allfaces_optional",
		tiles = {"mindeca_bush_"..name.."_leaves.png"},
		special_tiles = {"mindeca_bush_"..name.."_leaves_s.png"},
		paramtype = "light",
		light_source = def.light,
		groups = {snappy = 3, flammable = 2, leaves = 1, spirit = 1},
		drop = {
			max_items = 1,
			items = {
				{items = {"mindeca:bush_"..name.."_seed"}, rarity = 5},
				{items = {"mindeca:bush_"..name.."_leaves"}}
			}
		},
		sounds = default.node_sound_leaves_defaults(),

		on_timer = function(pos, elapsed)
			if minetest.get_node_light(pos) < 3 then
				minetest.get_node_timer(pos):start(200)
			else
				minetest.set_node(pos, {name = "mindeca:bush_"..name.."_berries"})
			end
		end,
	})

	function grow_minebush["grow_new_"..name.."_bush"] (pos)
		if not mindeca.can_grow(pos, def.grow) then
			-- try a bit later again
			minetest.get_node_timer(pos):start(math.random(1, 1))
			return
		end
		minetest.remove_node(pos)
		minetest.place_schematic({x = pos.x-1, y = pos.y, z = pos.z-1}, minetest.get_modpath("mindeca").."/schematics/mindeca_bush_berry_"..name..".mts", "random", nil, false)
	end

	minetest.register_node("mindeca:bush_"..name.."_seed", {
		description = def.seeddesc.."\n\nMindeca",
		drawtype = "plantlike",
		tiles = {"mindeca_item_plant_bush_"..name..".png"},
		inventory_image = "mindeca_item_plant_bush_"..name..".png",
		wield_image = "mindeca_item_plant_bush_"..name..".png",
		paramtype = "light",
		sunlight_propagates = true,
		walkable = false,
		on_timer = grow_minebush["grow_new_"..name.."_bush"],
		selection_box = {
			type = "fixed",
			fixed = {-4 / 16, -0.5, -4 / 16, 4 / 16, 7 / 16, 4 / 16}
		},
		groups = {snappy = 2, dig_immediate = 3, flammable = 2, attached_node = 1, sapling = 1},
		sounds = default.node_sound_leaves_defaults(),

		on_construct = function(pos)
			minetest.get_node_timer(pos):start(math.random(240, 600))
		end,

		on_place = function(itemstack, placer, pointed_thing)
			itemstack = default.sapling_on_place(itemstack, placer, pointed_thing,
				"mindeca:bush_rasp_seed",
				-- minp, maxp to be checked, relative to sapling pos
				-- minp_relative.y = 1 because sapling pos has been checked
				{x = -2, y = 1, z = -2},
				{x = 2, y = 2, z = 2},
				-- maximum interval of interior volume check
				4)
			return itemstack
		end,
	})
end
Error because of course there's another error:

Code: Select all

ModError: Failed to load and run script from D:\Users\Vee\Documents\!!!Games\Open World\minetest-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main\init.lua:
...-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main/bushes.lua:73: '(' expected near '['
stack traceback:
	[C]: in function 'dofile'
	...st-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main\init.lua:17: in main chunk
Check debug.txt for details.
Yes I put (, I closed the (, then it gives an error asking for a ... and then another error and another error and another error no matter what I do I am just

gggggggg

EDIT: Sorry, just... This and the Sunflower are really driving me nuts. It's just... Really wearing on me

User avatar
Skamiz Kazzarch
Member
Posts: 613
Joined: Fri Mar 09, 2018 20:34
GitHub: Skamiz
In-game: Skamiz
Location: la lojbaugag.

Re: Arrays and functions formatting

by Skamiz Kazzarch » Post

function grow_minebush["grow_new_"..name.."_bush"] (pos)
should be
grow_minebush["grow_new_"..name.."_bush"] = function(pos)

User avatar
Blockhead
Member
Posts: 1623
Joined: Wed Jul 17, 2019 10:14
GitHub: Montandalar
IRC: Blockhead256
In-game: Blockhead Blockhead256
Location: Land Down Under
Contact:

Re: Arrays and functions formatting

by Blockhead » Post

The sooner you can figure out the actual syntax of Lua the sooner you'll stop butting your head against a brick wall looking at the weeds of what each error message tells you and looking more holistically at what's wrong on that particular line. Instead of "it tells me it expects this, I'll put one there", you can begin to think in terms like "this function declaration is wrong". In this case, Lua's syntax for the start of a function declaration takes only a few valid forms. Here's a tutorial on those:

Code: Select all

--[[ 0. The syntax of a function declaration (informally)
Don't worry if you don't understand it all at once - we will go through many examples
Function Declaration :=
function Name Funcbody 
OR
local function Name Funcbody
OR
var = function Funcbody

var is a simple variable name, or an expression with tables, which we will cover later

Funcbody := (Arglist)
    -- Any Lua code goes in here
    ...
end

Arglist := () OR (arg1) OR (arg1, arg2) OR (arg1, arg2, arg3) ... etc
There is also varargs syntax, but we will ignore it for simplicity's sake
--]]


-- 1. Global function
function name()
function name(arg1)
function name(arg1, arg2)
...

-- 2. Local function.
local function name()
local function name(arg1)
local function name(arg1, arg2)
...

-- 3. Function that lives inside a table, possibly inside another table
-- key3, key5, and key6 must already be defined as a tables
function table_variable.key1()
function table_variable.key2(arg1)
function table_variable.key3.key4(arg1, arg2)
function table_variable.key5.key6.key7(arg1, arg2, 3)
...

-- 4. Assigning a function to be the value of a global variable. Equivalent in effect to (1)
name = function()
name = function(arg1)
name = function(arg1, arg2)

-- 5. Also a function that lives inside a table, but with string keys instead of dot syntax.
-- These 3 declarations create the exact same variables as previous.
-- Once again, key3, key5, and key6 must already be defined as a tables.
table_variable["key1"] = function(arg)
table_variable["key2"] = function(arg1)
table_variable["key3"]["key4"] = function(arg1, arg2)
table_variable["key5"]["key6"]["key7"] = function(arg1, arg2, arg3)
...

-- 6. You can even mix [] and . syntax:
table_variable["key1"].key2["key3"] = function(arg1, arg2, arg3)

-- 7. You can even use a variable name inside the [], which you can't do with the . (dot):
local keyname = "key"
local table_variable = {}
table_variable[keyname] = {}
table_variable[keyname][keyname] = {}
table_variable[keyname][keyname][keyname] = function(arg1, arg2, arg3)

-- 8. You can use more complex expressions inside []
local table_of_functions = {}
local function_partial_name = "bruce"
table_of_functions["prefix_" .. function_partial_name .. "_suffix"] = function(arg1, arg2)
And remember every function needs its end statement! I have (mostly) just given the start of each function above, not the function body or the end statement. A more thorough specification of Lua's syntax can be found at the end of my favourite reference material, the Lua 5.1 Reference manual, and the Geany reference posted recently

Now back to your problem line:

Code: Select all

    function grow_minebush["grow_new_"..name.."_bush"] (pos)
Using the above syntax tutorial, can you tell me how you would reformat that line so it's valid Lua?
/˳˳_˳˳]_[˳˳_˳˳]_[˳˳_˳˳\ Advtrains enthusiast | My map: Noah's Railyard | My Content on ContentDB ✝️♂

Icalasari
Member
Posts: 166
Joined: Tue Sep 23, 2014 05:29
IRC: Icalasari
In-game: Icalasari

Re: Arrays and functions formatting

by Icalasari » Post

Blockhead wrote:
Mon Aug 15, 2022 04:18
The sooner you can figure out the actual syntax of Lua the sooner you'll stop butting your head against a brick wall looking at the weeds of what each error message tells you and looking more holistically at what's wrong on that particular line. Instead of "it tells me it expects this, I'll put one there", you can begin to think in terms like "this function declaration is wrong". In this case, Lua's syntax for the start of a function declaration takes only a few valid forms. Here's a tutorial on those:

SNIP

Code: Select all

-- 4. Assigning a function to be the value of a global variable. Equivalent in effect to (1)
name = function()
name = function(arg1)
name = function(arg1, arg2)

Code: Select all

    function grow_minebush["grow_new_"..name.."_bush"] (pos)
Using the above syntax tutorial, can you tell me how you would reformat that line so it's valid Lua?
The name order there. Skamiz did answer it but I understand now. Guess most of my problems are syntax related, so going to pay extra close attention to that chapter tomorrow after work (woo it being too late at night to do any more trouble shooting nor looking at the links in detail)

All the help I am getting is greatly appreciated, but I like the way you help the most XD It's honestly helping me see the why of what I did wrong rather than just the what. Other help I do try to understand, or I try to backward engineer, but as you can see, it's... Not working out like it usually does for other problems I approach that way

Now I'm getting a new "error" but my god it... This makes me feel like a ton of weight is off my shoulders, I think it's because I am now understanding it instead of feeling like I'm guessing at what is wrong?

I may need to stop looking at code for a few days after this just to get my head straight

Last part is getting it to refer to the growth cycle. I at least know what's going wrong here, just not sure how to go around it since well, both need to be function(pos), right?

can_grow(pos, def.grow) doesn't work

Code: Select all

	grow_minebush["grow_new_"..name.."_bush"] = function(pos)
	can_grow["can_grow_"..def.grow] = function(pos)
		if not mindeca.can_grow(pos) then
			-- try a bit later again
			minetest.get_node_timer(pos):start(math.random(1, 1))
			return
		end
		minetest.remove_node(pos)
		minetest.place_schematic({x = pos.x-1, y = pos.y, z = pos.z-1}, minetest.get_modpath("mindeca").."/schematics/mindeca_bush_berry_"..name..".mts", "random", nil, false)
	end
If I do try can_grow(pos, def.grow, then I get this:

Error:

Code: Select all

AsyncErr: Lua: Runtime error from mod 'mindeca' in callback node_on_timer(): ...-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main/bushes.lua:75: attempt to call upvalue 'can_grow_' (a table value)
stack traceback:
	...-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main/bushes.lua:75: in function <...-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main/bushes.lua:74>
Lines 74-82 of the Code:

Code: Select all

	grow_minebush["grow_new_"..name.."_bush"] = function(pos)
		if not can_grow_(pos, def.grow) then
			-- try a bit later again
			minetest.get_node_timer(pos):start(math.random(1, 1))
			return
		end
		minetest.remove_node(pos)
		minetest.place_schematic({x = pos.x-1, y = pos.y, z = pos.z-1}, minetest.get_modpath("mindeca").."/schematics/mindeca_bush_berry_"..name..".mts", "random", nil, false)
	end
The table in question + Local Functions that are declared:

Code: Select all

mindeca.mbush = {
	--bush = {
		--"Bush Desc",
		--"Seedling Desc",
		--"Fruit",
		--"Berry Laden Bush Desc",
		--"Grow Cycle",
		--"Berry Grow Cycle",
		--"Light Level",
		--"Water Need"},
	--Spring
	--Summer
	rasp = {
		bushdesc = "Raspberry Bush Leaves\n\nLeaves that once bore raspberries.",
		seeddesc = "Raspberry Bush Seedling\n\nA seedling to grow a bush of tart raspberries.",
		fruit = "raspberry",
		bushfdesc = "Raspberry Bush Leaves (Berry Laden)\n\n[Creative]",
		grow = _spirit,
		bgrow = rasp,
		glow = nil,
		water = nil},
	--Autumn
	--Winter
}



local grow_minebush = { }
local can_grow_ = { }
EDIT: Got finished getting ready faster than I expected and got a chance to look at the link and...

function in can_grow["can_grow_"..def.grow] = function(pos) doesn't HAVE to be function, does it? I could have function2(pos) instead, right?

EDIT 2: Nope misread. Looks like it has to be function(pos) no matter what. Unless I'm still misreading from tiredness and mental exhaustion

Definitely need to come back to this tomorrow

EDIT 3: Just clicked this is the same as on_place = function(blah), after_dignode = function(blah), and so on

...I need to stop putting off sleep due to things occurring to me. Still a bit confused but I think I have an idea? I think. Not really confident

EDIT 4: Nope, I had no idea. But I know the issue is this part:

Code: Select all

	grow_minebush["grow_new_"..name.."_bush"] = function(pos)
		if not can_grow_(pos, def.grow) then
			-- try a bit later again
			minetest.get_node_timer(pos):start(math.random(1, 1))
			return
		end
I'm not sure how to make if not can_grow_(pos, def.grow) then or if not can_grow_["can_grow_"..name] then work. It feels like something obvious, like something I should be able to easily figure out from even my own code, but something is not clicking

Icalasari
Member
Posts: 166
Joined: Tue Sep 23, 2014 05:29
IRC: Icalasari
In-game: Icalasari

Re: Arrays and functions formatting

by Icalasari » Post

Tried a few more solutions, just getting nil value errors. And I am not sure what to google to figure out how to do this right (attempted a few times and just getting things that don't apply at all). Still keeping at it (especially with the self contained growth node stuff figured out - Woo learning more and more about syntax and grammar for Lua to the point of a confidence boost?), but it's a head scratcher on how to do this right

Code:

Code: Select all

mindeca.mbush = {
	--bush = {
		--"Bush Desc",
		--"Seedling Desc",
		--"Fruit",
		--"Berry Laden Bush Desc",
		--"Grow Cycle",
		--"Berry Grow Cycle",
		--"Light Level",
		--"Water Need"},
	--Spring
	--Summer
	rasp = {
		bushdesc = "Raspberry Bush Leaves\n\nLeaves that once bore raspberries.",
		seeddesc = "Raspberry Bush Seedling\n\nA seedling to grow a bush of tart raspberries.",
		fruit = "raspberry",
		bushfdesc = "Raspberry Bush Leaves (Berry Laden)\n\n[Creative]",
		grow = "fullsun",
		bgrow = rasp,
		glow = nil,
		water = nil},
	--Autumn
	--Winter
}



local grow_minebush = { }
local can_grow_ = { }
for name,def in pairs(mindeca.mbush) do
	minetest.register_node("mindeca:bush_"..name.."_berries", {
		description = def.bushfdesc.."\n\nMindeca",
		drawtype = "allfaces_optional",
		tiles = {"mindeca_bush_"..name.."_leaves.png^mindeca_bush_"..name.."_berries.png"},
		special_tiles = {"mindeca_bush_"..name.."_leaves_s.png^mindeca_bush_"..name.."_berries.png"},
		paramtype = "light",
		light_source = def.light,
		groups = {snappy = 3, flammable = 2, leaves = 1, dig_immediate = 3, spirit = 1},
		drop = "mindeca:item_fruit_"..def.fruit,
		sounds = default.node_sound_leaves_defaults(),
		node_dig_prediction = "mindeca:bush_"..name.."_leaves",
		after_dig_node = function(pos, oldnode, oldmetadata, digger)
			minetest.set_node(pos, {name = "mindeca:bush_"..name.."_leaves"})
			minetest.get_node_timer(pos):start(math.random(300, 1500))
		end,
	})

	minetest.register_node("mindeca:bush_"..name.."_leaves", {
		description = def.bushdesc.."\n\nMindeca",
		drawtype = "allfaces_optional",
		tiles = {"mindeca_bush_"..name.."_leaves.png"},
		special_tiles = {"mindeca_bush_"..name.."_leaves_s.png"},
		paramtype = "light",
		light_source = def.light,
		groups = {snappy = 3, flammable = 2, leaves = 1, spirit = 1},
		drop = {
			max_items = 1,
			items = {
				{items = {"mindeca:bush_"..name.."_seed"}, rarity = 5},
				{items = {"mindeca:bush_"..name.."_leaves"}}
			}
		},
		sounds = default.node_sound_leaves_defaults(),

		on_timer = function(pos, elapsed)
			if minetest.get_node_light(pos) < 3 then
				minetest.get_node_timer(pos):start(1)
			else
				minetest.set_node(pos, {name = "mindeca:bush_"..name.."_berries"})
			end
		end,
	})

	grow_minebush["grow_new_"..name.."_bush"] = function(pos)
		if not can_grow_[def.grow](pos) then
			-- try a bit later again
			minetest.get_node_timer(pos):start(math.random(1, 1))
			return
		end
		minetest.remove_node(pos)
		minetest.place_schematic({x = pos.x-1, y = pos.y, z = pos.z-1}, minetest.get_modpath("mindeca").."/schematics/mindeca_bush_berry_"..name..".mts", "random", nil, false)
	end

	minetest.register_node("mindeca:bush_"..name.."_seed", {
		description = def.seeddesc.."\n\nMindeca",
		drawtype = "plantlike",
		tiles = {"mindeca_item_plant_bush_"..name..".png"},
		inventory_image = "mindeca_item_plant_bush_"..name..".png",
		wield_image = "mindeca_item_plant_bush_"..name..".png",
		paramtype = "light",
		sunlight_propagates = true,
		walkable = false,
		on_timer = grow_minebush["grow_new_"..name.."_bush"],
		selection_box = {
			type = "fixed",
			fixed = {-4 / 16, -0.5, -4 / 16, 4 / 16, 7 / 16, 4 / 16}
		},
		groups = {snappy = 2, dig_immediate = 3, flammable = 2, attached_node = 1, sapling = 1},
		sounds = default.node_sound_leaves_defaults(),

		on_construct = function(pos)
			minetest.get_node_timer(pos):start(math.random(1, 1))
		end,

		on_place = function(itemstack, placer, pointed_thing)
			itemstack = default.sapling_on_place(itemstack, placer, pointed_thing,
				"mindeca:bush_rasp_seed",
				-- minp, maxp to be checked, relative to sapling pos
				-- minp_relative.y = 1 because sapling pos has been checked
				{x = -2, y = 1, z = -2},
				{x = 2, y = 2, z = 2},
				-- maximum interval of interior volume check
				4)
			return itemstack
		end,
	})
end
Error:

Code: Select all

AsyncErr: Lua: Runtime error from mod 'mindeca' in callback node_on_timer(): ...-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main/bushes.lua:75: attempt to call a nil value
stack traceback:
	...-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main/bushes.lua:75: in function <...-5.5.1-win64\bin\..\mods\mindeca\Mindeca Main/bushes.lua:74>
Just... Always that nil

EDIT: Took another look at the syntax list and spotted this - function ( [ args , ] ... ) body [ return values ] end

I tried turning if not can_grow_[def.grow](pos) then to if not can_grow_([def.grow] pos) then, but got an outright crash rather than just one when the bush attempts its check. There's something definitely not clicking for me

User avatar
Skamiz Kazzarch
Member
Posts: 613
Joined: Fri Mar 09, 2018 20:34
GitHub: Skamiz
In-game: Skamiz
Location: la lojbaugag.

Re: Arrays and functions formatting

by Skamiz Kazzarch » Post

this was correct
if not can_grow_[def.grow](pos) then
this is not
if not can_grow_([def.grow] pos) then

The square brackets in this: function ( [ args , ] ... ) are only meant to indicate that the function arguments are optional. They aren't actually meant to be in the code.

In Lua, square brackets are used for two things:
1) To index tables.
2) To write multiline comments and strings.
If you see them anywhere else (like bracketing arbitrary function arguments), something probably went wrong.

You might want to look into getting a linter. That is, a program / addon for your code editor, which checks for syntax errors. (Errors which prevent the game starting in the first place, because the code isn't valid Lua code.)


Back to the nil error.
The if statement is irrelevant, so I will leave it out.
can_grow_[def.grow](pos)
What's happening here, is that you have a variable 'can_grow_'. You index it with the key stored in 'def.grow'. And then you call the retrieved value as a function.
attempt to call a nil value means that the value you retrieved from the 'can_grow_' table is nil and thus the code tries to execute nil(pos) , which throws an error because you can only execute functions.

The question now becomes, why is the retrieved value nil?

Spoiler with my own observation, but feel free to try figure it out yourself first.
Spoiler
As far as I can tell, you create can_grow_ as an empty table. ... And then never put anything into it, so there is nothing to retrieve.
Calling on a function which doesn't exist expectedly doesn't work.

Icalasari
Member
Posts: 166
Joined: Tue Sep 23, 2014 05:29
IRC: Icalasari
In-game: Icalasari

Re: Arrays and functions formatting

by Icalasari » Post

I tried a few ideas but still the nil value. I added local can_grow_ = {fullsun = {grow = fullsun}, shade = {grow = shade}} but still nada

There's something I'm missing here and I'm not sure what it is. As in I'm not sure what to fill in or where, apparently

EDIT: What I'm really not understanding is grow_minebush["grow_new_"..name.."_bush"] = function(pos) works, but yet if not mindeca.can_grow_[def.grow](pos) then won't. It should be reading it as, for the raspberry part, if not mindeca.can_grow_fullsun](pos) but instead is going if not mindeca.can_grow_nil(pos)

EDIT:

Code: Select all

	grow_minebush["grow_new_"..name.."_bush"] = function(pos)
		if not can_grow_["mindeca.can_grow_"..def.grow](pos) then
			-- try a bit later again
			minetest.get_node_timer(pos):start(math.random(1, 1))
			return
		end
		minetest.remove_node(pos)
		minetest.place_schematic({x = pos.x-1, y = pos.y, z = pos.z-1}, minetest.get_modpath("mindeca").."/schematics/mindeca_bush_berry_"..name..".mts", "random", nil, false)
	end
Still getting a nil

User avatar
Skamiz Kazzarch
Member
Posts: 613
Joined: Fri Mar 09, 2018 20:34
GitHub: Skamiz
In-game: Skamiz
Location: la lojbaugag.

Re: Arrays and functions formatting

by Skamiz Kazzarch » Post

mindeca.can_grow_[def.grow](pos)
Assuming that def.grow == "fullsun" does neither turn into mindeca.can_grow_fullsun(pos) nor mindeca.can_grow_nil(pos)

Rather it turns into mindeca.can_grow_["fullsun"](pos), which is the same as mindeca.can_grow_.fullsun(pos) (notice the dot).

Does that help?

Icalasari
Member
Posts: 166
Joined: Tue Sep 23, 2014 05:29
IRC: Icalasari
In-game: Icalasari

Re: Arrays and functions formatting

by Icalasari » Post

Skamiz Kazzarch wrote:
Sun Aug 21, 2022 03:30
mindeca.can_grow_[def.grow](pos)
Assuming that def.grow == "fullsun" does neither turn into mindeca.can_grow_fullsun(pos) nor mindeca.can_grow_nil(pos)

Rather it turns into mindeca.can_grow_["fullsun"](pos), which is the same as mindeca.can_grow_.fullsun(pos) (notice the dot).

Does that help?
It did some, plus learned that it may be not grabbing it because the function is mindeca.can_grow_fullsun which is acting a global value...

Would this mean I need to copy everything in from the grow functions so they can be local?

Icalasari
Member
Posts: 166
Joined: Tue Sep 23, 2014 05:29
IRC: Icalasari
In-game: Icalasari

Re: Arrays and functions formatting

by Icalasari » Post

Been asking a few places and got a solution that worked!

Code: Select all

mindeca.mbush = {
	--bush = {
		--"Bush Desc",
		--"Seedling Desc",
		--"Fruit",
		--"Berry Laden Bush Desc",
		--"Grow Cycle",
		--"Berry Grow Cycle",
		--"Light Level",
		--"Water Need"},
	--Spring
	--Summer
	rasp = {
		bushdesc = "Raspberry Bush Leaves\n\nLeaves that once bore raspberries.",
		seeddesc = "Raspberry Bush Seedling\n\nA seedling to grow a bush of tart raspberries.",
		fruit = "raspberry",
		bushfdesc = "Raspberry Bush Leaves (Berry Laden)\n\n[Creative]",
		grow = "fullsun",
		bgrow = rasp,
		glow = nil,
		water = nil},
	--Autumn
	--Winter
}



local grow_minebush = { }
local can_grow_ = { }
for name,def in pairs(mindeca.mbush) do
	minetest.register_node("mindeca:bush_"..name.."_berries", {
		description = def.bushfdesc.."\n\nMindeca",
		drawtype = "allfaces_optional",
		tiles = {"mindeca_bush_"..name.."_leaves.png^mindeca_bush_"..name.."_berries.png"},
		special_tiles = {"mindeca_bush_"..name.."_leaves_s.png^mindeca_bush_"..name.."_berries.png"},
		paramtype = "light",
		light_source = def.light,
		groups = {snappy = 3, flammable = 2, leaves = 1, dig_immediate = 3, spirit = 1},
		drop = "mindeca:item_fruit_"..def.fruit,
		sounds = default.node_sound_leaves_defaults(),
		node_dig_prediction = "mindeca:bush_"..name.."_leaves",
		after_dig_node = function(pos, oldnode, oldmetadata, digger)
			minetest.set_node(pos, {name = "mindeca:bush_"..name.."_leaves"})
			minetest.get_node_timer(pos):start(math.random(300, 1500))
		end,
	})

	minetest.register_node("mindeca:bush_"..name.."_leaves", {
		description = def.bushdesc.."\n\nMindeca",
		drawtype = "allfaces_optional",
		tiles = {"mindeca_bush_"..name.."_leaves.png"},
		special_tiles = {"mindeca_bush_"..name.."_leaves_s.png"},
		paramtype = "light",
		light_source = def.light,
		groups = {snappy = 3, flammable = 2, leaves = 1, spirit = 1},
		drop = {
			max_items = 1,
			items = {
				{items = {"mindeca:bush_"..name.."_seed"}, rarity = 5},
				{items = {"mindeca:bush_"..name.."_leaves"}}
			}
		},
		sounds = default.node_sound_leaves_defaults(),

		on_timer = function(pos, elapsed)
			if minetest.get_node_light(pos) < 3 then
				minetest.get_node_timer(pos):start(1)
			else
				minetest.set_node(pos, {name = "mindeca:bush_"..name.."_berries"})
			end
		end,
	})

	grow_minebush["grow_new_"..name.."_bush"] = function(pos)
		if not mindeca["can_grow_" .. def.grow](pos) then
			-- try a bit later again
			minetest.get_node_timer(pos):start(math.random(1, 1))
			return
		end
		minetest.remove_node(pos)
		minetest.place_schematic({x = pos.x-1, y = pos.y, z = pos.z-1}, minetest.get_modpath("mindeca").."/schematics/mindeca_bush_berry_"..name..".mts", "random", nil, false)
	end

	minetest.register_node("mindeca:bush_"..name.."_seed", {
		description = def.seeddesc.."\n\nMindeca",
		drawtype = "plantlike",
		tiles = {"mindeca_item_plant_bush_"..name..".png"},
		inventory_image = "mindeca_item_plant_bush_"..name..".png",
		wield_image = "mindeca_item_plant_bush_"..name..".png",
		paramtype = "light",
		sunlight_propagates = true,
		walkable = false,
		on_timer = grow_minebush["grow_new_"..name.."_bush"],
		selection_box = {
			type = "fixed",
			fixed = {-4 / 16, -0.5, -4 / 16, 4 / 16, 7 / 16, 4 / 16}
		},
		groups = {snappy = 2, dig_immediate = 3, flammable = 2, attached_node = 1, sapling = 1},
		sounds = default.node_sound_leaves_defaults(),

		on_construct = function(pos)
			minetest.get_node_timer(pos):start(math.random(1, 1))
		end,

		on_place = function(itemstack, placer, pointed_thing)
			itemstack = default.sapling_on_place(itemstack, placer, pointed_thing,
				"mindeca:bush_rasp_seed",
				-- minp, maxp to be checked, relative to sapling pos
				-- minp_relative.y = 1 because sapling pos has been checked
				{x = -2, y = 1, z = -2},
				{x = 2, y = 2, z = 2},
				-- maximum interval of interior volume check
				4)
			return itemstack
		end,
	})
end
It was even a variant I tried before, just was if not mindeca["can_grow_"..def.grow] then instead of if not mindeca["can_grow_" .. def.grow](pos) then

For anybody else who was/is confused like me:

if not mindeca["can_grow_" .. def.grow](pos) then reads as, using Raspberry's fullsun, if not mindeca.can_grow_fullsun(pos) then. So the stuff inside gets added to the code right, but gets treated as if there is a dot proceeding it. And (pos) missing was making it just not run so the sapling never grew. Learned so much about the anatomy of a function

Thank you EVERYBODY for your help, I think I learned a ton about lua due to this as I am seeing now how the syntax works on this and understanding a lot more on how things get read

Edit: Another relief - I can now apply this to my tree code so I can get rid of the sapling file. I wonder if I can figure out how to use this to condense the growth files as well so there isn't need for a subfolder...

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests