MOgre, MOIS and Lua

rNakrani

18-01-2010 03:44:16

Hello!

So here is my dilemma, or perhaps its an easy thing to do but I haven't figured it out yet. Basically I am using LuaInterface with MOgre so that I can script mostly everything rather than code it.

From a technical level designer's perspective they will need to be able to add their own FrameStartEventHandler, FrameEndEventHandler and Input Handlers (KeyPressed, KeyReleased, etc...).

I can do this with Win32 assemblies like so:


luanet.load_assembly("System.Windows.Forms")

Form = luanet.import_type("System.Windows.Forms.Form")

function newForm_OnLoad
-- form on load stuff goes here
end

newForm = Form()
newForm.Load:Add(newForm_OnLoad)
newForm:Show()


In the example above, I use Lua to create a new Windows Form and add an event handler, completely written in Lua, to the form's Load event. Its great, I love it, it makes development move much faster.

When dealing with the MOgre and MOIS assemblies it is not working with the FrameEvent handlers and Input Event Handlers. For example (with relevant Lua script only):



luanet.load_assembly("an assembly containing the engine class I created...")
luanet.load_assembly("Mogre")
luanet.load_assembly("MOIS")

function keyboard_KeyPressed(keyEvent)
-- key pressed stuff goes here
end

-- Engine is a class that I have made in C#
-- The CreateKeyboard function creates a new keyboard object from the MOIS Input Manager
-- and it works great
engine = Engine()
keyboard = engine.CreateKeyboard()

-- The following line DOES NOT WORK
keyboard.KeyPressed:Add(keyboard_KeyPressed)


Has anyone else had to deal with this, or those who are familiar with Lua and LuaInterface have any ideas on how I could get the functionality to have technical level designers script frame and input event handlers?

I am really excited to having a fully scripted engine so easily, if I can get passed these event handler issues I should be pretty much set for a while (one would hope!) :mrgreen:

Thanks in advance!

smiley80

18-01-2010 15:18:34

This works for me:
luanet.load_assembly("test")
Engine=luanet.import_type("test.Engine")

function keyboard_KeyPressed(keyEvent)
print 'Works'
return true
end

engine = Engine()
keyboard = engine:CreateKeyboard()

keyboard.KeyPressed:Add(keyboard_KeyPressed)

test.Engine.CreateKeyboard returns a previously created MOIS.Keyboard.

What exception are you getting?

rNakrani

18-01-2010 20:19:08

Hi Smiley!

That is what is interesting, I am not getting any exceptions in my output window. I will provide some additional information. Here is a more full version of my "test" script:


luanet.load_assembly("some assembly that has my engine class")
luanet.load_assembly("Mogre")
luanet.load_assembly("MOIS")
luanet.load_assembly("MogreNewt")

Engine = luanet.import_type("some assembly that has my engine class.Engine.Engine")

Camera = luanet.import_type("Mogre.Camera")
Entity = luanet.import_type("Mogre.Entity")
SceneManager = luanet.import_type("Mogre.SceneManager")
SceneType = luanet.import_type("Mogre.SceneType")
Vector3 = luanet.import_type("Mogre.Vector3")

KeyCode = luanet.import_type("MOIS.KeyCode")

function keyboard_KeyPressed(Key)

print 'Works'
return true

end

function Start()

engine = Engine()

scriptedKeyboard = engine:CreateKeyboard()
scriptedKeyboard.KeyPressed:Add(keyboard_KeyPressed)

sceneManager = engine.Root:CreateSceneManager(SceneType.ST_GENERIC)
camera = sceneManager:CreateCamera("Camera")
engine.Root.AutoCreatedWindow:AddViewport(camera)

ninja = sceneManager:CreateEntity("ninja", "ninja.mesh")
Barrel = sceneManager:CreateEntity("Barrell", "Barrel.mesh")
knot = sceneManager:CreateEntity("knot", "knot.mesh")
ninja4 = sceneManager:CreateEntity("ninja4", "ninja.mesh")
fish = sceneManager:CreateEntity("fish", "fish.mesh")

sceneManager.RootSceneNode:CreateChildSceneNode():AttachObject(ninja)
sceneManager.RootSceneNode:CreateChildSceneNode():AttachObject(Barrel)
sceneManager.RootSceneNode:CreateChildSceneNode():AttachObject(knot)
sceneManager.RootSceneNode:CreateChildSceneNode():AttachObject(ninja4)
sceneManager.RootSceneNode:CreateChildSceneNode():AttachObject(fish)

camera:LookAt(ninja.BoundingBox.Center)

engine:Start()

end

Start()


The C# code for creating the keyboard looks like this, and I have confirmed it is being called:


public Keyboard CreateKeyboard()
{
return (Keyboard)m_InputManager.CreateInputObject(MOIS.Type.OISKeyboard, true);
}

smiley80

18-01-2010 22:08:17

Do you call scriptedKeyboard:Capture somewhere?
Cause if you don't, the events will never be raised.

rNakrani

19-01-2010 02:00:15

Leave it to the Ogre / MOgre, OIS / MOIS newbie to forget something critical :oops:

Works great now, thanks!

rNakrani

20-01-2010 00:58:45

Well, at least I thought it was. It doesn't crash but it does throw an exception. One that we all are familiar of, its just an issue of finding out what object its talking about.

Object reference not set to an instance of an object.
A first chance exception of type 'System.NullReferenceException' occurred in LuaInterface.dll

smiley80

20-01-2010 01:22:35

Do your event handler always return a bool where it's expected (KeyPressed, KeyReleased, FrameStarted, ...)?