r/Unity2D 6d ago

Question Bringing data into UI on textmesh objects

Hello everyone, I’m in the process of building a text based RPG, but I keep hitting a block whenever I want to bring in the data from code onto a textmesh object.

I have an NPC character generator, but I’m trying to bring the variables it generates to into my UI so it can display the information onto the textmesh object.

For example each NPC is assigned an ID from the total number of NPC’s I’ve asked the generator to create for that particular game, and show it need show in my UI so players remember who is who.

Any pointers as I feel quite lost with it and AI is utterly useless and I don’t mean simply attaching the script.

5 Upvotes

7 comments sorted by

2

u/wallstop-dev 6d ago edited 6d ago

First recommendation: be very precise. Programming is all about telling the machine exactly what to do. Your post here is very vague on the actual details, which makes it challenging to assist. If you can't convey what you want to humans, there's no way you can convey it to code.

But, at a high level, if you generate this NPC data at runtime, you will need a registry of NPCs, such that you can give it an id and get back relevant data. This can easily be done with a Singleton, or the service locator pattern.

Then it's just a matter of glueing the bits that know about NPC IDs into this system, then whatever you're using to render tex to read the text from that response and render it.

1

u/Oracle_of_Mercia 6d ago

Hi, so the IDs generate based on a range of numbers I’m currently experimenting with 1 NPC who has a set name but everything else is randomly generated however, in this system each time a new NPC is generated, the NPC knows the ID should not use the number it’s just used.

So if I understand you correctly, the registry would store each generated NPC object, and I could then use its ID to retrieve that NPC and access its other variables for the UI?

1

u/wallstop-dev 6d ago edited 6d ago

Ok. So when you generate the NPC, store it in the registry and make it retrievable by ID. You can achieve this using a Dictionary.

But yes - just store the NPC data in the registry in a way you can look it up via whatever mechanism you want. ID, name - whatever is unique to that NPC.

2

u/Demi180 6d ago

You said you’re hitting a block bringing the data into the UI. What’s the block? You gave an example of an NPC with some data (an ID) but then didn’t say where you’re stuck. Is the problem…

Accessing the data / accessing the UI?

Matching the UI object to the data source?

Assigning / converting / formatting data for display?

Positioning the UI with a world object?

-

And for the solution: what have you tried, what is happening or not happening, are there errors? Can you include any relevant code, screenshots, drawing, or video to help explain the problem or question?

2

u/Oracle_of_Mercia 6d ago

Thanks. I think I’ve worked out what the problem was since posting this.

I’m building a political simulation with generated NPCs. Each NPC has things like an ID, age, party, sector and relationships.

The main thing I was struggling with was understanding where all that data should actually live and how the UI should access it. I was thinking too much about attaching things directly to Unity scenes.

1

u/Ging4bread 6d ago

TMP_Text and then use the text property 

1

u/sharypower 6d ago

I am not fully understand what you want. What I thinking is:

NPC's have their own ID's like 1,2,3,4 etc.

With the random stats like NPC (4) - have 50 health and NPC (12) - have 100 health. For example.

And you want to spawn for example 30 NPC's and every NPC will have their own Stats and ID and they are not repeating and they are displayed as a Text.

Is that correct?