Saving

From Robowiki
Jump to navigation Jump to search

How

Static Variables

The easiest way to save data between rounds is to make your variables static. There is no need to save data to a file and retrieve it. You usually store the enemy information you collected in previous rounds (if you need it later) or information stored in files that you need to read only once.

Note: Multiple instances of the same bot on the field do not result them in sharing static variables, even though they are of the same class. This is because each robot is created by separate ClassLoaders, so that they do not share static variables.

Serialization

Serializable means that a class can be converted into an array of bytes which can then be written to disk, sent over a network, etc. For a class to be Serializable it must:

  • have no member variables which hold non-serializable classes, and
  • implement the Serializable interface.

The only real problem you can run into is if you have an object with a lot of references to other objects: when you serialize it, you'll also be serializing and all of those objects, any objects that they have references to, etc. You can use the transient keyword to indicate that a member variables aren't part of the persistant object, but remember to manually initialize transient variables after initialization.

See Also: Compressed Serialization

Location of data directory

If it's from a .jar file you put in the robots dir, it will be in something like Robocode/.robotcache/jarname/package/botname.data. If it's a dev version you're working on, it will be in robots/package/botname.data.

Other

If you have your data in simple, primitive type arrays you can store and retrieve that data as shown in: Writing Arrays To File

If you are having problems saving data, look here: SecurityException Bug

What

This depends heavily on what aiming methods and movement system you are using.

Size

Reduce unnecessary data by writing only the fields you absolutely need Serializing Enemy objects works, but if you find yourself running out of space, you can do better. You don't need to store things like last position, heading, velocity, etc. which are probably in your enemy class.

Also, Object Streams consume a lot of space; use Data Streams instead. Take a look inside a file written using ObjectOutputStream. If I remember correctly, each field is identified by its name and type, as well as the field's value. The signatures of the object's methods might also be written to the file. On the other hand, these files should compress very well with Zip Streams. See: Compressed Serialization

If you still need to reduce the file size, try cramming larger fields into smaller ones where possible (ie: double into int). Sure, you will lose some precision, but if file size is a real issue for you, you have to be willing to make some sacrifices.