So, here is some code. Put this in your menu and edit the location for the target .lua file (no UI, sorry).
Every time you choose that option from the menu, it loads your script afresh and evaluates whatever is in the file. (Your file should have access to a "moho" object and all the other globals.)
Code: Select all
-- THIS SCRIPT IS BASED HEAVILY ON LM SCRIPTS
-- ALSO, note that I have absolutely *NO* clue about this stuff.
--
-- [ DF_Debug 0.01a ]================================
--
-- Purpose:
-- Allow easy "realtime" debugging of Lua scripts without restarting moho.
-- Loads a lua chunk as specified and runs it.
ScriptName = "DF_Debug"
DF_Debug = {}
function DF_Debug:Name()
return "Debug"
end
function DF_Debug:Version()
return "0.01a"
end
function DF_Debug:Description()
return "Loads a lua file/chunk and runs it."
end
function DF_Debug:Creator()
return "mwdf"
end
function DF_Debug:UILabel()
return("Debug")
end
DF_Debug.fileName = "s:/projects/lua/test.lua"
DF_Debug.runCount = 0
function DF_Debug:Run(moho)
self.runCount = self.runCount + 1
print("*** Starting iteration " .. self.runCount .. " of " .. self.fileName)
local f = loadfile(self.fileName)
if (f) then
print ("*** Function loaded")
-- Now we need to give the script access to the moho object
local e = {}
e["moho"] = moho
-- And anything in the global environment (_G)
for k,v in pairs(_G) do
e[k] = v
end
-- And this sets the function's scope to the table e just built up
setfenv(f, e)
-- RUN IT!
f()
print ("*** Function complete")
else
print ("*** Load failure from " .. self.fileName)
end
end
To test this, make a dummy test.lua file somewhere and have it contain something like:
Code: Select all
print "HEY HEY HEY"
LM.GUI.Alert(LM.GUI.ALERT_INFO, "It's working!")
local mesh = moho:Mesh()
local v = LM.Vector2:new_local()
mesh:AddLonePoint(v, 0)
local a = math.pi * 2 / 10
for i = 1, 10 do
v.x = (.2 + math.random() * .6) * math.cos(a * i)
v.y = (.2 + math.random() * .6) * math.sin(a * i)
mesh:AppendPoint(v, 0)
end
v.x = 0
v.y = 0
mesh:AppendPoint(v, 0)
mesh:WeldPoints(0, mesh:CountPoints() - 1, 0)
mesh:SelectAll()
moho:CreateShape(true)
Now, try changing the file and selecting the menu again. Off you go into the wild and wacky world of trying new things in moho without restarting!
(I know I may have reinvented the wheel here -- if there is some better way to do this, I hope someone can point me in the right direction.)