|
|
|
|
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