XNA Creators Club Online
Page 1 of 1 (17 items)
Sort Posts: Previous Next

Possible to make GameComponent (or any class) an Asset in XNA?

Last post 4/28/2008 3:41 PM by Onions. 16 replies.
  • 4/26/2008 8:57 PM

    Possible to make GameComponent (or any class) an Asset in XNA?

    This may sound outrageous, but I'm interested in knowing if I could load code (a cs file, for example), or a class, into my game while running and use whatever methods or members that may be in it.

     

    If not using it as an "asset" in XNA, then are there any other methods to load code on the fly (and still work on for the Xbox 360)?

  • 4/26/2008 10:06 PM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    You could compile a .dll file with additional code for your game exernal from the .exe, but you can't run raw uncompiled code in your game. That would require an interpreter of some sort.
    __________________________________________________
    Check out my XNA project here:
    http://people.cis.ksu.edu/~ccaywood/astroblast/
  • 4/26/2008 11:04 PM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    Yes, you can absolutely do that. Just write a TypeWriter for the content pipeline, and a TypeReader for the runtime, and you can load it as any other asset. Look for the content pipeline section of the documentation for samples and instructions.

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 4/27/2008 12:09 AM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    I've spent over 2 hours now trying to figure out how to get this to work, but I've gotten to the point where my MethodInfo variable seems to be null always... so I'm here to ask what bits of code loads a dll, finds the class/type I want to use, then the method within it - in which I'd like to invoke or possibly use a "delegate" to reference it (if I can).

     

    As for writing a content pipeline for cs files... I don't really know where to begin with that. The dll seems like it'd work for what I need, I just can't get it implemented no matter what I try, though.

     

    Thanks for all of y'alls help.

  • 4/27/2008 12:48 AM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    A .dll won't give you much advantage other than you can compile it separately from the rest of the program. You'll need to start a new project using a Game Library as your template, and implement your classes in there. Then add a reference to your main project to the .dll, and then access the classes by "DLLName.ClassName" or if you put "using DLLName" at the top, you can just access the class with ClassName.

    We might be able to help you better if you explain what you want to accomplish with using code separate from your main .exe.
    __________________________________________________
    Check out my XNA project here:
    http://people.cis.ksu.edu/~ccaywood/astroblast/
  • 4/27/2008 8:41 AM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    I'd like to release my game with one "mission" in it for people to play. Later, based on what my game already allows for (but I might have never had the idea for when I made the actual game) I'd like to release "mission" packs that ones could download.

    For this to work each mission pack would contain new content - such as textures, models, and even some XML data. But for it to really work how I want it to, I need to be able to go beyond just supplying it data (which XML does) and use actual code - namely methods in which to test data against each other that's already found in my main game, and then alter that data too.

     

    So, much like the other content, I need to give it a directory to search for any dll files that match a certain name or in a certain directory, and for it to load it and use the methods found in it (which may also be in a class) based on what my main game tells it using either what's already hard coded in it or in other loaded dll files from within that specific "mission" pack.

     

    Hopefully I've explained my planned use of it enough. If not, let me know.

    Thanks again.

  • 4/27/2008 2:43 PM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    You can't do that on Xbox. Assuming you only care about Windows, then what you want is to dynamically load an assembly.

    Here are the top three Google hits for the search term "load assembly dynamically." All three of them seem to explain exactly what you want:

    http://www.dotnet247.com/247reference/msgs/41/207717.aspx
    http://bartdesmet.net/blogs/bart/archive/2004/11/26/475.aspx
    http://www.codeproject.com/KB/cs/DynLoadClassInvokeMethod.aspx

    Note that life will be simpler on you if you define an interface in your main code, that the "mission" class can derive from, and boot-strap the process of talking between the application and the plug-in.

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 4/27/2008 3:22 PM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    My target platform is Xbox 360, not windows, so that is of main concern to me.

    May I ask this - how do XBLA games handle downloadable content exactly, if you can't dynamically load data that wasn't there to begin with?

     

    EDIT: Or rather, I shouldn't say how XBLA titles load data, but instead how they add code - because I know alot of added content for those games have more than just added parameters, but are in fact manipulating the data in various new ways (which only methods can do).

     

    EDIT2: (I'm editing alot here) Also, why do the methods I'm currently using to try and load a dll dynamically say it'll work on the 360? I'd also read some posts here that claimed some would work, too.

  • 4/27/2008 5:01 PM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    Well, I finally got it to work with this bit of code here...

     

                Assembly MyAssembly;
    MyAssembly = Assembly.LoadFrom("Missions.dll");

    Type[] MyType = MyAssembly.GetTypes();

    MethodInfo MyMethodInfo = MyType[0].GetMethod("Add");

    object[] parameters = { GameScene };
    MyMethodInfo.Invoke(null, parameters);

     

    I was having problems with MyMethodInfo.Invoke throwing an error about a wrong kind of object for my target, but after doing a search I found out this was do to a method not being considered "static" in my dll. Once I changed it to this it started working, and I can now change data in my game's "GameScene" dynamically (I can set the location of my "Missions.dll", for example, in one of ever how many "Mission" folders I add to the game at a later date).

    And from knowledge of looking over the help files, all the methods I'm using are Xbox360 friendly. But if not, I'd appreciate knowing now - heh.

  • 4/27/2008 5:32 PM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    sounds like a nice concept... the only way to really know if it works or not its just testing it on the 360...so go ahead try it out and let us know if it works ok
    Cheers!!

    David Montes de oca Segovia
  • 4/27/2008 7:44 PM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    Hah, well, you may laugh at this; but I can't test on a 360 actually. I've just been reading what others say on this forum and what the help files say as for what should work or not for the certain platforms.

     

    I use to have a 360 even, but it broke (for no reason at all... first time of that happening with any console of mine, in fact - but anyways...). I probably need to get another one and the membership thing to test my games on it, but I'm just focusing on getting a Windows version working that would be compatible with the 360 for demo purposes and proposals to the XBLA team.

    I'd submitted and gotten one other project to a board meeting before, and that was using Windows and DirectX (with no XNA). But after working with XNA for a little while now, I've been pretty impressed with what XNA can help with - namely, dealing and handling Windows, which was my biggest problem (and in my case now, making porting over to the 360 easier too, I would assume).

  • 4/27/2008 9:50 PM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    There are really several aspects to this.

    The first is, how do you dynamically load code? That's pretty easy thanks to to assembly and reflection API's in .NET: there's plenty of material about how to implement a plugin system using dynamically loaded assemblies which you can find through a web search.

    The next question which usually follows shortly after the first is, how do I then unload that code when I am done with it? This one is rather harder. On Windows, it requires you to isolate each unloadable module into a separate AppDomain, and use .NET remoting to marshal calls and data across the domain boundary. Possible, but fairly subtle to get right and quite a lot of work. On Xbox, .NET remoting is not supported, so there is no way to dynamiaclly unload code modules (or more pedantically, you can load and unload code using separate AppDomains, but there is no way to call into that second AppDomain, so in practice this is not useful). That may be ok as long as you have enough memory that you never need to unload your code, though.

    The worst question for you is once you have your code being loaded from these dynamic modules, how do you get those modules onto the Xbox? This is where you are plumb out of luck. There is no way to dynamically add any kind of content, code or other, into an XNA Framework game on Xbox. If your game design depends on dynamically adding new content, the XNA Framework on Xbox is probably not a good technology choice for you because this simply is not possible.
    XNA Framework Developer - blog - homepage
  • 4/27/2008 10:00 PM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    XNA GS does not give you access to any kind of incremental update. The planned community for games will require you to ship an entirely new download of your product, from what's been made public so far.
    If you have a publisher contract, you can get access to the the native XBLA libraries, which will allow you to do both code and data downloads.

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 4/27/2008 10:54 PM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    jwatte:
    XNA GS does not give you access to any kind of incremental update. The planned community for games will require you to ship an entirely new download of your product, from what's been made public so far.
    If you have a publisher contract, you can get access to the the native XBLA libraries, which will allow you to do both code and data downloads.

     

    I'm not working on this for being listed as a community game, or for a hobby. I know there is downloadable content for XBLA games, and as you said the XBLA libs will allow for this - so I suppose even if my code couldn't be left as-is if my game were to get greenlighted; it's a close enough representation of how it could/would work.

    I'd suspected I'd have to do some sort of porting if it were actually published anyways. So I'll just take what I got as safe enough since I know my planned end result in my design is possible atleast.

     

    Thanks for all of y'alls help, guys.

  • 4/28/2008 12:02 PM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    how do I then unload that code when I am done with it?


    Shawn, you should know the work-around: just reboot the console with the next level as the start-up binary. Clears out all that unnecessary garbage right quick.
    Although I assume that, given the way the current XNA CLR works on the Xbox, the start-up binary would actually be the launcher, which would complicate things. Clear proof that things are much easier with native development -- so just release the XDK, will you? :-)

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 4/28/2008 12:30 PM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    jwatte:
    how do I then unload that code when I am done with it?


    Shawn, you should know the work-around: just reboot the console with the next level as the start-up binary. Clears out all that unnecessary garbage right quick.



    That works nicely in native code, but not in managed land. The overhead of CLR spinup, plus the lack of any managed reboot or launch process API, makes this pretty much impossible in fact...
    XNA Framework Developer - blog - homepage
  • 4/28/2008 3:41 PM In reply to

    Re: Possible to make GameComponent (or any class) an Asset in XNA?

    Better yet guys, I've figured out how to get a derived class (of a parent class found in my main game) from my DLL and then make an instance of it that my main game stores and uses...

                Assembly MyAssembly;
    MyAssembly = Assembly.LoadFrom("Missions.dll");

    Type[] MyType = MyAssembly.GetTypes();

    GameMission = (Mission)Activator.CreateInstance(MyType[0]);

    This way I can not only manipulate my data found in my main game/exe using whatever variables I created or had in mind for then, but I can also add variables/members within my DLL's derived class and use them for added flexability of manipulating my game's data.

    And from what I've read, CreateInstance works on the 360 too. So I should still be safe.

     

    EDIT: By the way, if I were to load a DLL file of the same name over and over again (but with a different file path), would it overwrite its data, or would each file loaded still remain in memory?

Page 1 of 1 (17 items) Previous Next