Put methods and objects in xml

Craaaazy question…

Does this sound feeeeesable? Write a method called process like so…


public void process(string cmd, string parms) {
      // get the cmd
      // get the num of parms by parsing string by delimeter
               // do something
}

Use the cmd string to find the a matching xml node in an xml doc. Then get the number of parameters by parsing the parms on a pre-determined delimeter. Check xml for matching parm count for the cmd node.
Then use this the parms to do some type of action on the parms and send results back to parent app.

How would one “do the action” based on data from the xml node for that cmd?

Example use: chat server where an admin might add a new xml node by sending the following string:
create @method wink @param-usera @param-target @action write[usera ‘winks at’ target]

the app would add an xml node like so:


<methods>
      <method name="wink">
            <paramCount>2</paramCount>
            <param>user a</param>
            <param>target</param>
            <action>usera 'winks at' target ( to server out channel)</action>
      </method>
</methods>

Then if a user, say kevgalss, types: wink blah3

room would get the result: kevglass winks at blah3

Where I am lost is the action part, how to get this to function.

Another example: drop blah3
result: the action would be to find the blah3 object and disconnect it.

Another example: poke blah3
result: the action may be to add one irritation point to blah3 and send: kevglass pokes blah3
if irritation points grow to over 20 for kevglass, blah3 would auto ignore kevglass

The goal here is to build a core for the chat server (or MUD that comes to mind as I write this) and then the person using the core could create his own emotes, actions, commands, etc…

My apologies for using members in the above theatrics. :wink: There is no compensation as the play has no financial backing.

EDIT: the more I think about it, this could be a good mud core with action of attack, etc…that have to be pre-built by an admin/dev, etc…

After a little work, I found that doing the emotes (not actually affecting the object) are fairly easy from xml. Now on to affecting objects, and making them. Got a piece of code to work that creates a java file, compiles it and loads the class all from an active app. This has to get me closer… :slight_smile:

for my PoolSplash game I wrote code that would generate a level, put it in a .java file, compile it, load it, and test it. If it was solveable, it’d keep it, if it wasn’t, it’d delete it. Then it would generate another level :slight_smile: I generated like over 1500 levels before weeding them down to 300.

/me is honoured, but doesn’t feel so happy about being poked…

Anyway, what you really want to do is forget this XML malarky, and use beanshell.org scripts for each instead.

that way, your users write simplified java, and you can load it from a file or from a string and it is TRIVIAL to integrate with any existing java code.

