How to save a player's position (2D) with a button (Not child or Parent)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Progamer27

Recently I learned how to code a save and load function to save a player’s position with the key O and load it when the key P was pressed. Since I am making a mobile game, I need to use a button to save the player’s position, and then load it when the game starts. Here is the code I’m using to save/load with the keys:

func save_to_file():
var file = File.new()
file.open(Player_file, File.WRITE)
file.store_var(position)
file.close()

func load_from_file():
var file = File.new()
if file.file_exists(Player_file):
	file.open(Player_file, File.READ)
	position = file.get_var()
	file.close()

func _input(event):
if event is InputEventKey and event.pressed:
	if event.scancode == KEY_O:
		save_to_file()
		print("saved")
if event is InputEventKey and event.pressed:
	if event.scancode == KEY_P:
		load_from_file()
		print("loaded")

Is there a way to use these functions and put the save function’s script in the button and use the load function in the player?

:bust_in_silhouette: Reply From: Asthmar

Use the button signals on_pressed() . And because the button is not a parent of the tree just connect the signal to the the parent scrip, or whatever script you have these functions in.

If your button is instead not part of the tree, then you are probably going to have to make your save script a global so the functions in the script can be accessed from anywhere.

example :
func _on_Button_pressed(): YourGlobalSaveScriptName.save_to_file()

More on singletons (globals) : https://godot-es-docs.readthedocs.io/en/latest/getting_started/step_by_step/singletons_autoload.html

Hope that helps!

Well, that was helpful, but didn’t really help this situation. I’m trying to pull the position of the player from a button. Under the main.tscn, The Button.tscn and Player.tscn are present. The player is not connected in any way to the button and the button is not connected in any way to the player.

Progamer27 | 2020-05-01 22:17

You want a button that saves and loads? Or a button that stores your player position into a var when its pressed? Just making sure I understand what you mean.

Asthmar | 2020-05-02 04:38

A button that stores the player’s position when pressed. The load from file will stay in the player’s script so it can be pulled when the game starts.

Progamer27 | 2020-05-02 14:42

Ok, to make a button store the player’s position you can try something like this

First create a global script and a var for the player position. Or you can just put the var into your parent’s script.

Var PlayerPos = Vector2()

Connect the button pressed signal to the player.

And inside the player script you can do something like this

func _on_Button_pressed():
GlobalScript.PlayerPositionVar = Position

Or If your var is in the parent script

func _on_Button_pressed():
Get_parent().PlayerPos = Vector2

Or if your player is the parent

func _on_Button_pressed():
           PlayerPos = position
            print(PlayerPos)

And lastly in your player script

func _ready():
  load_from_file() 

Does that work?

Asthmar | 2020-05-02 19:28

You can’t connect the on Button pressed function to a kinematicbody2D. Here, the main scene looks like this:

↓ Main
Player
Button

Main is the parent. The player and button are the children. Sorry, I was trying to specify this before, and it didn’t come out right. Should I make the Button the parent under main?

Progamer27 | 2020-05-02 21:41

Thanks for showing the tree . But that’s quite odd that you cannot connect your button pressed signal to your Kinematicbody as I am able to without any issues. Making the button the parent may cause issues but you can try it, I would suggest that you try and get your button signal working with your player though, as that would be a better solution.

Asthmar | 2020-05-02 21:59

Why would I need to connect the button pressed signal to the player if I’m not clicking the player?

Progamer27 | 2020-05-03 13:55

The reason we are connecting the button signal to the player is so when the button is pressed it sends a signal to the player node letting it know that the button was pressed and that the player should now store its current position

Asthmar | 2020-05-04 03:23

Got it. But I can’t connect the button signal to the player. Any other options?

Progamer27 | 2020-05-05 18:38