• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
satsujin

Creating NET object in Lua

7 posts in this topic

Hi folks,

 

I'm having trouble creating a C#.NET object in Lua. I can do this no problem with the System.Console type but this object has another object in it's constructor so that may be the issue. I hope you guys can help.

 

Here is my c# code:

 

public class GameController : DrawableGameComponent
    {
        public string gcname;
 
        public GameController(Game game)
            : base(game)
        {
            this.gcname = "Pulled from gc";
        }

 

    }

 

Here is my lua code:

luanet.load_assembly ("PointClick.GameController");
luanet.load_assembly ("Microsoft.Xna.Framework.Game");
luanet.load_assembly ("Microsoft.Xna.Framework");
 
game = luanet.import_type("Microsoft.Xna.Framework.Game");
gObj2 = luanet.import_type("PointClick.GameController");
 
  Console.WriteLine(gObj.gcname);
 
  --gObj2=gOb2:GameController(game);
  gObj2.gcname=" to this";
  gObj= gObj2;

 

 

I get the error "attempt to index global 'gOb2' (a nil value)" on the line "gObj2.gcname=to this"

Thanks in advance.

Edited by satsujin
0

Share this post


Link to post
Share on other sites
First step? Fix your naming.

game = luanet.import_type("Microsoft.Xna.Framework.Game");
gObj2 = luanet.import_type("PointClick.GameController");


Here, you create an object named gObj2.

Console.WriteLine(gObj.gcname);


But here, you try to print out the gcname member of an object called gObj which, unless it's created somewhere else, doesn't exist at this point (note the missing '2' in the name), so it's probably just writing nil to the console if it's not erroring.

--gObj2=gOb2:GameController(game);


For clarity, if this line in your actual source is actually commented out, it should be omitted from the post. Because if it's not actually commented out in your working source, then it looks like you are trying to call a function on an object called gOb2, which again does not exist. (Remember, to this point you have created an object called gObj2 only, whereas this is trying to access gOb2 without the 'j'.

gObj2.gcname=" to this";


This line, where you indicate the error occurs, actually should work as long as the above call gObj2 = luanet.import_type("PointClick.GameController"); succeeds. After that call, you should test to see if gObj2==nil which would indicate that there is a problem with the import_type method call. It's entirely possible there is a problem here, but you report that the error is from trying to access the global gOb2 (note the missing 'j'), which you are not trying to access on this line, so the error kind of makes no sense to be on this line.

gObj= gObj2;


Finally, you get around to assigning gObj a value, but it's too late for the earlier attempts to access it to succeed.

The chief problem you have for debugging purposes is your poor naming. You have gObj, gOb2 and gObj2 all floating around in there, and those similar-but-different names just make it harder for you to spot problems. So I'd say, fix your naming first and see if it works; and if not, take a look at whatever is going on inside the call luanet.import_type("PointClick.GameController"); (of which I have no idea, not being a .NET kind of guy.)
0

Share this post


Link to post
Share on other sites

 


Sorry gOb2 was a typo I did not notice. But the problem seems to be that luanet.import_type("PointClick.GameController") is returning nil and I have no idea why. import_type does not return nil for existing NET references like "Microsoft.XNA.Framework" or "System.Console". The problem seems to come when trying to access or create custom classes in Lua. if gObj is registered with the Lua statemachine in C#, I can access its string type properties for example, but not a custom type. I think this is because custom types register as a black box userdata type in Lua. I hope somebody can help since there is practically zero support for Lua online. There are some sites that discuss how to handle userdata types in Lua of different implementations with C++ but that doesn't really work with C#.

 

Btw, sorry for the empty quote box above. Can't delete it in editor.

 

0

Share this post


Link to post
Share on other sites
I must admit I have no familiarity whatsoever with .NET, C#, or Lua integration with .NET. But something that jumps out at me:

gObj2 = luanet.import_type("PointClick.GameController");


This seems to me, based on the name, that it imports a type, rather than actually instancing an object of that type. So are you sure that you don't need to use some form of instancing after the type is imported in order to actually create an object of that type? It would make sense to me that after you import the type definition, you would then have to instance the type, ie using some syntax like:

GameControllerType=luanet.import_type("PointClick.GameController")
gamecontrollerobject=GameControllerType()
And that hunch of mine seems to be validated by this stackoverflow question, the first answer, which shows this syntax being used to import a Test type then instance an object of that type.

Now, given that a GameController requires an object of type Game passed in to the constructor, I suspect it would be more like this instead:

GameType = luanet.import_type("Microsoft.Xna.Framework.Game");
GameControllerType = luanet.import_type("PointClick.GameController");

gameobject=GameType()
gamecontrollerobject=GameControllerType(gameobject)
Again, I have no experience with these .NET frameworks so I can't really speak authoritatively, but this seems like it might be an avenue worth pursuing.
0

Share this post


Link to post
Share on other sites

JTippetts' answer looks right on the money.  GameController (your gObj2) is a type, not an object.  Maybe luanet.import_type is returning nil, but what you have is undoubtedly wrong.  My noise library uses Lua for its scripting, and a trivial script looks something like this:

luanet.load_assembly("Racket")
Perlin = luanet.import_type("Racket.Perlin")
p = Perlin()
--do stuff.

Perlin in this example is basically a class (not really, but it helps get the point across I think).

0

Share this post


Link to post
Share on other sites

Thanks folks but I saw that format of calling the import type in other posts and already tried that. Both approaches return nil on the import_type call. Also, if you use import_type on "System.Console" , for example, you can directly use WriteLine on the type variable. This is probably because it is defined as static.

 

I think the string I am passing to import_type or load_assembly must be wrong somehow. I've tried passing either "PointClick" and "PointClick.GameController" to load_assembly but it still returns nil on import_type("PointClick.GameController")

Edited by satsujin
0

Share this post


Link to post
Share on other sites
luanet.load_assembly ("PointClick");
gObj = luanet.import_type("PointClick.GameController");
gObj.gcname="to this"

Is undoubtedly the right snippet.  If that doesn't work, it's possible that lua can't find the assembly at all.

Edited by SeraphLance
0

Share this post


Link to post
Share on other sites

Yeah, you'll note in my last post I said tried both strings. I don't know if it's having trouble finding the assembly. I do know that error reporting with load_Assembly is non-existent. You can type nonsense in the string and it won't throw an exception. Not to worry, a temporary solution is to write a function in C# that returns a newly created instance of the object you want. That's not creating it in Lua but it works.....for now.

0

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  
Followers 0