Change script at Runtime

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

A player can choose his opponent(Player2) to be either a computer or human, both computer and human have their seperate script, the computer script was been assigned to the opponent(Player2) as the default. If the player decides to face a human, the human script is been attached. Here is my code to change to human script:

if !is_computer:
	player2.set_script(load(player_script))

I tracked my problem to some object not been initialized. Here is a screenshot, when game is running:

Before changing to human script.
Before Script Changed

After changing to human script.
After Script Changed

Note: Both computer and human script inherits(extends) from the Actor.gd(base class), since they share similar nodes and functionality.

Question: What are the likely causes and how to fix them. Also are signals disconnected when the script is been detached(when you use the set script to change script).

A script doesn’t have any data associated with it; that’s what (custom) Resources are for: Resources — Godot Engine (stable) documentation in English

spaceyjase | 2023-05-23 11:02