Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  dynamically loading AI classes  (Read 1703 times)
0 Members and 1 Guest are viewing this topic.
Offline deepthought
« Posted 2011-02-28 17: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 Smiley

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline SimonH
« Reply #1 - Posted 2011-02-28 18:03:50 »

Check out reflection. It's pretty slow though - you might be better off using scripting.

Heroes' Keep is in development.
Meanwhile try Bloodridge
Offline cylab

JGO Knight


Medals: 30



« Reply #2 - Posted 2011-02-28 18:35:26 »

Recommending the use of scripting over reflection, because of performance considerations, is a bit questionable (at least Wink)

What you want is an URLClassloader and loadClass()

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline Eli Delventhal
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


Game Engineer


« Reply #3 - Posted 2011-02-28 18: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
Offline Roquen

JGO Ninja


Medals: 66



« Reply #4 - Posted 2011-02-28 19:01:01 »

Yeah scripting is the better way to go.  Otherwise think "ClassLoader".
Offline aazimon

Senior Member


Medals: 5



« Reply #5 - Posted 2011-02-28 19: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.
Offline Eli Delventhal
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


Game Engineer


« Reply #6 - Posted 2011-02-28 19: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
Offline aazimon

Senior Member


Medals: 5



« Reply #7 - Posted 2011-02-28 20: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?
Offline Eli Delventhal
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


Game Engineer


« Reply #8 - Posted 2011-02-28 22: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.php

I 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
Offline deepthought
« Reply #9 - Posted 2011-03-01 00: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.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline aazimon

Senior Member


Medals: 5



« Reply #10 - Posted 2011-03-01 01: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.
Offline deepthought
« Reply #11 - Posted 2011-03-01 14:26:11 »

so i would call getgenericinterfaces() on the class i loaded and check that it implements my interface?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline aazimon

Senior Member


Medals: 5



« Reply #12 - Posted 2011-03-01 17: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.
Offline deepthought
« Reply #13 - Posted 2011-03-02 00:27:08 »

 ::)derp! why did i not remember that?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (116 views)
2013-05-17 21:29:12

alaslipknot (123 views)
2013-05-16 21:24:48

gouessej (152 views)
2013-05-16 00:53:38

gouessej (145 views)
2013-05-16 00:17:58

theagentd (159 views)
2013-05-15 15:01:13

theagentd (144 views)
2013-05-15 15:00:54

StreetDoggy (188 views)
2013-05-14 15:56:26

kutucuk (212 views)
2013-05-12 17:10:36

kutucuk (211 views)
2013-05-12 15:36:09

UnluckyDevil (215 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.117 seconds with 20 queries.