# help for earthquake simulation

hi guys
i’m working for a earthquake simulation now,but there is a problem that input data of the earthquake wave, i got a list that is real-time data,which shows velocity per 0.02s.
ex. 0 12.5m/s
0.02 16.4m/s
0.04 3m/s

now i want to insert the data to blender object that make it to move , i write a cycle loop
import GameLogic as gl

cont = gl.getCurrentController()
own = cont.getOwner()f=open(‘eqwave.txt’,‘r’)
for i in f1
v=[i,0,0]
own.setVelocity(v,0)

ok,the problem is here, the object’s velocity is last data of the list but not get each data per 0.02s and change its velocity.

so how can i do what i want that make the object move base time.

A script is executed once within a logic tick. Setting the valocity multiple times within one script run makes no sense. Only the last set will remain. In your case the last entry of your file.

What you need is to set one velocity of your file. The next script run can set the next valocity.

There are multiple solutions for that.
Here is one:

Add a integer property “keyframe” to the object with the script. Initialize with 0.

if “keyframe” == 0 : open the file and store the filehandler in an property of the object e.g. “file”
end if

if “keyframe” >=0:
set the velocity with the value from that line

If you reached the end of the file close it and set “keyframe” to -1 to make sure no further reads on the file a processed.
end if

I hope it helps

thanks a lot
but i don’t know how to set keyFrame with python,could you post the python script?and you said there are multiple solutions ,so i think could you tell me the others ，and it will be great.

set a property:

own[“keyframe”] = own[“keyframe”] + 1

another solution would be to read the whole file in one step (usually at the 1st frame) and store the values in a list.
Then you can read through the list. That might give a small performance benefit.

thanks for help
and i wrote the script as you said , but it didn’t run.The script is fellowing , is that right?

``````import GameLogic as g
import Blender
cont=g.getCurrentController()
own=cont.owner
if keyFrame==0:
file=open('c:\eqwave.txt','r')
i=0
if keyFrame&gt;=0:
line=f[i]
p=float(line)
v=[p,0,0]
vel=setLinearVelocity(v,0)
keyFrame=keyFrame+1
i+=1
if i&gt;len(f):
keyFrame=-1
``````

Do not import Blender. It is not available to runtime.

change to following:

``````
import GameLogic
own=GameLogic.getCurrentController().owner

# If the file data is not already read - do it now
if not "fileData" in own:
file=open('c:\eqwave.txt','r')
# read file and store all data in property fileData
own["keyFrame"] = 0

#Store the properties in a local variable (this avoids multiple dictionary access later)
fileData = own["fileData"]
keyFrame = own["keyFrame"]

# if there are remaining data process the next record
if keyFrame&gt;=0 and keyFrame&lt;len(fileData):
line=fileData[i]
p=float(line)
v=[p,0,0]
vel=setLinearVelocity(v,0)
# this frame is done - increase the stored property
own["keyFrame"] = keyFrame + 1
``````

It is untested. I hope it helps.

thanks a lot
i tried this script , but it did not run , it showed the error message like that
TypeError: argument of type ‘KX_GameObject’ is not iterable

so i deleted ‘‘if not “fileData” in own:’’
althought the script can run , it also got the last data only to insert setLinearVelocity() ,and the keyframe was not be created.