WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Splitting a string into 3 ints, or getting x,y,z position (https://forum.worldviz.com/showthread.php?t=4734)

williamchappell 08-29-2013 02:36 PM

Splitting a string into 3 ints, or getting x,y,z position
 
Hey, the project I'm currently working on requires me to store the players position in a text file, and then read the data back into the file to make a simulation of exactly what they did.

So far I can write the position in the file, and read it back out, however, when I read it out, I can only retrieve a string like "10,10,10" and need to split this into three floats, but nothing I'm doing seems to work.

Either this, or if I could get the x, y, z position of the player individually, that might be easier.

Thanks, Will.

farshizzo 08-29-2013 03:20 PM

Here's a code sample that shows how to parse the string "10,10,10" into a list of floating point numbers:
Code:

s = "10,10,10"

pos = [ float(x) for x in s.split(',') ]

print pos


williamchappell 08-29-2013 05:11 PM

Thank you so much! You're a life saver.

Frank Verberne 08-30-2013 08:32 AM

In the future, you might want to look at using the cPickle module for storing objects (lists, numbers, dictionaries, pretty much anything) in a text file and retrieving the same objects later on.

Here is an example where the rotation of the mainview of Vizard is stored in a text file (by pressing the d-key) and read from that file (by pressing the r-key):
Code:

import viz
import cPickle

viz.go()

def writeInfo():
        orientation = viz.MainView.getEuler()
        file_rot = open('MainView_rot.txt', 'w+')
        cPickle.dump(orientation, file_rot)
        file_rot.flush()
        file_rot.close()
        print 'Succesfully pickled:', orientation

def readInfo():
        try:
                file_rot = open('MainView_rot.txt', 'r')
                orientation = cPickle.load(file_rot)
                file_rot.flush()
                file_rot.close()
                print 'Succesfully unpickled:', orientation
        except IOError:
                print 'Could not read file!'
       
def onKeyDown(key):
        if key in 'dD':
                writeInfo()
        elif key in 'rR':
                readInfo()
        else:
                pass
       
viz.callback(viz.KEYDOWN_EVENT, onKeyDown)



All times are GMT -7. The time now is 09:52 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC