Design of a Scripting system using XML

The question was asked here http://stackoverflow.com/q/10891470/897112 It is better formatted over there also. I think that that community can help me, but the jME community can help me a lot also (I think).



I’ve built a system that parses a XML file into a Script class (described above). That file contains information about the scripting language (Javascript, Python etc) that is being used and the code it should run. Then, I’ve built another system that runs a Script class, using the name and version provided, I can detect the correct engine to be used. Finally, after thinking and thinking of how to solve my problems, I failed to achieve so, therefore I ask your help.



Let me show you the current design



[java]class Script {



String content;

Language language;



class Language {



String name;

String version;

}

}



class Effect {



Script initial;

Script final;



}



class Usuable {



Script onUse;

Script onHit;



}[/java]



Necessities:


  • I want to be able to work with multiple languages and versions.
  • An Effect class therefore contains 2 Script classes instances. Those scripts are run multiple times.
  • The same applies for the Usuable class.
  • Scripts on the Usuable class may be the same of the ones used on Effect.
  • A Script may call another script while running.
  • Scripts should be able to recieve parameters to work with.



    Changeable:


  • Effect, Usuable and Script classes are loaded from XML files.



    I’ve seen the necessity of separating the Script from inside the Effect (on the XML files), so I’ve decided to use ID’s on those root nodes, so I can load the correct files. So the Script class gains an ID attribute, and in the Effect and Usuable XML files only stores the ID’s for those Effects.



    Now, to pass parameters to a script code located inside a XML file and run other Scripts, I’ve found no good solution, and I think that the problem is the solution I’ve come up. I wanted to avoid externalizing the code to the XML by creating Parameter elements in the Script XML files.



    So, I’ve imagined that I could load the Scripts classes, and the code inside them should be a function, that could be called. But I’ve not found a good way to keep up the references.


  1. Is that approach correct? If you can’t answer that, do you like it or think it’s good?
  2. Is it a good approach to store scripts in XML files? I’ve seen many other applications that simply put the script files in a ZIP file, and supouse they were made for the current version, but I cannot affort to trust that.
  3. How can I keep up with the references? I don’t know a good way to generate the ID’s, so they can pass information like a function name does.
  4. If I apply that last idea I’ve come up with, would it be good to name the functions with it’s ID’s?

Without stepping off of the deep end because this is a potentially HUGE topic…



Why have XML files at all? Why not just have scripts that run to setup the other structures you need? They could be any language you want either based on something in the file or based on their file extension or something. Or you just make sure that the init scripts are all one language.



You can connect up anything you want this way. And then I’m not sure what the point of the IDs would be since you could just give the objects their references.



I may be misunderstanding what you are trying to do but it seems overly complicated to me.

I have XML files so I can organize the codes for them to be independent of the scripting language. I could achieve the same with a extension format, that’s why I asked about that. The only problem is that, if I put the script files directly, wihout embeding in a XML file, meta-informations that I could put in the XML file would be lost (like date created, from wich part that script is from, why was it built, whom built it, additional comments). Although I could do a comment in the beginning of each file with that information, I think it wouldn’t change.



There must be multiple scripting language, it is a requirement.



A script must be able to call another existing script without much trouble, and for that, I must be able to show what are the scripts and what they do (that’s why I thought of XML files, meta-data would be easily retrieved). The problem is that I don’t know how to link those scripts. Somehow, inside the script A, I must detect that it uses script B and C, load B and C, for when it calls both are already loaded. To achieve this, I thought that if I transform B and C in functions, I could simply calling them using JSR-223 by transforming them into something callable.

Yeah, I’m not sure you understood my response but good luck.