Save object coordinates

i am trying to save object coordinates and if the object is hit by a near sensor or not i would like to save it in a txt file or database to do statistical analysis on it is it doable ?

To get the position using the near sensor, use the “hitObjectList” attribute for get all the objects hitted(return a list of KX_GameObject), and use the worldPosition of it.

And to save it in a txt file use somenthing like this:

import bge
cont = bge.logic.getCurrentController()

near = cont.sensors["Near"]

path = bge.logic.expandPath("//")

name = "file.txt"
file=open(path + name,'w')

for objects in near.hitObjectList:
    position = objects.worldPosition
    text = "The position of the object " + objects.name + " is: " + "x = {0}, y = {1}, z = {2}".format(position.x, position.y, position.z)
    file.write(text + "
")

file.close()

PD: try this file Save in a file the positions of the objects hitted by a near sensor.blend (89 KB)

this looks great but i wanna it to save every new object that my character hit like in a bubble game where the bubbles appear random and the target is to hit each bubble and save the coordinate of the ones i hit

what if i want to save position and property of random created objects ?

It’s still basically the same code.

What exactly are you trying to achieve, in a general sense?

i wanna it to save every new object that my character hit like in a bubble game where the bubbles appear random and the target is to hit each bubble and save the coordinate of the ones i hit