General Java problem making flexible loader

Hello!



Preface: I’m willing to take any criticism on this, as it might be a case of “Yer doin’ it wrong.” :stuck_out_tongue:



I’ve spent the last week reworking a loader that handles building a sprite with defined animation sequences for different states. The data is in an XML file and contains everything specific to a character’s animations via the sprites (textures, texture coordinates, frame durations, states etc). Now, the problem that I have, is that the way I’ve designed it is to have a general “Animation Manager” class that every sprite gets. It has very generalized methods that more or less facilitate the “Animation Definitions” that are associated with different player states (IDLE, WALKING, CRAWLING for example). Basically, the Animation Manager is a control that handles a bunch of controls that perform specific duties.



Now, this all works fine and good hard coded. The problem is that I don’t want every character to have the same set of animation definitions. That was kinda the point of all this ~ So that I could have different subsets of specific animation controls for specific things. The problem is, that unless I know what to expect from the XML, I have no idea what definitions are going to be coming from that file. It could be IdleAnimation_Generic, or IdleAnimation_wPickingNose (lol), or IdleAnimation_wLookAround, etc etc.



I’ve looked at Class.forname(String) a bit, but I got about halfway with it and got a bit confused.



If anyone has any recommendations on what to do, or maybe another way to think about this, my ears are open. My brain hurts too much from pounding this problem for a week :stuck_out_tongue:



Thanks!

~FlaH

You are trying to go from XML to Java objects? This has been solved half-a-dozen ways and some are even standards. Do a google search for “Java XML serialization” and you’ll find all kinds of things.



But yeah, if you are trying to create objects from XML, generally at some point you go from some XML name to Class.forName() with the appropriate amount of mangling for your particular approach.



For anything beyond the simple stuff, you should definitely look at something prebuilt because it gets complicated pretty fast.

Yeah, I don’t even know if I chose the right way to go. I just thought since Java and XML sleep together that it might be a good format to use for data structures.



For my animation frames and sequences I’m just going fairly straight from XML to classes because I know the XML should have these things in it. The problem I’m having is that I don’t know which particular definition the XML file is going to have.



oooh. I guess it would’ve behooved me to actually post a sample of what I’m looking at. The thing is though, if I’m using Class.forName to determine what to do next, how on earth do I do anything with it once I get it in? I mean, all of the definitions are extended from an AbstractClass, so do I pull it in, have it set to this abstract, create a new instance of the class, and then have some generic setters to set the thing up (since you can only newInstance() empty constructors. I expected this XD)?



I’m halfway starting to think hardcoding this might be a good idea. XD



~FlaH



[java]

<SpriteProperties>

<!-- Other Sprite Information goes here -->

<SpriteAnimation>

<Sheet>resources/sprite/sprite_dummy.png</Sheet>

<AnimSequence PlayerState=“IDLE” SheetIndex=“0” FrameWidth=“64” FrameHeight=“64” Definition=“CharIdle”>

<AnimFrame EightWayFacing=“SOUTH”><Duration>0</Duration>

<X>0</X>

<Y>0</Y>

</AnimFrame>

<AnimFrame EightWayFacing=“SOUTHWEST”>

<Duration>0</Duration>

<X>64</X>

<Y>0</Y>

</AnimFrame>

<AnimFrame EightWayFacing=“WEST”>

<Duration>0</Duration>

<X>128</X>

<Y>0</Y>

</AnimFrame>

<AnimFrame EightWayFacing=“NORTHWEST”>

<Duration>0</Duration>

<X>192</X>

<Y>0</Y>

</AnimFrame>

<AnimFrame EightWayFacing=“NORTH”>

<Duration>0</Duration>

<X>256</X>

<Y>0</Y>

</AnimFrame>

<AnimFrame EightWayFacing=“NORTHEAST”>

<Duration>0</Duration>

<X>320</X>

<Y>0</Y>

</AnimFrame>

<AnimFrame EightWayFacing=“EAST”>

<Duration>0</Duration>

<X>384</X>

<Y>0</Y>

</AnimFrame>

<AnimFrame EightWayFacing=“SOUTHEAST”>

<Duration>0</Duration>

<X>448</X>

<Y>0</Y>

</AnimFrame>

</AnimSequence>

</SpriteAnimation>

</SpriteProperties>

[/java]

The general approach to things like this is something along the lines of:

[java]

Class myClass = Class.forName( myClassName );

Object mObject = myClass.newInstance();

…then use reflection and/or the Java beans framework to set the values on the object…

[/java]



There are literally dozens of frameworks that can do this with XML. JAXB is the standard Java version. Spring does it. Apache digester does it. I’ve even written my own before as part of Meta-JB. There was a time when you couldn’t swing a stick without hitting one of these.



If reflection scares you then hard-coding is always a solution.

Hello again!



Got it. Thanks pspeed. It’s a bit tricky and my loader is a bit hodgepodge at the moment but it works!



Cheers!

~FlaH