Jump to content

  • Log In with Google      Sign In   
  • Create Account

Creating NET object in Lua


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 satsujin   Members   -  Reputation: 139

Like
0Likes
Like

Posted 24 November 2013 - 06:35 AM

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, 24 November 2013 - 06:57 AM.


Sponsor:

#2 JTippetts   Moderators   -  Reputation: 8648

Like
0Likes
Like

Posted 24 November 2013 - 10:31 AM

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.)

#3 satsujin   Members   -  Reputation: 139

Like
0Likes
Like

Posted 24 November 2013 - 11:04 AM

 


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.

 



#4 JTippetts   Moderators   -  Reputation: 8648

Like
0Likes
Like

Posted 24 November 2013 - 01:06 PM

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.

#5 SeraphLance   Members   -  Reputation: 1454

Like
0Likes
Like

Posted 24 November 2013 - 03:09 PM

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).



#6 satsujin   Members   -  Reputation: 139

Like
0Likes
Like

Posted 25 November 2013 - 01:44 AM

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, 25 November 2013 - 04:10 AM.


#7 SeraphLance   Members   -  Reputation: 1454

Like
0Likes
Like

Posted 25 November 2013 - 09:31 PM

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, 25 November 2013 - 09:34 PM.


#8 satsujin   Members   -  Reputation: 139

Like
0Likes
Like

Posted 25 November 2013 - 09:40 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS