Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 load class  (Read 1610 times)
0 Members and 1 Guest are viewing this topic.
Offline dan3008

Junior Newbie





« Posted 2013-05-26 22:36:08 »

Ok, first off, I'm new here, so sorry if this is in the wrong place Sad if it is, let me know, and i'll re-post it in the right place


now that's out the way, down to business.

I've just started out on a voxel-engine for making my future 3D games, so i'm not reinventing the wheel (or block as it were), every time i make a game (since most my games have been block based, or as i've come to lovingly call them, 3D tile based games, anyway).

What i really want to do, is allow for other people to add "blocks" into my game. so, I've decided that the best way to do that, is to dynamically load class's that match a standard template. And using a Map to hold them, so they can be "called"/identified by name. I'll also be using a loader, that reduces the risk of conflict, by renaming files that have the same name.

So, what i need to work out is how to dynamically load a class, and then be able to call its methods.

Heres an example of what all the class's WILL (moders permitting) will look like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
public class block{
        /*there may or may not be variables stored here, for example, my TNT will have a count down variable stored here when its done*/
   
   public block(float x, float y, float z){
   /*used to set all parameters for the block, will only be passed the x,y,z values. by default, this is the bottom left front corner of the cube*/
   }

        public void interact(){
   /*this is what happens when the player interacts with the block.*/
   }
   
   public void jump(){
   /*this is what happens when the player is stood on top of the block (will probably remain empty)*/
   }
   
   public void bump(){
   /*if the player walks into the side of the block*/
   }
   
   public void update(){
   /*called every game loop, to make any changes, such as movement, or checking for [i]some[/i] collisions*/
   }
   
   public void render(){
   /*called EVERY loop, otherwise the block vanishes*/
   }


any hints on how to load and call these class's would be much appreciated.

if its relevant, all class's will be .class files.

Thanks in advance

DAN
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #1 - Posted 2013-05-26 22:40:40 »

Im not sure if this is of any help but maybe make a super / sub class thing here. Super class containing everything all blocks have in common (grass, stone, water) and the sub class of specific things (water will have different properties than stone will). You just call the methods of those classes based off of user input.

EDIT: I read your post again now and I'm not sure if I understood what your asking for...

Offline seifpic

Junior Newbie





« Reply #2 - Posted 2013-05-26 22:49:55 »

I think this should give you a start: http://stackoverflow.com/questions/8546467/jar-plugins-implementation
You could use the Bukkit api as inspiration. Or maybe load the class files from a file using the URLClassLoader.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Corvo

Senior Newbie


Projects: 1



« Reply #3 - Posted 2013-05-26 23:04:08 »

You also could define the blocks in a text file in which you define the attributes of each block.
This file could look like this:

-block
name = grass;
texture = grass.png;
solid = true;
...

Then you just parse the attributes from the file. If you want now a grass Block you create a Block class/object and load the grass attributes in the object.

Or you could create objects which are derived from Block dynamically from their full class name.
This worked for me: http://www.rizzoweb.com/java/dynamicInstantiation.html
Online Agro
« Reply #4 - Posted 2013-05-26 23:05:06 »

http://www.java-gaming.org/topics/writing-a-java-plugin-system/29067/view.html

Offline Danny02
« Reply #5 - Posted 2013-05-26 23:19:30 »

I don't know if it was already metioned, but take a look at the standard ServiceLoader
Offline dan3008

Junior Newbie





« Reply #6 - Posted 2013-05-27 11:32:30 »

Thanks everyone for the quick (and supportive) replies Smiley

I'll have a look through all my options and pick the one that i think will work best for me

THANKYOU Cheesy

dan
Offline dan3008

Junior Newbie





« Reply #7 - Posted 2013-05-29 22:21:12 »

Ok, I'm faced with a slight difficulty now.

I've changed my mind a bit, and have all my class's in the main jar, but still want to call them dynamically. any ideas as to how?
Offline Danny02
« Reply #8 - Posted 2013-05-29 23:51:04 »

explain a bit more what you want to achieve
Offline Sammidysam
« Reply #9 - Posted 2013-05-30 00:42:09 »

Isn't this just using reflection?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Zhon

Junior Member


Medals: 4



« Reply #10 - Posted 2013-05-30 02:25:39 »

Offtopic, your class should start with an Upperclase letter.  Pointing
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #11 - Posted 2013-05-30 03:01:44 »

Why the heck are player collision with block code in the block? Even as a simple implementation it would be put into the player class, but overall it would be better to put it in your collision manager.
Offline dan3008

Junior Newbie





« Reply #12 - Posted 2013-05-31 15:04:02 »

Why the heck are player collision with block code in the block? Even as a simple implementation it would be put into the player class, but overall it would be better to put it in your collision manager.
I see your point. Collision detection, and stopping the player walking through the block is handled in the player class, its just block specific collision stuff, like pressure plates, and trip wires, where its handled in the block. In most blocks, this will be left blank, its just some special blocks that will be different. Similarly, to make blocks that aren't solid, a custom declaration and render would be needed. When i've finished, and got some new blocks type (and dynamite lol), i'll have to show you, then it'll all make sense (I hope)

Isn't this just using reflection?
It is indeed, I was just getting myself confused. Sorry about that one lol.

Offtopic, your class should start with an Upperclase letter.  Pointing
Indeed it should. I've borrowed and edited a friends class, and just havent got round to renaming it yet lol. I'll sort it before i finish.
Offline Sammidysam
« Reply #13 - Posted 2013-06-16 05:32:45 »

If you are still looking to do this, today I used reflection to do something similar and it worked out fantastically.  This is what I did with reflection.  Using this mini-library, I was able to get a list of classes in the same package as the class.  From there I sorted through their simple names and got rid of some classes that I didn't need.  Then, I looped through the good classes and checked some things before returning one or throwing an error if none met certain criteria.  In the end, for my FakeJavaOS console I could input commands and the matcher would dynamically load a class corresponding to the command.  Sorry for the lack of comments; if you need some I can put them in tomorrow.  Feel free to compile the application using the application's build.xml if you want to see it in action.  I think it would work great for what you are looking for.  It is basically what you are trying to do, after all.
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.

pw (24 views)
2014-07-24 01:59:36

Riven (24 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!