Is there a way to export to a file an array of objects?

Hi,

I'm finishing my first "big" program in Processing (its like a simple Transit Simulator in 2D using Cellular Automaton), where all the info is saved in an array of objects (objects with some variables and some other objects inside).

Visually its a Grid with an specific color every cell, depending of what is in that cell (a street, a car, a traffic light). You can edit the grid, adding streets and cars and etc using the keyboard.

My question, like the title say, its that i want to have a way to save the edited Grid to a file, to future use (and not having to write it manually in the code or doing it every time).

I guess that all the problem can be summarized in: there is a way to store an object (or an array of objects) to a file for future use.

Sorry for the bad english, not a native speaker, and thank you in advance for your help.

Answers

  • Would be great to see what your object look like. Could you post some code?

    Kf

  • yeah of course, i guess that with just the classes its ok so here it goes:

    class Calle{
      int Direccion;     // Hacia donde va, 1,2,3,4 = arriba,abajo,izquierda,derecha, 0 para una calle vacia
      String Tipo;         // O=Ortogonal
      int LimiteVelocidad;        //Limite de Velocidad para los autos 
      //Constructor
      Calle(int d,String t,int l){
        Direccion=d;
        Tipo=t;
        LimiteVelocidad=l;
      }
    }
    
    class Semaforo{
      boolean rojo;
      int timer;
      //Constructor
      Semaforo(boolean r,int t){
        rojo=r;
        timer=t;
      }
    }
    
    class Celda{
    boolean hay_auto;
    boolean hay_calle;
    boolean hay_semaforo;
    Auto auto;  
    Calle calle;
    Semaforo semaforo;
    color Color;
    
      //Constructor
      Celda(boolean hay_1,boolean hay_2,boolean hay_3,Auto a, Calle c,Semaforo s,color color_celda){
       hay_auto=hay_1;
       hay_calle=hay_2;
       hay_semaforo=hay_3;
       auto=a;  
       calle=c;
       semaforo=s;
       Color=color_celda;
      } 
    }
    

    sorry for the mix with spanish and english xD i have ro rewrite some parts of the code to make it look clean and pretty xD

    The array is a Celda[][] type. (Celda[][] Grid;)

  • processing has a built in xml library that'll let you read and write xml

    https://processing.org/reference/XML.html

    similarly for json

    https://processing.org/reference/saveJSONArray_.html

    the above are both portable and human readable.

  • (there are binary formats you can use - java can serialise / deserialise java arbitrary objects to/from files. but i'd recommend the xml / json route for ease of debugging)

Sign In or Register to comment.