How to treat a variable as a string

Discuss authoring naninovel scenario scripts.
Post Reply
TT2
Posts: 4
Joined: 18 Jul 2024 07:31

How to treat a variable as a string

Post by TT2 »

Hello,
I have a question.

script

Code: Select all

1: @input name summary:"Choose your name."
2: @stop
3: 
4: Archibald: Greetings, {name}!

For example, in the above script, if the user inputs "001", the expected result is "Greetings, 001!", but what is actually displayed is "Greetings, 1!".

When I use the var command to check the content of a custom variable, the content of the "name" variable is "001", so I think the {name} part is probably interpreting "001" as a number and displaying it as "1".
How can I get it to display "Greetings, 001!" as expected?

Elringus
admin
Posts: 538
Joined: 11 May 2020 18:03

Re: How to treat a variable as a string

Post by Elringus »

Hey, Thanks for letting us know of the issue!

This is now solved in v1.20, which you can download on our Discord after registering your license.

We've also added type parameter to @input command, allowing to enforce the input value type. For example, you can make sure player would only be able to input a number.

Code: Select all

; Guess the number game.
@set number=random(1,100);answer=0
@while answer!=number
    @input answer type:numeric summary:"Guess a number between 1 and 100"
    @stop
    @if answer<number
        Wrong, too low.
    @else if:answer>number
        Wrong, too high.
    @else
        Correct!
TT2
Posts: 4
Joined: 18 Jul 2024 07:31

Re: How to treat a variable as a string

Post by TT2 »

Hi Elringus,

Thank you for your reply.
I immediately registered and downloaded the latest version.

However, since I have already done a full range of testing, it would be risky to upgrade everything.
Is it possible to address this issue by replacing only specific source code included in the package?

Elringus
admin
Posts: 538
Joined: 11 May 2020 18:03

Re: How to treat a variable as a string

Post by Elringus »

The change to make it work was quite deep (we had to introduce strict typing to custom variables).

If you can't upgrade, I'd suggest instead making a custom UI to input a value and a custom state to store it.

Here's the guide on creating a custom UI: https://naninovel.com/guide/user-interf ... -custom-ui

Here's the guide on utilizing custom state: https://naninovel.com/guide/state-manag ... stom-state

TT2
Posts: 4
Joined: 18 Jul 2024 07:31

Re: How to treat a variable as a string

Post by TT2 »

If the value stored in the variable was "1" instead of "001", I was thinking of doing something about the Input in Custom UI.
But since the variable was stored as "001", I thought there was no point in changing the Input...

I can't imagine how to fix it, is there any example I can use?

Elringus
admin
Posts: 538
Joined: 11 May 2020 18:03

Re: How to treat a variable as a string

Post by Elringus »

It's not a bug per se, so there is nothing to fix. Prior to 1.20 custom variables didn't have type associated with them; the type was dynamically evaluated on access, so in your case 001 is interpreted as a number (which it actually is). To override that behavior and enforce a specific type on a variable you have to either override the custom variable manager and all the associated behaviors (which is what we've done in v1.20) or just roll up your own system for that particular mechanic with input. The latter is much easier, hence I suggested it. If you'd like to inject that variable to generic lines or command parameters, you may also find custom functions useful: https://naninovel.com/guide/script-expr ... -functions All the articles I've linked have associated examples with them, which you can use as references.

TT2
Posts: 4
Joined: 18 Jul 2024 07:31

Re: How to treat a variable as a string

Post by TT2 »

I see.
Thank you so much!

Post Reply