Fog_of_wars not launch

Godot Version

`4.3

Question

0 error / scene work i can play but code fog of war is not activate

hello today more complicated problem for me being new to code
I have a Characterbody2D in a scene for settings and I just need to import the Character2D into the game scene I want,
in my dungeon scene I have a
none2D /tilemap/character2D and a 2D sprite (fog of wars)
I have no errors I saved every path every frame the game launches but no area is revealed when I walk in the black area

fog of war code

extends Sprite2D

@export var fog_texture: Texture2D
@export var reveal_radius: int = 100
@export var grid_size: int = 16

var fog_image: Image
var fog_texture_instance: ImageTexture
var reveal_mask: Image

func _ready():
	# Initialisation de la texture du brouillard
	if fog_texture:
		fog_image = fog_texture.get_image().duplicate()  # Duplique l'image pour modification
		fog_image.convert(Image.FORMAT_RGBA8)  # Assurez-vous que le format est correct
		fog_texture_instance = ImageTexture.create_from_image(fog_image)
		texture = fog_texture_instance
		print("Fog initialized with size: ", fog_image.get_size())
	else:
		push_error("Fog texture is not assigned in the inspector!")

	# Chargez la texture de révélation (cercle blanc)
	var reveal_mask_path = "res://reveal_map/reveal_map.png"
	if FileAccess.file_exists(reveal_mask_path):
		reveal_mask = load(reveal_mask_path).get_image()
		reveal_mask.convert(Image.FORMAT_RGBA8)  # Assurez-vous qu'il correspond au brouillard
		print("Reveal mask loaded successfully with size: ", reveal_mask.get_size())
	else:
		push_error("Reveal mask not found at path: " + reveal_mask_path)


func reveal_at_position(position: Vector2):
	if fog_image and reveal_mask:
		# Convertir la position du joueur en position dans le brouillard
		var grid_position = position / float(grid_size)  # Division flottante
		var reveal_rect = Rect2(Vector2.ZERO, reveal_mask.get_size())
		var fog_rect = Rect2(grid_position - Vector2(reveal_mask.get_width() / 2, reveal_mask.get_height() / 2), reveal_mask.get_size())

		# Appliquer le masque de révélation sur le brouillard
		fog_image.blit_rect(reveal_mask, reveal_rect, fog_rect.position)

		# Mettre Ă  jour la texture du brouillard
		fog_texture_instance = ImageTexture.create_from_image(fog_image)
		texture = fog_texture_instance
	else:
		push_error("Fog image or reveal mask is not initialized!")

code characterbody2D

extends CharacterBody2D

@export var max_health: int = 100
var current_health: int = max_health
const MAX_HEALTH = 5
var health: int = MAX_HEALTH
@export var speed_run: float = 80.0
@export var speed: float = 35.0

@onready var fog: Sprite2D = null
@onready var animation: AnimationPlayer = $AnimationPlayer

func _ready() -> void:
	# Initialisation de l'UI
	update_health_ui()
	set_health_label()
	$healthbar.max_value = MAX_HEALTH
	set_health_bar()

	# Recherche du fog
	var fog_path = "Node2D/fog"  # Adaptez ce chemin selon votre hiérarchie
	fog = get_tree().get_root().get_node(fog_path)
	if fog == null:
		push_error("Fog instance is null! Check the node path: " + fog_path)
	else:
		print("Fog instance loaded successfully.")

func update_health_ui():
	set_health_bar()

func set_health_label():
	$healthlabel.text = "Health: %s" % health

func set_health_bar():
	$healthbar.value = health

func _input(event: InputEvent) -> void:
	if event.is_action_pressed("damage"):
		damage()

func damage():
	health -= 1
	if health < 0:
		health = MAX_HEALTH
	set_health_label()
	set_health_bar()
	update_health_ui()

func _physics_process(delta: float) -> void:
	handle_input()
	move_and_slide()
	update_animation()

	# Révéler autour du joueur
	if fog:
		fog.reveal_at_position(global_position)

func handle_input() -> void:
	var move_direction = Input.get_vector("left", "right", "up", "down")
	velocity = move_direction * speed

# Gestion des animations
func update_animation() -> void:
	if velocity.length() == 0:
		animation.stop()
	else:
		var direction = "down"
		if velocity.x < 0:
			direction = "left"
		elif velocity.x > 0:
			direction = "right"
		elif velocity.y < 0:
			direction = "up"

		animation.play("walk" + direction)

fF.png)

thank you ^^^

Please update your post to use preformatted text for code snippets with ```
Otherwise this is unreadable.

1 Like