How to change player movement script in new level?

Hello, I’m making a game, where one level is a platformer, and the next is like the Google T-rex game. The problem is that for both games individually, the movement script for the player is different. How can I switch the script once the player has finished the first level?

You can try using the set_script() method, but I suggest just making an input handler code and give it an update() method to be called every frame by the player. Create two types of this input handler script, one for platformer and one for the Google T-rex game. Then alternate between them as you like.

Also, I think you can simplify things further by just overriding the horizontal movement of the player when it transitions to a runner platformer mode. You can do:

if game_mode == "trex":
    velocity.x = trex_speed
else:
    # input control code for platformer

The jump controls are pretty much the same, ig.

In a similar scenario I use different input controllers for different control mechanisms.

I have a variable that contains the reference to the input handler like this:

var input_controller: InputBaseControllerClass

And then load in the appropriate subclass that extends InputBaseControllerClass:

	if DataStore.control_method == "directional":
		input_controller = DirectionalController.new(Player)
	else:
		input_controller = RotationalController.new(Player)

The controllers (one is tank controls and the other is the more classic ‘up means up’ whereas with tank controls ‘up’ means thrust in the direction you are facing) are like this:

class_name DirectionalController
extends InputBaseControllerClass

(Am planning to add a JoypadControllerClass too later)

So you could have a similar solution for your movement contoller. Create a class like MovementControllerBaseClass, then extend that with a PlatformerMovement class and another RunnerMovement class.

Then you can switch from one to the other just by loading it in. You do not have to remove the previously loaded class, it will unload automatically when it is no longer referenced.

That could work for your situation pretty well I think.

1 Like