Dynamic class loading

I honestly have no idea where this topic belongs :-[ but by an extremly tenuous link i’, placing it here :stuck_out_tongue:

I’m experimenting with dynamic class loading for game objects to avoid having to hard-code all the classes etc. and get the main core of the engine nicely abstracted from the game-specific stuff. So the idea is that i have a couple of base classes (DynamicObject and SpawnPoint) which are used as the interface, and things like player character, bullets, opponents extend from this. Then each one has a corresponding SpawnPoint which is used in the editor for setting position, properties etc.
The idea being that I should be able to write an extra pair of classes, drop the .class files into the correct directory, and have the editor load and use them without any modification :slight_smile:

Has anyone round here tried anything similar? It seems like it should work quite nicely, but I wonder if people had used different methods to solve a similar problem?

I use this system in the game i’m currently programming. It’s like a “mod” system (like Half-life, quake 3 etc…). Whan the game is launched, it lists all directories an class file in these directories, then it loads class it needs. To load a class dynamicly, use a ClassLoader :


ClassLoader loader = this.getClass().getClassLoader();
Player player = (Player)loader.loadClass(classPath).newInstance();

where classPath is the class filename & location.

I don’t know if i’ve correctly answered your question…

++
Chman

Well I’ve never used dynamic class loading for game entities before, but for loads of other stuff. It really gives a great deal of flexibility, but at the cost of performance (mo interfaces/objects).

Maton, how much performance loss had you been seeing? I dynamic bind juat about everything (just about all of my configuration is in an XML file) through ObjectFactories and store everything in WeakObjectCaches and hadn’t been able to measure the class load process during benchmarking. My applications spends most of its time in native code drawing.

The biggest problem I’ve had was the fact that much of what we did was interface based, such that our applications could be reused on other platforms (desktop, handheld and so forth), which meant that we had an awfull lot of interfaces, which took up a considerable size. I have no numbers for you regarding speed issues. But I don’t think that it will be that big a problem since hotspot should take care of inheritance issues. You could however mark the entities as final.

Having the configuration and such being really dynamic, shouldn’t cause any problems - the problem only comes when you have classes which are used in the rendering loop, but you’ll have to benchmark that to see if there is any loss of cycles - I doubt that it is noticable…

I was idlely wondering about speed issues, but since most (all?) of the dynamic loading should be done on level loading it shouldn’t be a problem hopefully.

On the other hand, it will happen quite alot in the level editor, and thats starting to chug slightly on large levels already :o Although thats probably my lazy rendering methods at the moment (almost everything in immediate mode :-[ )