LWJGL Forum

Programming => General Java Game Development => Topic started by: TechNick6425 on August 09, 2013, 11:03:32

Title: Custom tags on functions, what the?
Post by: TechNick6425 on August 09, 2013, 11:03:32
I want to create a game development kit for Java, similar to XNA. I need to have a function that loads stuff from the hard drive. I want it to look a little bit like this.

Model3D model = Content.Load<Model3D>("On/The/Hard/Drive");

I want the only thing that gets used in the load functions is Objects that extend a content class. Is it possible to do this?
Title: Re: Custom tags on functions, what the?
Post by: Fool Running on August 09, 2013, 12:22:24
Yes, but you should really just try it instead of asking. :P
Title: Re: Custom tags on functions, what the?
Post by: TechNick6425 on August 12, 2013, 11:22:10
I did try. I got frusterated. So, I asked here.
Title: Re: Custom tags on functions, what the?
Post by: quew8 on August 12, 2013, 15:12:35
You can either ask a more specific question (at the moment you are asking "can you write this for me") or you can read this.
http://docs.oracle.com/javase/tutorial/index.html (http://docs.oracle.com/javase/tutorial/index.html). Presumably you are familiar with c# or some other .NET language since you're familiar with xna, so you should breeze through the tutorials.
Title: Re: Custom tags on functions, what the?
Post by: TechNick6425 on August 12, 2013, 15:45:53
I'm sorry, I didn't realize how I was wording it! I was trying to make it, but I wasn't sure how I could get the brackets to work.
Title: Re: Custom tags on functions, what the?
Post by: quew8 on August 12, 2013, 16:50:51
The topic I think you need to read specifically is generics, but this is a vast topic and no one could sum it up in a single post.
Title: Re: Custom tags on functions, what the?
Post by: TechNick6425 on August 13, 2013, 00:37:57
I've read that section, but what I want to do is add generics to FUNCTIONS, not objects.
Title: Re: Custom tags on functions, what the?
Post by: TechNick6425 on August 13, 2013, 00:59:39
I'm also wondering if I could return the loaded content, somethine like public static E load;
Title: Re: Custom tags on functions, what the?
Post by: quew8 on August 13, 2013, 09:32:35
http://docs.oracle.com/javase/tutorial/extra/generics/methods.html (http://docs.oracle.com/javase/tutorial/extra/generics/methods.html) maybe you haven't read it as completely as you think. Yes you can return a generic type as well. It can be problematic to create an instance of a generic type at runtime due to type errasure but it is by no means impossible.

Here's an example of some very basic generic functions that concatenate a 2 dimensional array, or several 1 dimensional arrays into 1 single 1 dimensional array.


/**
     *
     * @param <E>
     * @param arrays
     * @param totalLength
     * @return
     */
    public static <E> E[] concatArrays(E[][] arrays, int totalLength) {
        E[] result = Arrays.copyOf(arrays[0], totalLength);
        for(int i = 1, pos = arrays[0].length; i < arrays.length; i++) {
            System.arraycopy(arrays[i], 0, result, pos, arrays[i].length);
            pos += arrays[i].length;
        }
        return result;
    }
   
    /**
     * Assumes all arrays are the same size.
     * @param <E>
     * @param arrays
     * @return
     */
    public static <E> E[] concatArrays(E[][] arrays) {
        return concatArrays(arrays, arrays.length * arrays[0].length);
    }
   
    /**
     *
     * @param <E>
     * @param arrays
     * @param lengths
     * @return
     */
    public static <E> E[] concatArrays(E[][] arrays, int[] lengths) {
        return concatArrays(arrays, sumArray(lengths));
    }
   
    /**
     *
     * @param <E>
     * @param arrays
     * @return
     */
    public static <E> E[] concatVariableLengthArrays(E[][] arrays) {
        return concatArrays(arrays, arrayOfLengths(arrays));
    }


Simple but effective.
Title: Re: Custom tags on functions, what the?
Post by: TechNick6425 on August 13, 2013, 11:05:22
Thanks!