edit a text in game run time

with python in game run time can we edit a specific line in text.txt? if we we can please share your script. thank you
something like:

if sensor.positive: with open(path + “text.txt”, ‘w’) as savefile:
in line 5 write hello

textObject.text = “Your Text”

Note: you can do this with logic bricks by using the text property and the property actuator.

Since you asked about it in python, here’s a little more “full” version of Nicholas_A’s code:


import bge

scene = bge.logic.getCurrentScene()
textObject = scene.objects['ObjectNameHere']
textObject.text = "Your Text"

sorry i mean text.txt

are you looking for something like this.


import re

def replace(file, pattern, subst):
    # Read contents from file as a single string
    file_handle = open(file, 'r')
    file_string = file_handle.read()
    file_handle.close()

    # Use RE package to allow for replacement (also allowing for (multiline) REGEX)
    file_string = (re.sub(pattern, subst, file_string))

    # Write contents to file.
    # Using mode 'w' truncates the file.
    file_handle = open(file, 'w')
    file_handle.write(file_string)
    file_handle.close()
    

replace("text.txt","hello world","hello stranger")

yes edderkop i’m looking for something like this

in game run time open and edit a specific line in a text.txt, no one else have a script or .blend example?

You were given an example already, but if that’s not enough here you have a tutorial http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python

If you’re writing to a file, you probably want serialization, check how to use json in python. And just as a side note, if you ever plan to install your game on Program Files/, remember that there are no writing permissions there.

This is pure Python. The BGE does not provide any operation to edit text files.

The naive approach is to load the complete file into memory, modify the memory and write the memory back to disk (see post#5).

Editing text in the BGE is rudimentary.