Help Converting Godot v3.1 Script to Godot V4+

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

Hi,

I’m trying to convert this Godot 3.1 script to Godot version 4+. I’m struggling with the new Tweens in Godot 4.

The script is from a Youtube tutorial (Time stamps 3:30 and 5:52):
How to Improve Your Camera for Platformers in Godot 3.1

The script alters the Camera2D node so that there is extra space in front of the player, allowing him to see things further ahead such as enemies.

Here’s the script:

extends Camera2D

const LOOK_AHEAD_FACTOR = 0.2
const SHIFT_TRANS = Tween.TRANS_SINE
const SHIFT_EASE = Tween.EASE_OUT
const SHIFT_DURATION = 1.0

var facing = 0

@onready var prev_camera_pos = get_global_position()
@onready var tween  = $Shift_Tween

func _process(delta):
	_check_facing()
	prev_camera_pos = get_global_position()


func _check_facing():
	var new_facing = sign(get_global_position().x - prev_camera_pos.x)
	if new_facing != 0 && facing != new_facing:
		facing = new_facing
		var target_offset = get_viewport_rect().size.x * LOOK_AHEAD_FACTOR * facing
		
		#position.x = target_offset
		tween.interpolate_property(self, "position:x", position.x, target_offset, SHIFT_DURATION, SHIFT_TRANS, SHIFT_EASE)
		tween.start()

Thanks.

:bust_in_silhouette: Reply From: zeludtnecniv

Just use this to replace tween change in _check_facing : (Not tested by the way)

var tween = get_tree().create_tween()
tween.tween_property(self, "position:x", target_offset,SHIFT_DURATION).set_trans(SHIFT_TRANS).set_ease(SHIFT_EASE)
tween.start()

Godot 4 tweens start automatically. No need to call start().

Enfyna | 2023-06-25 21:58

Hi,

Thanks for the help. However there is no from value other than the default.

From what I have read, you can use an additional from at the end of the statement:

tween.tween_property(self, "position:x", target_offset, SHIFT_DURATION).from(position.x)

The example you gave only has the one value to tween to:

tween.tween_property(self, "position:x", target_offset,SHIFT_DURATION)

I also read you can use the easing with seperate commands:

tween.set_trans(Tween.TRANS_BACK)
tween.set_ease(Tween.EASE_OUT)

Additionally, I read that the new Tweens autostart, unless you tell them otherwise, so I don’t think tween.start() is necassary.

So altogether it should be:

var tween = get_tree().create_tween()

tween.set_trans(Tween.TRANS_BACK)
tween.set_ease(Tween.EASE_OUT)
tween.tween_property(self, "position:x", target_offset, SHIFT_DURATION).from(position.x)

However I get an error:

Cannot call method 'from' on a null value

I got information regarding the from in Tween from a YouTube video:
How to Tween in Godot 4

Full script with the changes:

extends Camera2D

const LOOK_AHEAD_FACTOR = 0.5
const SHIFT_TRANS = Tween.TRANS_SINE
const SHIFT_EASE = Tween.EASE_OUT
const SHIFT_DURATION = 1.0

var facing = 0

@onready var prev_camera_pos = get_global_position()
@onready var tween = get_tree().create_tween()

func _process(delta):

	_check_facing()
	prev_camera_pos = get_global_position()


func _check_facing():
	var new_facing = sign(get_global_position().x - prev_camera_pos.x)
	if new_facing != 0 && facing != new_facing:
		facing = new_facing
		var target_offset = get_viewport_rect().size.x * LOOK_AHEAD_FACTOR * facing
			
		tween.set_trans(Tween.TRANS_BACK)
		tween.set_ease(Tween.EASE_OUT)
		tween.tween_property(self, "position:x", target_offset, SHIFT_DURATION).from(position.x)

Are you able to help further?

JayH | 2023-06-26 16:01