I’ve created an XNA application that utilizes LuaInterface (via Lua) to control many aspects of the application itself.
I added an extension to an XNA class (specifically the KeyboardState object). The problem is, if I call the extended function with Lua, I get an error: Attempt to call field ‘KeyDown’ (a userdata value.
Here is my Lua script:
Game.Updated:Add(function(t)
if Players.LocalPlayer.Controller.KeyboardState:KeyDown(KeyboardKey.W) then
print("W key down")
end
end)
And here is my module extending the class:
Imports System.Runtime.CompilerServices
Module XNAExtensions
<Extension()> _
Public Function KeyDown(ByVal this As Microsoft.Xna.Framework.Input.KeyboardState, ByVal e As Integer) As Boolean
Return this.IsKeyDown(e)
End Function
End Module
What am I doing wrong?
Extension methods are a function of the compiler, not the framework. In order to call this from Lua, you will need to call your XNAExtensions module directly as a static method. Lua doesn’t understand extension methods.