Jump to content

[SOLVED] Node movement from text file


photo

Recommended Posts

I have a set of pre-recorded data that I want to use to move a node.  The data is space deliminated with the following format for each line:

time  xpos  ypos  zpos  xrot  yrot  zrot

inside the Unigine script flush() function here is my code:

   File shipData = new File("myPositionAndRotationData.txt","r"); //
    
    int shipSize = shipData.getSize();  //i used this to make sure that the file was loaded
    int filePosition = shipData.tell();
    
    string shipLine = shipData.readLine();
    log.message("the file position is " + filePosition + " the data at this position is " + shipLine + " \n");
    shipData.seekCur(1);

It correctly prints the first line of the data but never moves to the next line, instead it continually prints the same line.  I thought that I could increment to the next line in the file using the seekCur() function but haven't had any luck (i've tried different integers etc).  How do I update it so that I can read the next line.

Link to comment

Hello Robert!

When you reopen the file, the cursor moves to the beginning of the file. There are two ways to avoid this:

1. if the file is not large - read all at initialization.

int init() {
	File shipData = new File("file.txt","r");
	while (!shipData.eof())
	{
		string shipLine = shipData.readLine();
		//save data in any container
		log.message("read: %s \n", shipLine );
	}
  	shipData.close();
}


2. open the file and do not close it.

File shipData;

int init() {
	shipData = new File("file.txt","r");
	return 1;
}

int flush() {
	// Write here code to be called before updating each physics frame: control physics in your application and put non-rendering calculations.
	// The engine calls flush() with the fixed rate (60 times per second by default) regardless of the FPS value.
	// WARNING: do not create, delete or change transformations of nodes here, because rendering is already in progress.
  
	//if end of file - set cursor to start of file
	if (shipData.eof()) shipData.seekSet(0);
	
	string shipLine = shipData.readLine();
	//save data in any container
	log.message("read: %s \n", shipLine );
	return 1;
}

int shutdown() {
	shipData.close()
	return 1;
}

 

Link to comment
  • silent changed the title to [SOLVED] Node movement from text file
×
×
  • Create New...