Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Popolee · Dec 16, 2018 at 07:20 AM · stringreflectiondialogue

How to transform a string into a variable in a dialogue

Hello,

I am making a simple dialogue system, and would like to "dynamise" some of the sentences. For exemple, I have a Sentence

Hey Adventurer {{PlayerName}} ! Welcome in the world !

Now In code I am trying to replace that by the real value of the string in my game. I am doing something like this. But it doesn't work. I do have a string PlayerName in my component where the function is situated

 Regex regex = new Regex("(?<={{)(.*?)(?=}})");
 MatchCollection matches = regex.Matches(sentence);
 
 for(int i = 0; i < matches.Count; i++)
 {
     Debug.Log(matches[i]);
     sentence.Replace("{{"+matches[i]+"}}", this.GetType().GetField(matches[i].ToString()).GetValue(this) as string);
 }
 return sentence;

But this return me an error, even tho the match is correct.

Any idea of a way to do it ?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Dec 16, 2018 at 07:46 AM 0
Share

what error? why are you using reflection?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by rossadamsm · Dec 16, 2018 at 08:03 AM

@Popolee This seems a little bit of a complicated way to replace strings (but I am assuming it makes more sense in the general dialog system you would like to create.)

I don't get any errors when I run your code however I wasn't getting the expected result because of one small oversight. You are not assigning the replaced sentence back into the sentence variable before returning it. That is , you need

 sentence = sentence.Replace("{{" + matches[i] + "}}", this.GetType().GetField(matches[i].ToString()).GetValue(this) as string);

Hope that solves your problem!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Popolee · Dec 16, 2018 at 08:13 AM 0
Share

yes thanks, I notice after posting. Then I must have a problem somewhere else. Will check on it

avatar image
0

Answer by Popolee · Dec 16, 2018 at 08:15 AM

Oh, the variable must be Public... !

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rossadamsm · Dec 16, 2018 at 08:46 AM 0
Share

Yes, if you using reflection it will only be public variables by default. However you can specify the binding flags to find private properties too.

 System.Reflection.BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic; 
             sentence = sentence.Replace("{{" + matches[i] + "}}", this.GetType().GetField(matches[i].ToString(),flags).GetValue(this) as string);


Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Modifying Brackeys' dialogue system for questions/different dialogue "paths"? 1 Answer

Problems with simple dialogue 0 Answers

How to make a text appear gradually? 0 Answers

Game slows down when printing text 1 Answer

Dialogue & GUI Text String 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges