Sign in to follow this  
MatsK

[.net] LuaInterface issues...

Recommended Posts

MatsK    394
Has anyone here used LuaInterface before? I'm trying to use it to get Lua scripting working from .NET.
I've managed to get it partially working, but I'm getting an error right now and I'm not sure what's causing.

The error is:

[code]
011.04.16-11.10.58:warn: Recoverable Lua Error
2011.04.16-11.10.58:warn: Recoverable Lua Error
2011.04.16-11.10.58:warn: Recoverable Lua Error
2011.04.16-11.10.58:warn: Recoverable Lua Error
2011.04.16-11.10.58:warn: Recoverable Lua Error
2011.04.16-11.10.58:warn: Recoverable Lua Error
2011.04.16-11.10.58:warn: Recoverable Lua Error
2011.04.16-11.10.58:warn: Recoverable Lua Error
2011.04.16-11.10.58:warn: Recoverable Lua Error
2011.04.16-11.10.59:warn: Recoverable Lua Error
2011.04.16-11.10.59:error: Lua syntax error!
instance method 'LoadBackground' requires a non null target object[/code]

Here is my Lua code:


[code]
luanet.load_assembly("TSOClient")
luanet.load_assembly("Microsoft.XNA.Framework")
luanet.load_assembly("Microsoft.XNA.Framework.Graphics")

UIScreen = luanet.import_type("TSOClient.LUI.UIScreen")
PersonSelectionScreen = UIScreen(ScreenManager, "uigraphics\\personselection\\personselection.dat")

PersonSelectionScreen.LoadBackground(0x3FA)[/code]



The last line is what seems to be causing the error.

Here is my C# code:

[code]using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using TSOClient.UI;
using TSOClient.LUI;
using SimsLib.FAR3;
using LogThis;

namespace TSOClient
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

FAR3Archive m_Archive;
MemoryStream m_TextureStream;
Texture2D m_Texture;

UIScene m_Scene;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";

Log.UseSensibleDefaults();
}

/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;

ScreenManager ScreenMgr = new ScreenManager(this);

LuaInterfaceManager.ExportObject("ScreenManager", ScreenMgr);
ScreenMgr.LoadInitialScreen("gamedata\\luascripts\\personselection.lua");

base.Initialize();
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
/*m_Scene = new UIScene("gamedata\\uiscripts\\personselection.uis", GraphicsDevice,
Content.Load<SpriteFont>("ComicSans"));*/
m_Archive = new FAR3Archive("uigraphics\\personselection\\personselection.dat");
m_TextureStream = new MemoryStream(m_Archive.GetItemByID(0x000003FA));
m_Texture = Texture2D.FromFile(graphics.GraphicsDevice, m_TextureStream);

// TODO: use this.Content to load your game content here
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}

/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here
//m_Scene.Render();

spriteBatch.Begin();

spriteBatch.Draw(m_Texture, new Rectangle(0, 0, 800, 600), Color.White);

spriteBatch.End();

base.Draw(gameTime);
}
}
}
[/code]



[code]
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using SimsLib.FAR3;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TSOClient.LUI
{
public class UIScreen
{
private ScreenManager m_ScreenMgr;

private List<Texture2D> m_Backgrounds = new List<Texture2D>();

private FAR3Archive m_Archive;
private string m_ArchivePath;

public UIScreen(ScreenManager ScreenMgr, string ArchivePath)
{
m_ScreenMgr = ScreenMgr;
/*LuaInterfaceManager.LuaVM.RegisterFunction("LoadBackground", this,
GetType().GetMethod("LoadBackground"));*/

m_ArchivePath = ArchivePath;
m_Archive = new FAR3Archive(ArchivePath);
}

public void LoadBackground(uint ID)
{
MemoryStream TextureStream = new MemoryStream(m_Archive.GetItemByID(ID));
m_Backgrounds.Add(Texture2D.FromFile(m_ScreenMgr.GraphicsDevice, TextureStream));
}
}
}[/code]


[code]using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TSOClient.LUI;

namespace TSOClient.LUI
{
public class ScreenManager
{
private Game m_G;
private List<UIScreen> m_Screens = new List<UIScreen>();

public GraphicsDevice GraphicsDevice
{
get { return m_G.GraphicsDevice; }
}

public ScreenManager(Game G)
{
m_G = G;
}

/// <summary>
/// Runs the Lua function that creates the initial UIScreen.
/// </summary>
/// <param name="Path">The path to the Lua script containing the function.</param>
public void LoadInitialScreen(string Path)
{
LuaInterfaceManager.RunFileInThread(Path);
}
}
}
[/code]


[code]using System;
using System.Collections.Generic;
using System.Text;
using LuaInterface;
using LogThis;

namespace TSOClient.LUI
{
public class LuaInterfaceManager
{
public static Lua LuaVM = new Lua();
private static int m_Retries = 0;

/// <summary>
/// Finds a Lua function.
/// From: http://forum.junowebdesign.com/net-programming/30025-luainterface-explained.html
/// </summary>
/// <param name="Name">The full path to the script containing the function.</param>
/// <returns>The Lua function.</returns>
public static LuaFunction FindFunc(string Name)
{
try
{
LuaFunction retfunc = LuaVM.GetFunction(Name);
return retfunc;
}
catch (Exception e)
{
Log.LogThis("Couldn't find Lua function!\r\n " + e.Message, eloglevel.error);
return null;
}
}

/// <summary>
/// Calls a Lua function defined in a script.
/// From: http://forum.junowebdesign.com/net-programming/30025-luainterface-explained.html
/// </summary>
/// <param name="Name">The full path to the Lua script.</param>
public static void CallFunction(string Name)
{
if (FindFunc(Name) == null)
{
return;
}
try
{
LuaFunction retfunc = LuaVM.GetFunction(Name);
retfunc.Call();

}
catch (Exception e)
{
Log.LogThis("Couldn't call Lua function!\r\n " + e.Message, eloglevel.error);
}
}

/// <summary>
/// Failsafe wrapper for Lua.DoFile().
/// Basically executes all the code in a script.
/// From: http://forum.junowebdesign.com/net-programming/30025-luainterface-explained.html
/// </summary>
/// <param name="Path">The path to the script.</param>
public static void RunFileInThread(string Path)
{
try
{
LuaVM.DoFile(Path);
}
catch (Exception ex)
{
m_Retries++;

if (m_Retries > 10)
{
Log.LogThis("Lua syntax error!\r\n " + ex.Message, eloglevel.error);
}
else
{
Log.LogThis("Recoverable Lua Error", eloglevel.warn);
RunFileInThread(Path);
}
}
}

/// <summary>
/// Exports a native object (class or variable) to Lua.
/// </summary>
/// <param name="ObjectName">The name of the object as it will appear to Lua.</param>
/// <param name="ExportableObj">The object to export.</param>
public static void ExportObject(string ObjectName, object ExportableObj)
{
LuaVM[ObjectName] = ExportableObj;
}
}
}
[/code]

Share this post


Link to post
Share on other sites
smart_idiot    1298
Sorry, I don't know about LuaInterface.

My best shot in the dark is that '[i]PersonSelectionScreen.LoadBackground(0x3FA[/i][i])[/i]' should be '[i]PersonSelectionScreen[color="#FF0000"][b]:[/b][/color][/i][i]LoadBackground(0x3FA[/i][i])[/i]', which is equivalent to [i]'PersonSelectionScreen.[/i][i]LoadBackground([/i][i]PersonSelectionScreen, [/i][i]0x3FA[/i][i])[/i]'

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this