Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: The creature on Mon 12/09/2022 20:43:19

Title: Displaying an array entity in a label.text/button.text
Post by: The creature on Mon 12/09/2022 20:43:19
Hello all

I'm running into a problem that I usually do fine without much thought. I have a struct and I'm trying to display text from it in a label

The error I receive is: formatting argument 1 is expected to be a string, but it is a null pointer.

Interestingly enough if I switch it to display an entry that is a number rather than a string it is fine.

I've displayed text this way before in other projects and have never seen this error before.

My code is:

Code (ags) Select

LStable1.Text=String.Format("%s", readyCreature[0].Name);


When I use other entries from the array I get no issues. Just when I use a text items. And again, I've done it this exact way before in other projects and it's been fine.

I tried modifying the label text using another Struct I made and that displayed correctly...so I have no idea what is going on with that other one...

Total loss.
Title: Re: Displaying an array entity in a label.text/button.text
Post by: Crimson Wizard on Mon 12/09/2022 22:59:22
The error "formatting argument 1 is expected to be a string, but it is a null pointer." you were getting with String.Format is likely because Name was not assigned anything yet, so it equals null. String.Format does not like that.

By the way, in this case it looks like you don't need formatting at all. Both Label.Text and readyCreature[0].Name are strings, so they may be assigned directly.

Code (ags) Select

LStable1.Text = readyCreature[0].Name;


Unless the above code is not a real code, while in real code the label contains more information besides the name.
Title: Re: Displaying an array entity in a label.text/button.text
Post by: The creature on Mon 12/09/2022 23:31:32
Ok, so I'm applying it directly and it's giving me the same error. (Null string supplied to CheckForTranslation).

I get this error regardless of what value I type into the field.

My struct is
Code (ags) Select

ActiveCreature creature[6];
export creature;

Function game_start()
{
creature[0].Name="ffhgsfghh";
}


Code (ags) Select

lStable1.Text = creature[0].Name;


Again, I just can't get my head around this because it's something I've done many times before and never ran into issues.
Title: Re: Displaying an array entity in a label.text/button.text
Post by: Crimson Wizard on Mon 12/09/2022 23:49:03
So, this sounds like a problem of value not saved into the struct.

Are you assigning this to a label in the same script where you set the default value, or these are different scripts?

How do you declare this in the script header (import)?

Do you double check that the game_start function runs and the array members get assigned?

Title: Re: Displaying an array entity in a label.text/button.text
Post by: eri0o on Tue 13/09/2022 01:48:14
@The creature Which AGS version are you using? (I remember  AGS 3.6.0 - Beta 4 had the data disappearing bug, but it was fixed right after)

Also, if the data gets assigned and read in game_start in different scripts, make sure the one that reads it happens after the one that assigns it (like CW said)
Title: Re: Displaying an array entity in a label.text/button.text
Post by: The creature on Tue 13/09/2022 06:17:00
So the struct is being declared in another script above the Global Script and I'm displaying the information in the repeatedly execute of the Global Script.

The header is:
Code (ags) Select

struct ActiveCreature
{
String Name;
};
Import ActiveCreature creature[6];
Title: Re: Displaying an array entity in a label.text/button.text
Post by: Crimson Wizard on Tue 13/09/2022 11:40:07
Quote from: The creature on Tue 13/09/2022 06:17:00
So the struct is being declared in another script above the Global Script and I'm displaying the information in the repeatedly execute of the Global Script.

Can you set the breakpoints in both places to double check that the assignment happens earlier?
In general, if that's a script logic error, this may be fixed by testing whether Name is null before assigning it to the label.

Quote from: eri0o on Tue 13/09/2022 01:48:14
@The creature Which AGS version are you using? (I remember  AGS 3.6.0 - Beta 4 had the data disappearing bug, but it was fixed right after)

Right, there have been a bug in the past, where the Strings were disappearing, but I think that was related to saving and restoring a game.
Title: Re: Displaying an array entity in a label.text/button.text
Post by: The creature on Tue 13/09/2022 19:40:41
Ok it is null when i added this to the repeatedly execute
Code (ags) Select

If (creature[0].name == null)
{
lStable1.Text ="Null";
}


So that is returning true. But if I set some text to the object's name or leave it blank with just ""; I get that error. So how do I exactly set it so it isn't null because I thought that doing something like:
Code (ags) Select
creature [0].Name="DAVE"; would mean it wasn't null. But even with text in the field and a blank field like this:
Code (ags) Select
creature [0].name=""; still produces the null error.

I've used structs many times and I have never had an error like this....
Title: Re: Displaying an array entity in a label.text/button.text
Post by: The creature on Tue 13/09/2022 19:48:36
I'm so so sorry.....words cannot express how sorry I am. My only  excuse is I have been very overworked and have been very tired. So I can only profusely apologize for wasting everyone's time....

I know what the issue is...

My function game_start() was:
function start_game()

I feel very stupid and ashamed. I usually try very hard to research an issue before approaching the forum...

I'm very sorry.