Extremely noobish question: global variables in script [solved]

Started by Lord Vetinari, Mon 20/03/2017 18:24:45

Previous topic - Next topic

Lord Vetinari

Apparently I don't know how to do anything, please send help  :P
The very basic idea is this: there's a player controlled clock in game (I mean that time advances only if the player interacts with anything) and I'm trying to display it in a label on temporary GUI for testing purposes and I ended up immediately stuck.
I created three global variables in the global variables editor, vIntDays, vIntMinutes and vIntHours, created the gui and the label, then put in the repetedly_execute function of the global script this very simple line:
Code: ags

    lbClock.Text = ("Day %d", vIntDays, ", %d", vIntHours, ":%d", vIntMinutes);


The goal is to have it display something like "Day 1, 6:30")
I compile it and it says GlobalScript.asc(66): Error (line 66): Parse error in expr near '"Day %d"'
I dug an old question from the forums, stuff that dates back to more than 10 years ago, and the solution was to convert the int variables into strings before displaying them, so I did that:

Code: ags

    vStrDays = ("%d", vIntDays);
    vStrMinutes = ("%d", vIntMinutes);
    vStrHours = ("%d", vIntHours);
    lbClock.Text = ("Day ", vStrDays, " ", vStrHours, ":", vStrMinutes);


(of course I created the three new string global variables beforehand the same way).

Same results, except that the error now point to the very first line, Error (line 66): Parse error in expr near '"%d"'. So obviously either %d doesn't work (but the forums says it does) or the game doesn't recognize the variables (but I can find them in the autocomplete), or repetedly_execute is not the right place (but where else could I put it to keep the clock updated?) What am I doing wrong? The manual says that if you create a variable in the global variable editor you don't have to declare and/or import it the old way.
I haven't coded the clock, yet (this was the preparatory bit before delving into more complex scripting. I'm off to a great start!), but it shouldn't be an issue because I gave all the int variable the base value 0 in the editor.

dayowlron

Try this. You need to have the format as one single string and it is the first string. also have to call the string format function.

Code: ags

    lbClock.Text = String.Format("Day %d, %d:%d",vIntDays,vIntHours,vIntMinutes);
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

Slasher

And put it in function repeatedly_execute_always()

Lord Vetinari

#3
Thank you! I knew I was missing something.

Regarding putting it in repeatedly_execute_always, I can see it as a safety precaution against my clumsiness (I can easily see myself calling the functions in the wrong order, for example), but is it really necessary? There isn't an actual timer, the function that updates the clock is called only if the player interacts with something. I'm asking to understand better how things work.

EDIT: one quick question while I'm at it: I never thought of that, but of course if the minutes are less than 10 this code displays them as a single digit rather than 6:05, for example. I fixed it rather bluntly by checking if vIntMinutes is less than 10 and using two different messages, one with a 0 added in the string and one without it. It seems a rather blunt solution, is there something more elegant than this?

dayowlron

#4
The issue with the single digit minutes, change the format to %02d:

Code: ags

    lbClock.Text = String.Format("Day %d, %d:%02d",vIntDays,vIntHours,vIntMinutes);

the 02 means it should contain 2 digits and zero fill the number to the left.

And you would put it in repeatedly_execute_always if you want the label to be updated even when there is a blocking command running.
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

Lord Vetinari

Thank you.
Thank you. I knew that about the repeatedly_execute_always, however I don't think it is needed because the clock doesn't update in the background, it updates when you interact, in theory after every other interaction-related code is executed; that's why I said it shouldn't matter, any blocking action should be endend by the time I call the clock function.

Cassiebsg

In that case, it doesn't even need to be in rep_exec. Just call it after each interaction, no need to check it 40 times a sec.  ;)
There are those who believe that life here began out there...

Lord Vetinari

Quote from: Cassiebsg on Tue 21/03/2017 15:16:27
In that case, it doesn't even need to be in rep_exec. Just call it after each interaction, no need to check it 40 times a sec.  ;)

Well, you are absolutely right now that I think about it. Thank you for pointing that out!
For some reason I had this fixation that, being a GUI, it had to be constantly updated.

SMF spam blocked by CleanTalk