0 votes

I have character that moves with keyboard inputs and I need to know how to record the movement and write it to a file. I use the 'func _process(delta)' function so I would like to record the character's translation and rotation from each frame. I don't know if this is helpful, but this is how I have the movement coded:

extends RigidBody

func process(delta):
if(Input.is
keypressed(KEYLEFT)):
translateobjectlocal((Vector3(0,0,delta7)))
if(Input.iskeypressed(KEYRIGHT)):
translate
object_local((Vector3(0,0,-delta
7)))

in Engine by (19 points)

For better readability (and maybe solve the problem quicker), may I suggest formatting the GDscript with spaces. For each of the code snippets, indent them four spaces.

Anyway, you may be able to do that via the mechanism called serialization. You can save the lines in a file like this:

var file = File.new()
file.open("user://movement.txt", File.WRITE)
file.store_line(character_position)
file.close()

With this code, you could probably save the character's position to a file. Having written that, it may not be a great idea to constantly write the character's position to a file, as that could waste CPU cycles.

1 Answer

+1 vote
Best answer

Here

func process(delta):
    var file = File.new()
        if (file.open("user://movement.txt", File.READ_WRITE) == OK):
            file.seek_end()
            file.store_line("\r")
            file.store_line(str(self.translation)+"\n"+str(self.rotation)+"\n")
            file.close()
        else:
            print("Error writing file")
by (271 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.