Announcements, support questions, and discussion for the Dialogue System.
OneManOnMars
Posts: 106 Joined: Tue Apr 05, 2016 9:37 am
Post
by OneManOnMars » Thu Jan 18, 2024 5:55 am
Hey hey,
is there a way to round the variables in the dialogue text?
I would like to provide record times here and it would be cool to only show 2 digits after the point.
Currently, it looks like this:
I did try to round the number before setting it like this:
Code: Select all
public void StopTimer()
{
if (isTiming)
{
FinishPaticle.Play();
elapsedTime = Time.time - startTime;
float roundedTime = Mathf.Round(elapsedTime * 100) / 100;
isTiming = false;
// Vergleich mit der Record-Zeit
if (elapsedTime < record || record == 0)
{
Debug.Log(elapsedTime);
DialogueLua.SetVariable(DSRecordVariable, roundedTime);
Thank you very much.
OneManOnMars
Posts: 106 Joined: Tue Apr 05, 2016 9:37 am
Post
by OneManOnMars » Thu Jan 18, 2024 7:10 am
Ok, I did some more digging and strangely the variable seems to be stored right. But when I display it like shown above it is different.
Please look here:
Tony Li
Posts: 22091 Joined: Thu Jul 18, 2013 1:27 pm
Post
by Tony Li » Thu Jan 18, 2024 7:52 am
Hi,
That's floating point numbers for you. Their values can drift slightly. The Unity editor may be doing a bit of rounding when showing the value in the Dialogue Editor's Watches section, which just calls EditorGUILayout.FloatField().
If you're doing math in C#, use Mathf.RoundToInt():
Code: Select all
DialogueLua.SetVariable(DSRecordVariable, Mathf.RoundToInt(elapsedTime));
Then it should show correctly when you use [var=
variable ] in Dialogue Text.
If you're doing it entirely in Lua, there's no rounding function, but you can use math.floor():
Code: Select all
Level 1: [[lua(math.floor(0.5 + Variable["RecordTC00"]))]
OneManOnMars
Posts: 106 Joined: Tue Apr 05, 2016 9:37 am
Post
by OneManOnMars » Thu Jan 18, 2024 10:38 am
Thank you Tony for the fast reply.
Using floor was no real option since I wanted 2 decimal numbers.
However, in the end, I figured that displaying only seconds is not the best option anyway. So, I decided to store two variables. One of them as a string in the correct format.
This looks like this, just in case anyone is interested in it.
Code: Select all
public void StopTimer()
{
if (isTiming)
{
FinishPaticle.Play();
elapsedTime = Time.time - startTime;
float roundedTime = Mathf.RoundToInt(elapsedTime * 100) / 100f;
isTiming = false;
// Compare Time
if (elapsedTime < record || record == 0)
{
Debug.Log(roundedTime);
DialogueLua.SetVariable(DSRecordVariable, roundedTime);
if (timerText != null)
{
NewRecordText.text = FormatTime(roundedTime);
NewRecordUI.SetActive(true);
}
DialogueLua.SetVariable(DSRecordVariable + "_String", FormatTime(roundedTime));
}
else
{
if (timerText != null)
{
timerText.text = FormatTime(roundedTime);
RecordText.text = FormatTime(record);
RunTimerUI.SetActive(true);
}
}
// display time in UI component
}
else
{
Debug.Log("Zeitmessung ist nicht gestartet.");
}
}
// Helper function to format to time format
private string FormatTime(float timeInSeconds)
{
int minutes = Mathf.FloorToInt(timeInSeconds / 60f);
int seconds = Mathf.FloorToInt(timeInSeconds % 60f);
int milliseconds = Mathf.FloorToInt((timeInSeconds * 100f) % 100f);
return string.Format("{0:00}:{1:00}:{2:00}", minutes, seconds, milliseconds);
}
Tony Li
Posts: 22091 Joined: Thu Jul 18, 2013 1:27 pm
Post
by Tony Li » Thu Jan 18, 2024 10:45 am
Thanks for sharing!