Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 2017 times)
0 Members and 1 Guest are viewing this topic.
Offline deepthought
« Posted 2011-02-28 16: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 17:03:50 »

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

People make games and games make people
Offline cylab

JGO Ninja


Medals: 49



« Reply #2 - Posted 2011-02-28 17: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!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #3 - Posted 2011-02-28 17: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
« Reply #4 - Posted 2011-02-28 18:01:01 »

Yeah scripting is the better way to go.  Otherwise think "ClassLoader".
Offline aazimon
« Reply #5 - Posted 2011-02-28 18: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

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


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

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #8 - Posted 2011-02-28 21: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-02-28 23: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!
Legends of Yore - The Casual Retro Roguelike
Offline aazimon
« Reply #10 - Posted 2011-03-01 00: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 13: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
« Reply #12 - Posted 2011-03-01 16: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-01 23: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.

 

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

The first screenshot will be displayed as a thumbnail.

Dwinin (23 views)
2014-09-12 09:08:26

Norakomi (56 views)
2014-09-10 13:57:51

TehJavaDev (69 views)
2014-09-10 06:39:09

Tekkerue (34 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (25 views)
2014-09-07 01:12:14

Longarmx (31 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (39 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!