different rooms are essentially different scenes but since I instantiate the player manually every time, it doesn't keep the power up and instead immediately goes into idle mode.
I tried to fix the problem by creating a singleton with a variable (to store the power up to keep) and a signal (to check if the door has been entered).
The way the code works is that the moment the player is colliding with the door's area2d it checks what the current power up is with a match statement and put the power up string inside of the singleton's poweruptokeep variable.
after that inside of the player states I checked for every "important" state (idle, the power up) what the poweruptokeep variable was and changed the state (still with a match case), according to the variable.
This seems almost fine (a little bit convoluted maybe) and it almost worked for me aside from the fact that the player when absorbing the power up, the moment it tries to remove it it comes back so I tried another solution with the signal from the single (doorentered)
Though another problem rises with using the signal, since the signal plays when the door is entered (when it's still in the first scene), the moment the next scene is on and the player is instantiated again it doesn't keep the power up.
I will be attaching the code I used under here, if anybody wants to have a shot at solving this problem,thanks in advance.
THE CODE FOR THE SINGLETON
extends Node
var coins = 0
var playerHealth: int
var PlayerPowerUpToKeep: String
signal DoorEntered
THE CODE FOR THE DOOR
extends Node2D
@ export var NextScene: PackedScene #reddit automatically assumes the tag is for tagging other users so I put a space on here right now (just for the post)
var PlayerInside = false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
`pass # Replace with function body.`
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
`if PlayerInside == true:`
`if Input.is_action_pressed("ui_up"):`
`GameManager.emit_signal("DoorEntered")`
`get_tree().change_scene_to_packed(NextScene)`
func _on_area_2d_body_entered(body: Node2D):
`if body.is_in_group("Player"):`
`PlayerInside = true`
`MatchPowerUp(body)`
Also the current power up is stored inside of the characterbody2d parent script of every state node (im using a little node based state machine from a tutorial)
THE CODE FOR THE PLAYER IN IDLE STATE
extends State
@ export var JumpState: State
@ export var AbsorbState: State
@ export var DamagedState: State
@ export var DuckState: State
@ export var CupidState: State
const DOUBLETAP_DELAY = .25
var doubletap_time = DOUBLETAP_DELAY
var last_keycode = ""
func on_enter():
`player.SPEED= 500`
`player.CurrentPowerUp = "idle"`
`print(GameManager.PlayerPowerUpToKeep)`
`GameManager.connect("DoorEntered", KeepPowerUp)`
#then theres the keep power up function
func KeepPowerUp():
`print("this function Starts")`
`match GameManager.PlayerPowerUpToKeep:`
`"cupid":`
`next_state = CupidState`
`print("yeah I should be cupid technically")`
`"idle":`
`pass`
INSIDE OF THE CUPID NODE (THE POWER UP NODE STATE)
extends State
@ export var IdleState: State
@ export var JumpState: State
@ onready var Arrow = preload("res://POWERUPS/Cupid/cupid_projectile.tscn")
var CanShoot = true
enum attackstates {RESET, NORMALSHOOT, GOINGDOWN, DOWNSHOOT}
var lastattack = attackstates.RESET
func on_enter():
`player.CurrentPowerUp = "cupid"`
`$"../../AnimatedSprite2D".play("CupidIdlde")`
`$"../../AnimatedSprite2D".stop()`
`GameManager.connect("DoorEntered", KeepPowerUp)`
#the keep power up function in the cupid node
func KeepPowerUp():
`print("this function Starts")`
`match GameManager.PlayerPowerUpToKeep:`
`"cupid":`
pass
`"idle":`
next_state = IdleState
(let me know if you think the code I've posted is too vague Ill be glad to share more)