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.

Code :

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.

July 27th, 2012, 05:27 PM

Norm

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?

July 27th, 2012, 11:32 PM

Gravity Games

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.

July 28th, 2012, 12:17 AM

pbrockway2

Re: Help with an object based level format

Quote:

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.

July 28th, 2012, 01:46 AM

Gravity Games

Re: Help with an object based level format

Quote:

Originally Posted by pbrockway2

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.

July 28th, 2012, 01:59 AM

pbrockway2

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:

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

Code :

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.

July 28th, 2012, 12:49 PM

Gravity Games

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?

July 28th, 2012, 12:52 PM

elamre

Re: Help with an object based level format

By adding an extra number:

Code :

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.

July 28th, 2012, 04:27 PM

Gravity Games

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.

July 28th, 2012, 04:41 PM

jps

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:

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:

Code :

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.

July 28th, 2012, 04:42 PM

Norm

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.

July 28th, 2012, 07:04 PM

Gravity Games

Re: Help with an object based level format

Quote:

Originally Posted by jps

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:

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.

July 28th, 2012, 07:45 PM

Gravity Games

Re: Help with an object based level format

Quote:

Originally Posted by Norm

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.

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?

July 28th, 2012, 08:01 PM

elamre

Re: Help with an object based level format

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

July 28th, 2012, 08:19 PM

Gravity Games

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).

July 28th, 2012, 08:28 PM

Norm

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.

July 28th, 2012, 08:32 PM

Gravity Games

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)

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:

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

July 28th, 2012, 10:21 PM

Norm

Re: Help with an object based level format

Quote:

Am I reading from these arrays properly?

What happens when you compile and execute the code? Does it work and do what you want?

July 28th, 2012, 10:24 PM

Gravity Games

Re: Help with an object based level format

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

Quote:

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:

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 (" ")