Ignoring unused arguments passed to `String.format`?

Godot Version

4.5.1.stable

Question

I’m working on some text logging for a turn-based combat system, and in my ideal setup, for each of my skills, I produce a choice_<skillname> string that I translate, then format with some additional context, including the name of the user and the name of the target. Then, some amount of outcome_<hit/miss/heal/absorb/etc.> log strings are produced, that are also passed some arbitrary context details.

This is roughly how I’m doing this:

var context = skill.metadata.merged({
    "user": user.name,
    "target": target.name,
    "skill": skill.name,
})

var log_string = String.format(tr("choice_%s" % skill.id), context)

The problem with this approach is that any unused arguments within the format call results in a raised error. It’s a bit frustrating I can’t disable these errors, as they cause things like unnecessary unit test failures.

Am I taking the wrong approach?

You can’t use String.format() like that because it’s not a static method. The correct way would be:

var log_string = tr("choice_%s" % skill.id).format(context)
2 Likes

Right, I wrote the example code wrong. My mistake!

And as I’m trying to reproduce the error I had, I now can’t find it. Maybe I really was just using the function wrong.

Thank you for the response :slightly_smiling_face:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.