Search Unity

[SOLVED] Passing a String, and trying to use it as part of a reference to a ScriptableObject

Discussion in 'Scripting' started by JustStopAndThink, Oct 4, 2019.

  1. JustStopAndThink

    JustStopAndThink

    Joined:
    Dec 15, 2014
    Posts:
    18
    I apologize in advance for my formatting errors.

    I saw Ryan Hipple's talk on ScriptableObjects.
    I got excited.
    I downloaded Daniel Everland's Event system, which was based on those talks.
    My excitement grew.

    Daniel's GameEventListeners can pass all sorts of nifty things to your scripts, including Strings.

    My game has a bunch of Characters. When they "hear a bell" (rather, when a GameEvent is raised), they all have their NavMeshAgent set a new destination.

    My great hope was to have a human-readable setup.
    When the GameEvent (Bell1) was raised, each Character's GameEventListener would hear.
    This worked just fine.
    The GameEventListener would then send a String to another script that was on the Character.
    This also worked just fine.
    The plan was to have the String be the name of the Room that would be the new Destination of the Character's NavMeshAgent.

    This is where the wheels fell off.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AI;
    3.  
    4. public class GetGoing : MonoBehaviour
    5. {
    6. public NavMeshAgent agent;
    7.  
    8. //This is a custom ScriptableObject type, and it works fine.  It's just a bunch of GameObjects. If you properly reference one, you get their Transform.positions and everything else you could want.
    9. public RoomList roomlist;
    10.  
    11. //This is a variable I made for an unsuccessful secondary attempt.
    12. //private GameObject thisroom;
    13.  
    14. private void Awake()
    15. {
    16. //There is a script called "SchedulePopulator" that makes it very easy for all the prefabs to see and reference the RoomList.
    17.     roomlist = GetComponent<SchedulePopulator>().roomlist;
    18. }
    19.  
    20. //This is invoked when the GameEventListener hears the Bell1 event, and successfully passes a string.
    21.  
    22.     public void GoToYourRoom(string SomeRoom)
    23.     {
    24.        //This next line proved to me that the string was being passed correctly, as "Kitchen" showed nicely in the console:
    25.         Debug.Log(SomeRoom);
    26.  
    27. //This next line proved to me that the references would work, since I got the proper coordinates to show in the console:
    28.         Debug.Log("The kitchen is at: " + roomlist.Kitchen.transform.position);
    29.  
    30. //This next line is where things went very, very wrong:
    31.         agent.SetDestination(roomlist.SomeRoom.transform.position);
    32.  
    33. //I also unsuccessfully tried:
    34. //        thisroom = roomlist.SomeRoom;
    35. //        agent.SetDestination(thisroom.transform.position);
    36.  
    37. //To prove to myself that it should work, I tried this next line, and it definitely worked:
    38. //        agent.SetDestination(roomlist.Kitchen.transform.position);
    39.  
    40.             }
    41. }
    It seems like I should be able to pass the String and get the right reference.
    But I have no idea how.

    Clearly, the important part is to replace "SomeRoom" with the actual content of that String (in this case, "Kitchen").

    I spent a few hours looking at how "Reflection" could be used, but I was never able to see how Reflection would be able to put the contents of the String into a usable format as a "plain text" that would be then used as part of a variable reference.

    I have now spent an entire night reading Microsoft's documentation on the String class, and I do not feel like I am any closer to a solution.

    Is what I am trying even possible?
     
    Last edited: Oct 4, 2019
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You could use a dictionary. Have all the rooms add themselves to the dictionary and then find them with that.

    You would have something like this;
    Code (CSharp):
    1. public Dictionary <string,RoomClass> Rooms;
    Have the Room add itself;

    Code (CSharp):
    1. Rooms.Add(RoomClass.Name,RoomClass);
    Then simply use the string key to find the room

    Code (CSharp):
    1. var room = Rooms[<string>];
    2.  
    3. if(room == null)
    4.     return;
    5.  
    6. //Do something with the roomclass
    or you can search through your list;

    Code (CSharp):
    1. var room = roomList.Find(room => room.Name == <string>);
     
    JustStopAndThink likes this.
  3. JustStopAndThink

    JustStopAndThink

    Joined:
    Dec 15, 2014
    Posts:
    18
    I fear I may be running afoul of the community rules by saying "Thank you!" without much other content, but I am willing to risk it because of your helpfulness.

    Thank you so much for replying quickly and thoroughly!
     
    Kurt-Dekker likes this.