Displaying an array entity in a label.text/button.text

Started by The creature, Mon 12/09/2022 20:43:19

Previous topic - Next topic

The creature

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

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.

Crimson Wizard

#1
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

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.

The creature

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

ActiveCreature creature[6];
export creature;

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


Code: ags

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.

Crimson Wizard

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?


eri0o

@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)

The creature

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

struct ActiveCreature
{
String Name;
};
Import ActiveCreature creature[6];

Crimson Wizard

#6
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.

The creature

#7
Ok it is null when i added this to the repeatedly execute
Code: ags

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
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
creature [0].name="";
still produces the null error.

I've used structs many times and I have never had an error like this....

The creature

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.

SMF spam blocked by CleanTalk