Example source from survivor will go online sometime a few weeks after GDC. Only…that’s when our commercial game launches, so maybe a few weeks after that :(.

I plan on researching bs, but in themeantime, how are classes, objects, methods created, altered by beanshell stored on the server? class or java file or both, if the system runs for 40 days and has many chagnes done “live” to it, then it is shutdown for some major code changes, are the runtime changes accessible to an api app like jbuilder or such?

Just use getDeclaredMethod(String name, Class[] parameterTypes) or getMethod(String name, Class[] parameterTypes) from the Class Class. Provide the name of the method you want and the array of classes for the signature of the method you want. For primitive types, you use the class value TYPE. Integer.TYPE is the class of the primitive int.

one last bs question: does this mean I can make these changes/calls from my telnet prompt while communicating with the app. IE in chat or connected to a mud?

When you are connected with Telnet, then when you send the command it creates/updates an XML file? If so, then you would have to re-read the XML file to execute the command.

If you already have the XML file loaded and it is just a reference of available commands that you can send over Telnet, then yes.

As long as the class and method exists as you define in your call to getMethod(…)

The thing you need to be sure of is the type of the parameters in the XML file. It looks like they might all be Strings. If that is the case, then it is easy. If not, then you would have to modify the XML or know based on the param values what the class type would be.

and CapnJester, you are talking about beanshell?

No, I am talking about using XML and Reflection. I am not sure about what exactly you are trying to do, but you can create any class that exists in the classpath with Reflection. You can also invoke any method that exits in a class as well. So it is just a matter of how you want to communicate it through your program.

To be honest, I was working on a chat server for a friend that would be a base core. When connecting with telnet (or a chat client using telnet protocols), he could type something like:
create emote @target smile [$n smiles at $target]
or something similar and add the “smile” command to the system. After tinkering with this, I found I could do this with XML and have a single method that would take x number of args and use the xml definitions to find the command and apply the appropriate args.
Then I said to myself…this could solve a mud issue I was mulling over some time back. Build a core, then xml the method definitions, objects, etc… and let devs create much of the functionality as well as builders build the world. I have seen examples done, but they almost all have only done the emotes. What I can’t figure out is how to allow devs to add new methods/objects and the like that affect other objects.

example: the core is done with a bunch of emotes. Now a dev comes on line and types:
create go @target [other stuff here] and it would add a command/method that would allow the invoking user to “go 1456” and have his character moved to room id 1456. Or
create drop @target stuff stuff [more stuff] or whatever and this would create a drop command for a user to “drop knife”

The method drop will not have existed in any file, java or class, before that time.

I can do the emotes, can’t do the things that effect/change other objects.

sample emote, as you can see it is striclty string manipulation and returns that string to all connected(in room) need to figure out how to do the same but $n cast magic at $N and get the results and effect the target, etc…


<EMOTE NAME="smile">
  <NO_ARG>You smile happily.</ACT_NOTARG>
  <ROOM_NO_ARG>$n smiles happily.</PER_NOTARG>
  <SELF_BAD_ARG>There's no one by that name around.</ACT_BADTARG>
  <SELF_SELF_ARG>You smile at yourself.</ACT_ACTTARG>
  <ROOM_SELF_ARG>$n smiles at $mself.</PER_ACTTARG>
  <SELF>You smile at $M.</ACTOR>
  <TARGET>$n smiles at you.</TARGET>
  <ROOM>$n beams a smile at $N.</PEERS>
</EMOTE>

Basically, allow devs/admins to “code” mud object and functionality through type string while connected and active as a user/player in the mud, store the changes and make them available. Also, if the system is ever stopped, the “core” could still be edited in JBuilder (or the like) and when recompiled, all the admin/dev run-time added features will still be there.

asking too much?

Sorry. I can’t see how they could add commands like that without some kind of backing framework.

How did I miss this one!?!? :slight_smile:

If you want to do as you say a couple of posts up (without using BeanShell, which would be easier but would force your MUD devs to know a bit java syntax) you must first decide what your core objects are. For instance, if we’re talking about MUDs maybe:

Room
Actor
Object

Is enough? Then allow setting and getting of properties on these objects, i.e. make them beans really. Now command definition has some powerful tools to play with, you go command:


create [go target] [set ${player} location ${target}] [display You move to ${target.name}]

Where the generic form of the create command would be


create <command_spec> <instruction 1> <instruction 2> .. <instruction N>

Where command spec is:


[<command name> <parameter description>]

And instructions are defined as:


[set <entity> <property> <value>] 
[display <message>]

You’d have to have a relatively sophiscated infrastructure behind it to cope with converting from ids/tags/names to entities and back again, but not too tricky really. Specially not for you :wink:

Sync the command defs to XML, load them at startup… lovely.

And before anyone starts on me, yes its defining a new language for the sake of it. And yes it might be easier in bean shell. It might make it easier for devs somewhere tho.

Kev

PS. ‘kevglass winks at Blah3’

Thanks kev. My devs will more than likely not know java. I had started down that path with mudobject as the base object, then location/room and so on from it. with some core methods on it. I just thay my brainfreeze to get the concept onto paper then code, though I think I was close with the first post.

PS: kev, slow down, everytime you finish and start a new game, I get the urge to start a new one, but unlike you… I ain’t finish’d’others yet! ;D

Hehe, I don’t tend to finish many either, they do get quite far along, but never finished to my standards.

I’m trying a new tactic at the moment to help me slow down, I’m working on 3 projects at once :slight_smile:

Kev

just 3 ? :stuck_out_tongue: ;D

LOL

EDIT: Beans within beans from xml…lovin it! Found a nice xml - bean example and looks good.

Am I asking to much for the ability to…

Say you have the actor object. Very simple, name, type, hitpoints/lifpoints/hull and some simple methods to effect those.

What if I wanted to allow devs to add a new variable to the actor or a class that extends it? for example, the dev decides he/she wants to add a profession variable and the ascociated gets and sets… could this be done with the “command and xml” theory as well?

This is what I was trying to get out above about just assigning properties to entities. There may be some require properties, like location for actor, but this makes it very flexible.

Its even better if the properties are string -> object mappings. That way an entiy can have a another as a property value. All entities should be stringable so that the text based stuff can work.

Kev

Yeah I was thinking hash table with string keys…

Please excuse my supreme brain freeze, lack of knowledge, and complete clubieness on this… but I think what I am asking below will definitely require recompile and code on the dev part…unless I am looking right at kev’s suggestion and not seeing it. I understand having a table of objects for properites, and that an actor or object may have another as a property(neat)…but as for devs altering the following where I have added the question to the code below:


abstract class Command {

      Object initiator;
      Object target;
      String...

      public Command(String[] args) {
            
            set stuff here..
      
      }
      
      public void execute() {
            how can a dev alter what this does on creation or edit?
      }



}