deepthought
Full Member   Posts: 212 Medals: 1
|
 |
«
on:
2011-02-28 11:58:51 » |
|
how would i dynamically load a Java class from an inputstream? i intend to create a class for the AI for each type of character, then the game dynamically loads them. this should make the game easier to mod. any suggestions? :)thanks 
|
|
|
|
SimonH
JGO Strike Force    Posts: 896 Medals: 14
|
 |
«
Reply #1 on:
2011-02-28 12:03:50 » |
|
Check out reflection. It's pretty slow though - you might be better off using scripting.
|
|
|
|
cylab
JGO Kernel      Posts: 1940 Medals: 27
|
 |
«
Reply #2 on:
2011-02-28 12:35:26 » |
|
Recommending the use of scripting over reflection, because of performance considerations, is a bit questionable (at least  ) What you want is an URLClassloader and loadClass()
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
Games published by our own members! Go get 'em!
|
|
Eli Delventhal
« League of Dukes » JGO Kernel      Posts: 3573 Medals: 44
Game Engineer
|
 |
«
Reply #3 on:
2011-02-28 12:56:24 » |
|
Maybe this is just reflection again, bit couldn't he export every AI script as a separate class file, and then execute it as part of the program? That has worked for me in the past.
|
See my work:OTC Software<br /> Currently Working On:Secret project... I edit JGO in production, because I simply don't waste time writing bugs
|
|
|
Roquen
JGO Strike Force    Posts: 823 Medals: 25
|
 |
«
Reply #4 on:
2011-02-28 13:01:01 » |
|
Yeah scripting is the better way to go. Otherwise think "ClassLoader".
|
|
|
|
|
aazimon
Full Member   Posts: 208 Medals: 5
|
 |
«
Reply #5 on:
2011-02-28 13:06:42 » |
|
If the intent is to allow others to create MODs, a scripting language would be easier for non-programmers to work with. Another alternative is to use a type of Plug-in system.
|
|
|
|
|
Eli Delventhal
« League of Dukes » JGO Kernel      Posts: 3573 Medals: 44
Game Engineer
|
 |
«
Reply #6 on:
2011-02-28 13:32:54 » |
|
If the intent is to allow others to create MODs, a scripting language would be easier for non-programmers to work with. Another alternative is to use a type of Plug-in system.
I made an RPG where you could have a special script for each level, basically I made a simple scripting language then got translated into Java so would be loaded by the ClassLoader.
|
See my work:OTC Software<br /> Currently Working On:Secret project... I edit JGO in production, because I simply don't waste time writing bugs
|
|
|
aazimon
Full Member   Posts: 208 Medals: 5
|
 |
«
Reply #7 on:
2011-02-28 14:14:02 » |
|
I made an RPG where you could have a special script for each level, basically I made a simple scripting language then got translated into Java so would be loaded by the ClassLoader.
Cool. Where can I see the RPG?
|
|
|
|
|
Eli Delventhal
« League of Dukes » JGO Kernel      Posts: 3573 Medals: 44
Game Engineer
|
 |
«
Reply #8 on:
2011-02-28 16:19:18 » |
|
I made an RPG where you could have a special script for each level, basically I made a simple scripting language then got translated into Java so would be loaded by the ClassLoader.
Cool. Where can I see the RPG? You can look at its page here. http://www.otcsw.com/bge2.phpI basically ended up making all the code except the combat engine and then lost interest. The level editor was pretty complex, though.
|
See my work:OTC Software<br /> Currently Working On:Secret project... I edit JGO in production, because I simply don't waste time writing bugs
|
|
|
deepthought
Full Member   Posts: 212 Medals: 1
|
 |
«
Reply #9 on:
2011-02-28 18:40:55 » |
|
i had experimented a bit with loading classes using forname, and here's something along the lines of what i'm using. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public abstract class behavior { public character avatar;
public behavior(Character c) { avatar=c;
}
public abstract void loop(); }
pseudocode for the character class because i'm too lazy to find it:
Class c = Class.forname(classname);
constructor con = g.getconstructor( Class{Character.Class});
instance = (behavior) con.newinstance(new Object{this});
instance.loop(); |
i believe this way, reflection is only used to load the class and create an instance. the rest should be able to take place at normal java speed.
|
|
|
|
Games published by our own members! Go get 'em!
|
|
aazimon
Full Member   Posts: 208 Medals: 5
|
 |
«
Reply #10 on:
2011-02-28 19:02:37 » |
|
It looks like your psuedo code, you have casting the loaded class to an interface. If you have an interface api setup, you have the code check a given directory for new classes and load them. You could even do this during the run time of the game. But you will need to insure that the class is an instance of the interface to avoid errors.
|
|
|
|
|
deepthought
Full Member   Posts: 212 Medals: 1
|
 |
«
Reply #11 on:
2011-03-01 08:26:11 » |
|
so i would call getgenericinterfaces() on the class i loaded and check that it implements my interface?
|
|
|
|
aazimon
Full Member   Posts: 208 Medals: 5
|
 |
«
Reply #12 on:
2011-03-01 11:53:01 » |
|
If you have a Java Object already, all you need to do is 1
| if (obj instanceof MyInterface) |
Then cast it to the interface and start using it. Otherwise, yes. You can use getGenericInterfaces() or getInterfaces(), which will give you the Class objects.
|
|
|
|
|
deepthought
Full Member   Posts: 212 Medals: 1
|
 |
«
Reply #13 on:
2011-03-01 18:27:08 » |
|
::)derp! why did i not remember that?
|
|
|
|
|