fName?

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
User avatar
maxic
Posts: 64
Joined: Wed Sep 22, 2004 8:24 am
Location: Moscow, Russian Federation
Contact:

fName?

Post by maxic »

Help please. I can not find a error.

Code: Select all

function MOHO.BuildShapeMenu(menu, mesh, baseMsg, dummyMsg)
	menu:RemoveAllItems()
	if (mesh:CountShapes() == 0) then
		menu:AddItem("<None>", 0, dummyMsg)
		menu:SetEnabled(dummyMsg, false)
	else
		for i = 0, mesh:CountShapes() - 1 do

			local shape = mesh:Shape(i)
                      if (shape.fName) then
				 local my_name = shape.fName
			else
				 local my_name = "# "..i
			end

			menu:AddItem(my_name, 0, baseMsg + i)
		end
	end
end
Shape always remains empty.
Probably I not correctly define a name?
This part raises the doubts
mesh:Shape(i).fName
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

Okay, I did a little reformatting so you can see exactly where you are in the loops and conditional (if-then) statements:

Code: Select all

function MOHO.BuildShapeMenu(menu, mesh, baseMsg, dummyMsg)
	menu:RemoveAllItems()
	if (mesh:CountShapes() == 0) then
		menu:AddItem("<None>", 0, dummyMsg)
		menu:SetEnabled(dummyMsg, false)
	else
		for i = 0, mesh:CountShapes() - 1 do
			local shape = mesh:Shape(i)
			if (shape.fName) then
				local my_name = shape.fName
			else
				local my_name = "# "..i
			end
			menu:AddItem(my_name, 0, baseMsg + i)
		end
	end
end
the problem is here:

Code: Select all

			if (shape.fName) then
				local my_name = shape.fName
			else
				local my_name = "# "..i
			end
You are defining my_name as a local variable which is only valid inside that if-then-else structure. When you call the "menu:AddItem(my_name, 0, baseMsg + i)" method right after that, the my_name in it is nil, because the local variable inside the if-then-else structure no longer exists. As they say in the Lua documentation, this is an example of "lexical scoping". Whatever that means. Try this:

Code: Select all

function MOHO.BuildShapeMenu(menu, mesh, baseMsg, dummyMsg)
	menu:RemoveAllItems()
	if (mesh:CountShapes() == 0) then
		menu:AddItem("<None>", 0, dummyMsg)
		menu:SetEnabled(dummyMsg, false)
	else
		for i = 0, mesh:CountShapes() - 1 do
			local shape = mesh:Shape(i)
			local my_name = ""
			if (shape.fName) then
				my_name = shape.fName
			else
				my_name = "# "..i
			end
			menu:AddItem(my_name, 0, baseMsg + i)
		end
	end
end
Now, the my_name variable is defined at the same level as the AddItem. I suggest that you always try to keep your formatting proper, it will always point out problems like this. If I didn't do it (like when I first started scripting) I would be confused as hell and have a hard time finding most of my bugs. But that should make it work for you.

Also, the functions like "MOHO.BuildGroupMenu" in the lm_utilities.lua file use the "MOHO." at the start because they are basic things that LM wants to include but are really an all Lua part of Moho. It makes it part of the MOHO class of functions, it should be reserved for Lost Marble and you should not use it. Better to either make a utility file of your own (like svet_utilities.lua) in the moho>scripts>utility folder, and call it something like "SVET.BuildShapeMenu" or make it a local function inside of the script you are writing.

And good luck. If you need any more help, feel free to ask. And by the way, I like your stuff a lot. Keep up the good work.
User avatar
maxic
Posts: 64
Joined: Wed Sep 22, 2004 8:24 am
Location: Moscow, Russian Federation
Contact:

Post by maxic »

Thanks for detailed explanations. I have understood all.
Post Reply