How do I add variable to escape sequence?

When I try to add a number to a string like this

local number = 1
print(`\\{number}`)

It is just printing 1

But when I do this

print("\1")

It prints unicode character 0001

How do I make the first one do the same like the second one with a variable involved?

1 Like

You can usestring.char to convert a number to its corresponding Unicode character.

2 Likes

Yeah but it is too slow

Don’t think that’s possible, you have to use string.char like @VegetationBush said.

You can cache the results if performance is a concern.

1 Like

are you genuinely serious right now

unless you’re timing something and it needs to be on point then there is no “this is too slow” in lua

3 Likes

That isn’t what they meant, every single operation you do takes some amount of resources, and calling a function hundreds of times in a frame can add up, certain operations are so intensive that even running it a few times a second can bring down the performance of your game.

OP most likely won’t run into performance issues using string.char, but the results can be cached in a table if efficiency is a high priority.

2 Likes

It seems like it’s very performance critical. There may be a better solution if you stated your use case!

I’m trying to make somebody else’s base64 module faster