Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 42

Thread: Help with an object based level format

  1. #1
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default [SOLVED] Help with an object based level format

    So I had to scrap my old tile based level format for an object based one so I could use arraylists. The first thing I need to figure out is how to execute Java code written in a text file. The reason I need this is because each object will be added to the arraylist one by one from the txt.

    Example of what I want to execute as my test(in case it helps)
     
    Level1.add(new GroundTileATop(10,10)
    Level1.add(new GroundTileATop(40,10)

    Sorry if this is a n00bish question, but I'm still sort of a beginner in Java, and I've never done anything like this before.
    Last edited by Gravity Games; August 3rd, 2012 at 10:43 AM. Reason: Solved
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with an object based level format

    To execute java code, you need to compile it.
    For java code to compile it must be in a class and a method for most cases.

    A problem would be for the variables referenced in the code, they would be isolated from the variables in the program that is compiling and executing the java statements. You'd have to pass references to the local variables to the method in the code being created.

    Can you explain what you are trying to do and why you think Java is the right language to do it in?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Help with an object based level format

    Well, I'm trying to write a list of objects in an external file to an arraylist, then place each object on the JPanel in order. I've got a tutorial I'm following for the actual placing that should work, but the problem is that the part of the tutorial that writes the list of objects to the arraylist is hardcoded. I absolutely can not hardcode much of anything, because 1) I want my engine to be as easily edited as possible, and 2) I may make other games based off of the same engine, so the less that's hardcoded, the less needs to be changed for a specific game.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with an object based level format

    The first thing I need to figure out is how to execute Java code written in a text file.
    First you can read in Java code and execute it on the fly. That's what libraries like Beanshell let you do. But there's a reasonable amount of work you would have to do to master its usage and your code would still have to take care of all the problems that can arise at runtime if the code you are interpreting is faulty in some way. (Ie you won't have the compiler to help you).

    But I am *really* unconvinced that you need to follow this approach.

    If your intention is to create a bunch of GroundTileATop objects and add them to this Level1 thing (level1 would be a better name) then just do that. Have a text file containing the integer arguments and read that file creating and adding the tiles as you go.

  5. #5
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Help with an object based level format

    Quote Originally Posted by pbrockway2 View Post
    If your intention is to create a bunch of GroundTileATop objects and add them to this Level1 thing (level1 would be a better name) then just do that. Have a text file containing the integer arguments and read that file creating and adding the tiles as you go.
    Well, I'll need more objects, this is just the one I'm coding first. So how would I add the objects like you said? I don't understand the way you put it.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Help with an object based level format

    The example of a text file which you said you wanted to read and execute was the following:

    Level1.add(new GroundTileATop(10,10)
    Level1.add(new GroundTileATop(40,10)

    I am suggesting that (0) you replace this with the following text file:

    10 10
    40 10

    Then (1) read the file line by line and, for each line (2) extract the two integers which represent the position or whatever of the tile.

    Then (3) create an instance of GroundTileATop based on those integers. And (4) add the tile just created to the list (or whatever the Level1 thing is).

    These last two steps - repeated for each line of the file - you suggested as being in the text file itself. What I am suggesting is that they be part of the method that reads that text file.

  7. #7
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Help with an object based level format

    But the problem I see with this is how would I be able to add more tiles than just the same ground tile, like say a water tile or the player?
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  8. #8
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Help with an object based level format

    By adding an extra number:
    1 10 10
    1 40 10
    2 30 10
    where the first int could be the object type, the second the x, and the third the y. You could add as much as you'd like. You could even add health in a fourth row. Possibilities are endless.

  9. #9
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Help with an object based level format

    Alright, so what is the best way to get my scanner to read the file and write the variables to integers? Unless I'm missing something, I'd have to have a hardcoded maximum value of the X and Y position.

    Like the 0th(or 1st) position of the row would be the object (example 1), then the next 4 would be the x position(example 0027), and finally the last 4 would be Y position(example 0001), with the full file reading "100270001". I really wouldn't like to do that, because a hardcoded maximum level size means more limitation, and I'd like the level format to be as non-limiting as possible (which is especially important since I want the game to eventually feature a level editor).

    Please tell me if I'm wrong about this, as I hope I am.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with an object based level format

    I don't understand where you would have to define a maximum x and y.
    if you place your integers in a text file with spaces between them you can easily get the next integer on the line no matter (within acceptable range for an integer) how large it is.
    For example, a sample text file as follows:

    1 2034 19952 214 3003454
    394522 35473 354 36843 3265234
    5 3 2 1 0
    999999 12345 53832 3422 9545
    4 lines with 5 integers each.

  11. #11
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Help with an object based level format

    You could do that, yes. But you also could add a letter in front of every line. Then you would end up with something like this:
    T1 X2300 Y323 
    T4 X2003 Y33
    Or you could use the spaces as end/beginning of your field. and the newline will stand for a new object. I would either do it with spaces, a char in front of the number or use an api for map reading. Slick2d for example has it build in already, but it's all up to you.

    edit, was just a sec too late. Jps beat me to it.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with an object based level format

    The data on each line could be delimited (separated) by spaces instead of in fixed columns to allow for a larger range of numeric values.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Help with an object based level format

    Quote Originally Posted by jps View Post
    I don't understand where you would have to define a maximum x and y.
    if you place your integers in a text file with spaces between them you can easily get the next integer on the line no matter (within acceptable range for an integer) how large it is.
    For example, a sample text file as follows:

    4 lines with 5 integers each.
    The reason I say I would have to set up a maximum X and Y is because of my read file method:
    public void File(){
     
    		try{
    		l = new Scanner(new File("C://CWWLEVELS//world1stage1.txt"));
    		while(l.hasNext()){
    			for(int i=0; i < ((levelsizey)); i++){
    				Level1.add(i,l.next());
    			}
    		}
    		l.close();
    		}catch(Exception e){
    			System.out.println("error loading map");
    		}

    To read each integer I would assume I have to make different for loops to read each one like so:
    public void File(){
     
    		try{
    		l = new Scanner(new File("C://CWWLEVELS//world1stage1.txt"));
    		while(l.hasNext()){
    			for(int i=0; i < ((levelsizey)); i++){
    				Level1.add(i,l.next());
    			}
    for(int i=0; i < 1; i++){  //to read object id
    				Level1.add(i,l.next());
    }
    or(int i=1; i < 5; i++){ //to read x and so on...
    				Level1.add(i,l.next());
    			}
    		l.close();
    		}catch(Exception e){
    			System.out.println("error loading map");
    		}

    Once again, I hope I'm wrong...
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with an object based level format

    If you want to read integer values you should use the nextInt() method. The next() method returns a String.

    You should work on the logic needed to read in the data and use it in the program before trying to write the code. Your nested loops don't make sense to me. What is on each line being read in from the file?
    What is to be done with each piece of data/number that is read in?
    Define what is in each line in the file and then show how that should be read and used in the program.
    Last edited by Norm; July 28th, 2012 at 06:17 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Help with an object based level format

    Quote Originally Posted by Norm View Post
    If you want to read integer values you should use the nextInt() method. The next() method returns a String.

    You should work on the logic needed to read in the data and use it in the program before trying to write the code. Your nested loops don't make sense to me. What is on each line being read in from the file?
    What is to be done with each piece of data/number that is read in?
    Define what is in each line in the file and then show how that should be read and used in the program.
    So I should change my code to something like this?:
    public void File(){
     
    		try{
    		l = new Scanner(new File("C://CWWLEVELS//world1stage1.txt"));
    		while(l.hasNext()){
    			for(int i=0; i < ((levelsizey)); i++){
    				Level1.add(i,l.nextInt());
    			}
    		}
    		l.close();
    		}catch(Exception e){
    			System.out.println("error loading map");
    		}

    Correct, or is there something I'm missing?
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with an object based level format

    Can you describe what is in the file being read and what you want to do with it when you read it into the program?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Help with an object based level format

    What is level1.add(,,) supposed to do?

  18. #18
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Help with an object based level format

    Currently the file has 3 integers per line in it like I was told, with spaces inbetween each. The first one is the item ID, when its a certain number, I want a certain object spawned (The main ground tile), the second one I want to be the X position of the object, and the third, the y position. Also, I haven't actually run the game since I've made these edits, as I want to make sure I correct the file reading method first (I know I'll get an error otherwise).
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with an object based level format

    If a line has three items on it, you need to read the full line and separate it into the three parts so you can process each one as required.
    The String split() method will separate the line into separate parts and put them into an array.
    The Integer class's parseInt method will convert a String to an int value.

    You should write a small test program that reads the file line by line, splits each line into its parts and prints out the parts to see how the methods work. When that works, you can merge the logic from the small test program into the larger program.
    Last edited by Norm; July 28th, 2012 at 07:46 PM. Reason: spelling
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Help with an object based level format

    Okay, I'll modify my small array testing program and see if what you said works. Wish me luck!

    Edit: Having a little trouble, am I reading the different parts of the array properly? (str1 is the array)

    i1 = Integer.parseInt(str1[0]);
    i2=Integer.parseInt(str1[1]);
    i3=Integer.parseInt(str1[2]);
    Last edited by Gravity Games; July 28th, 2012 at 09:05 PM.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  21. #21
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Help with an object based level format

    Sorry for the slight bump, but since the last post doesn't update for edits, I wanted to make sure people know that I've updated my post, hopefully reposting my question will make this post slightly less useless:

    Am I reading from these arrays properly?
    i1 = Integer.parseInt(str1[0]);
    i2=Integer.parseInt(str1[1]);
    i3=Integer.parseInt(str1[2]);

    Thank you for your time.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  22. #22
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Help with an object based level format

    i would not see why that would not work? what's your output? and what were you expecting?

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with an object based level format

    Am I reading from these arrays properly?
    What happens when you compile and execute the code? Does it work and do what you want?
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Help with an object based level format

    When I try to run it, these are the errors that come up in eclipse:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at stringtest.main.Main.readHeader(Main.java:70)
    at stringtest.main.Main.<init>(Main.java:26)
    at stringtest.main.Jframe.main(Jframe.java:15)
    Since the other errors come up at the start of the "read" method, and when the Main object is added to the JFrame, it can't be either of them. If it helps, here's all the code:

    package stringtest.main;
     
     
    import java.awt.*;
    import java.io.File;
    import java.util.Scanner;
     
    import javax.swing.JPanel;
     
     
    public class Main extends JPanel{
     
    	private String str1[] = new String [3];
    	private Scanner lh;
    	private String s1 = "13";
    	private String s2 = "13";
    	private String s3 = "0";
    	private String s4 = "0";
    	private int i1;
    	private int i2;
    	private int i3;
    	private int i4;
     
    public Main(){	
    		openHeader();
    		readHeader();
    		closeHeader();
     
    		i1 = Integer.parseInt(s1);
    		i2=Integer.parseInt(s2);
    		i3=Integer.parseInt(s3);
    		i4=Integer.parseInt(s4);
    	}
     
    	public void paintComponent(Graphics g) {
        	super.paintComponent(g);
     
        	g.setColor(Color.BLACK);
        	g.drawString(s1,10,10);
        	g.drawString(s2,10,30);
        	g.drawString(s3,10,50);
        	g.drawString(s4,10,70);
     
        	if (i1==2 && i2==1 && i3==1){
        		g.drawString(s1,10,90);
            	g.drawString(s2,10,110);
            	g.drawString(s3,10,130);
            	g.drawString(s4,10,150);
        	}
     
    	}
     
     
    public void openHeader(){
     
    		try{
    		lh = new Scanner(new File("C://CWWLEVELS//world1stage1.txt"));
    		}catch(Exception e){
    			System.out.println("error loading configuration file");
    		}
     
    	}
     
    	public void readHeader(){
     
    	while(lh.hasNext()){
    		for(int i=0; i < 1; i++){
    			s1=lh.nextLine();
    			str1=s1.split(s1);
    			i1 = Integer.parseInt(str1[0]);
    			i2=Integer.parseInt(str1[1]);
    			i3=Integer.parseInt(str1[2]);
    		}
    	}
     
    	}
     
    	public void closeHeader(){
     
    	lh.close();
     
    	}
    }
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  25. #25
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Help with an object based level format

    I see,
    That exception means that you are trying to acces an element of an array which doesnt exist. Say you've got an array which exists out of 5 elements(which is the max boundary as well), and you try to get information about a 6th one, then it will give that error, since it's out of its boundaries.
    Also as in what you are doing wrong,
    edit: didnt read your code well, let me read it again, sorry!

    edit 2:
    Try to print out s1. Also you are splitting s1 with s1. Something is not correct here! you want it to split on a space (" ")

Page 1 of 2 12 LastLast

Similar Threads

  1. A level format problem
    By Gravity Games in forum Object Oriented Programming
    Replies: 8
    Last Post: June 4th, 2012, 02:20 PM
  2. [SOLVED] Help Generating a level
    By Montario in forum AWT / Java Swing
    Replies: 22
    Last Post: April 12th, 2012, 07:22 AM
  3. Attempting to get to next level in API
    By meathead in forum The Cafe
    Replies: 8
    Last Post: October 12th, 2011, 11:17 AM
  4. Custom Log level help
    By seanman in forum Java SE APIs
    Replies: 1
    Last Post: September 25th, 2011, 08:55 PM
  5. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM