how to control things in a scene component


version 3.5.2 topic extern properties in a component

Imagine you are doing an enemy component that looks like this:

Troll:Node2D
_ AnimatedSprite:contains an iddle animation of 1 frame for example
__ TrollCollider:KinematicBody2D
___ CollisionShape2D:Rectangle2D

You attach a script to the Troll of Node2D and add this code:

tool
extends Node2D

# externally change the horizontal flip
export var flip_h = false setget setFlipHorizontal, getFlipHorizontal

# set the horizontal flip on the AnimatedSprite
func setFlipHorizontal(_flip_h):
	$AnimatedSprite.flip_h = _flip_h

# return the flip horizontal statAnimatedSprite
func getFlipHorizontal():
	return $AnimatedSprite.flip_h

With this script everytime you insert the EnemyTroll scene node as component into a level scene you select it and see at the right that you can toggle on/off the flip horizontal variable which affects the direction on which the troll looks forward.

You can use this to setup the game properly.