I'm using String.Format to try and turn the value of an integer into a readable string but I'm getting an error every time I try to use it.
my code looks like this
//main global script file
int gold;
gold = 300;
export gold;
String goldAsText = String.Format("%d", gold);
export goldAsText;
and i keep getting the error:
Error (Line 5): cannot assign value to global pointer.
If anyone can help me figure out what's happening here, that would be great.
AGS does not support function calls in global variable initializations.
The solution is to initialize your variable inside a function, such as "game_start":
String goldAsText;
<...>
function game_start()
{
goldAsText = String.Format("%d", gold);
}
Thank you so much. Although I did have to put it in repeatedly_excecute() for it to work properly.
I should really have figured that out days ago. I've been banging my head against a brick wall trying to figure it out and the solution was that simple.(wtf)
Don't use repeatedly_execute for that, use a function:
header:import int gold;
import String goldAsText();
main script:int gold = 300; export gold;
String goldAsText() { return String.Format("%d", gold); }
Now you can use // inside some function
lblGold.Text = goldAsText(); // this uses the current value of the gold int
However, may I ask why you need goldAsText? AGS doesn't support adding Strings with +, so if you want to add the amount of gold to a message, you'll have to use String.Format anyway.
I've come straight from using Unity and I've always created a text element for the name of something and another one that I manipulate through code to display something like
|Credits:| |lblCredits|
in the UI.
I'd then use things like creditsString = creditsInt.ToString(); and lblCredits.Text = creditsString;
Even though I can't append a string with an integer value, I can give it its own label which changes throughout the course of the game.
Quote from: Netgibbon on Thu 29/06/2017 21:25:03
I'd then use things like creditsString = creditsInt.ToString(); and lblCredits.Text = creditsString;
Even though I can't append a string with an integer value, I can give it its own label which changes throughout the course of the game.
But why using intermediate string creditsString when you can do just lblCredits.Text = creditsInt.ToString() (or lblGold.Text = String.Format("%d", gold); in the first case)?
Is there any purpose for keeping a prepared string like that in Unity?
It's a habit i picked up at work when i had a job (laugh). There was a work experience kid that turned up one day and he started doing that. It seemed to make more sense and sometimes it made the code easier to read so we all started to do it.
Quote from: Netgibbon on Thu 29/06/2017 23:49:50
It's a habit i picked up at work when i had a job (laugh). There was a work experience kid that turned up one day and he started doing that. It seemed to make more sense and sometimes it made the code easier to read so we all started to do it.
Ah, I see.
I'd probably use function instead, as Khris suggested, but I guess that's a matter of personal preference too.
Not to be rude, but doing that just for the sake of it is total nonsense. With proper code you'll be doing the conversion to a string in one or maybe two places in your code, and you certainly don't need to do it 40 times per second.
There's also weakly-typed languages like JavaScript where it's not necessary at all.