Debugging scripts without restarting Moho

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
mwdf
Posts: 4
Joined: Sun Feb 12, 2006 4:52 pm
Location: WI, USA
Contact:

Debugging scripts without restarting Moho

Post by mwdf »

I've been using Moho for a while, and I'm finally starting to try to learn the Lua scripting stuff. However, I've found it a bit of a pain to be constantly restart Moho every time I mess up when experimenting with a script! (I'm not very good at it, obviously...)

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)
Pick Debug from the menu.

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.)
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

Very interesting way to look at the problem, but kinda roundabout, as it isn't reallly a problem - pressm <CTRL><F5> and all of the scripts are reloaded. I use that key combo so much when I'm debugging it burned into my brain. Sometimes reinventing the wheel gets you a better wheel but in this case it's already there.

However, using something like that to dynamically reload scripts for different purposes might be an interesting idea. Hmmm.
mwdf
Posts: 4
Joined: Sun Feb 12, 2006 4:52 pm
Location: WI, USA
Contact:

Post by mwdf »

Haha, that's pretty funny. For some reason, I did not know that!

I looked at the keyboard shortcuts in the manual and everywhere.

Anyway, yeah, you now have a method for dynamically loading external Lua code if you need it...


Scripting is fun!
Post Reply