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 /
  • Help Room /
avatar image
1
Question by AaronBacon · Sep 24, 2017 at 10:34 AM · efficiencyregex

Regex Efficiency Question

So, I've made code that creates multi-page Dialogue for an NPC, and it fully works, but I'm pretty new to C# so I'm having a problem with efficiency.

This is the relevant code:

 public class NPCInteractor : MonoBehaviour {
     private bool inRange; // Used to store if you're in range of the NPC or not
     private bool talking; // Used to store if you're talking to an NPc or not
     private int dialouge; // Used to store how far along you are with an NPCs dialouge
     private string speech; // Holds the speech File as Text
     // ⬅️ POINT A
     void Start () {
         dialouge = 0;
         speech = (speechFile.text);
     }
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown("e") || Input.GetKeyDown("joystick button 0")) // When E is pressed
         {
             if (inRange == true) // And you are in range of the NPC
             {
                 if (talking == true) // if You are already talking to the NPC
                 {
                     dialouge++; // Move the dialouge along by One
                     string[] speechLines = Regex.Split(speech, "##"); //⬅️ POINT B
                     dialougeField.text = speechLines[dialouge];
                 }
                 else if (talking == false) // If you aren't already talking to the NPC 
                 {
                     talking = true; // Note you are now talking to the NPC
                     textBox.gameObject.SetActive(true);
                     dialouge = (start);
                     string[] speechLines = Regex.Split(speech, "##"); //⬅️ POINT C
                     dialougeField.text = speechLines[dialouge];
                     nameField.text = npcName;


So, I'm reading the lines from a separate text file, then split the file into a list of lines (Using "##" to separate parts of dialogue), with the NPC being defined in the editor which items in the list are theirs. But the problem is I've no idea how to only have to define the list once at POINT A, since whenever I've tried to use public at POINT B or POINT C it just throws an error at me, the same as if I try to define speechLines with the other variables at POINT A. Although it works, I would obviously prefer if I could make it only have to define the speechLines Variable at the start, Once.

Its probably a pretty basic thing, but it's gonna annoy me if my code has a glaring inefficiency like this,

If it's needed the full code is at https://pastebin.com/x0JcChCh

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

68 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

Related Questions

[ANSWERED] c# performance impact - Global variables vs Local variables 1 Answer

Reading specific line from .txt file on Android build 0 Answers

Should I use a Coroutine function or a Time.deltaTime equation to add to values overtime? 1 Answer

Is there a better way to do this? 0 Answers

Efficient way to use UGui.Overlap for a GUI Mouse 0 Answers


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