Problem with formatting strings in file path

Godot Version

4.7 (stable)

Question

I have tried to format my file path like a string, to have an export var where i can set the next level if the next level button is pressed, but it does not work and I get this error:

unsupported format character in operator '%'.

Her is my script:

@export var next_level = 2


func _on_next_texture_button_pressed() -> void:
	get_tree().change_scene_to_file("res://scenes/levels/level_ %i .tscn" % next_level)
	print ("next")

I read the docs about string formatting, but i was kinda confused (because I am not the best coder).
Wdit: next gets printed.

You have a space in there. Lets say next_level was 1. That would produce:
res://scenes/levels/level_ 1 .tscn

I think you meant to do:

res://scenes/levels/level_%i.tscn" % next_level

I must admit I have never used %i, I presume this is for an integer. I would use %s so it would be:

get_tree().change_scene_to_file("res://scenes/levels/level_%s.tscn" % next_level)

Still dose not work, i get the same error.

How would you format this?

What is the error you get?

unsupported format character in operator ‘%’

There is not i placeholder character for string formatting. You probably meant to write %d which will be a decimal.

Oh it needs to be in an array perhaps? Although I thought one variable alone didn’t have to be.

var file_name:String = “res://scenes/levels/level_%s.tscn” % [next_level]

Oh… i thought “I” stand for int and “s” for string.

Thank you @pauldrewett and @tibaverus for helping to solve this. :heart: