Search Unity

Unity unable to store large amount of string data.

Discussion in 'Scripting' started by Musafir264, Aug 24, 2019.

  1. Musafir264

    Musafir264

    Joined:
    Mar 30, 2019
    Posts:
    2
    Hi, I'm trying to create a text based game. I'm using a ScriptableObject to store my quest and it's dialogues.
    Code (CSharp):
    1. [CreateAssetMenu(fileName="New Questline",menuName="Questline")]
    2. public class Questline : ScriptableObject
    3. {
    4.     public Quest[] quest = new Quest[]
    5.     {
    6.         new Quest("START OF QUESTLINE","","","",0,0,0,0,0,0),
    7.         new Quest("\tIt has been one month since I moved to Paris. The initial excitement has worn down in quick time. Paris- the so called city of love was just one big blank city to me. My routine included ringing of the alarm at 8 then 8.05 then 8.10. till 9:00 when finally I wake up. After that I rush to Grand Boulevard to take metro ticket and rush to office. Office is equally appalling with same conventional cubical, a photocopier machine, same old colleagues- who gives robotic smile when I look to them.\n\tMy routine also involves eating same old spice less food in the office canteen where I do get the chance to stare at few of the gorgeous Italian girls. However, these Pink moments do not last long and contrast of life again turns black and white with the computer in my cubical and glass wall beside it.","","","",0,0,0,0,0,0),
    8.         new Quest("\tThe other day while I was sitting in my cubical and looking outside the glass window, I saw a girl working on the computer in the next building. She had big black eyes and beautiful dimple smiles. She was wearing black and white business formals. I cant help but to stare at her for a while. She looked back.","I started looking here and there.","I kept looking at her.","",3,0,0,0,0,0),
    9.         new Quest("\tI started looking here and there pretending this never happened. While she looked away, I started looking her with the corner of my eyes. Of course she noticed it. That was the last time I saw her. Apparently, she shifted her desk away from the window because I must have made her uncomfortable. Life goes on, I didn't think much about it.","","","",0,0,0,0,0,0),
    10.         new Quest("","","","",0,0,0,0,0,0),
    11.         new Quest("I kept looking at her. She blushed and then showed me a chart size paper with words written as \"<i>Take a Photo of mine and keep it with yourself. It would be more convenient.</i>\" That was embarassing and I started looking down. A few minutes later, she put another sign with a beautiful smile on her face that said \"<i>I am kidding! My name is Suzan.</i>\" I quickly wrote down \"<i> "+PlayerData.Instance.playerName+" </i>\" on an A4 size paper. She replied \"<i>Nice to meet you.</i>\" I was definitely sure that pleasure was all mine but decided to be subtle and said \"<i>Nice to see u 2!</i>\"","","","",0,0,0,0,9,0),
    12.         new Quest("","","","",0,0,0,0,0,0),
    13.         new Quest("\tI went to her office building. I wanted to meet her but I knew I wasn't ready for that yet.","","","",0,0,0,0,0,0),
    14.         new Quest("","","","",0,0,0,0,0,0),
    15.         new Quest("\tThe days after that were not same. Someway 8:00 A.M alarm means 7:55 A.M. It was like Elvis Presley singing at the background while travelling towards and in the metro. I use to sit at the cubical and talk to her with brand new mode of communication learnt. \n\tI played tic tac toe, shared silly whatsapp emoticons, played dumb charade using the big square piece of papers and markers. I asked her mobile number once but she didn't use one. I was confused but brought thick, fully packed A4 printing paper pack demonstrating that it wont be a problem. Time started passing by beautifully.","","","",0,0,0,0,0,0),
    16.         new Quest("One day I decided to ask her out for date. I wrote the word down into the paper and practiced how to show it to her several times in mirror. I wanted to be perfect in the act. I was ready to show the paper with words written on it as","\"<i>Are you free tomorrow?</i>\"","\"<i>Would you like to go on a date?</i>\"","",11,12,0,0,0,0),
    17.         new Quest("\t\"<i>Are you free tomorrow?</i>\"\n\t\"<i>I don't know, why?</i>\"\n\t\"<i>I would like to ask a beautiful girl on a date. I met her last week.</i>\"\n\t\"<i>And who would that be?</i>\" she asked as she blushed. \"<i>YOU</i>\" I replied with the biggest letters I've have ever written.\n\tShe put up another letter that said, \"<i>I have a secret.</i>\" I asked her what it was. She said that she was mute. The paper fell of my hand into the floor. I was shattered. I didnt look at her and left the office. I bought whiskey and headed straight to home. I did not go to office for next 3 days. The next Monday when I reached office, I did not find her.","","","",0,0,0,13,0,0),
    18.         new Quest("\t\"<i>Would you like to go on a date?</i>\"\n\tShe smiled. That pretty smile made her more beautiful than ever. \n\tShe put up another letter that said, \"<i>I have a secret.</i>\" I asked her what it was. She said that she was mute. The paper fell of my hand into the floor. I was shattered. I didnt look at her and left the office. I bought whiskey and headed straight to home. I did not go to office for next 3 days. The next Monday when I reached office, I did not find her.","","","",0,0,0,0,0,0),
    19.         new Quest("\tI remembered she used to go home with Ava. Ava was my schoolmate back in Everington. I knew her once but then we fell out of touch I guess. The only way to know what happened to Suzan was to ask her. I decided to pay her a visit at her home.","","","",0,0,0,0,0,0),
    20.         new Quest("","","","",0,0,0,0,0,0)
    21.     };
    22. }
    Yes, I'm initializing the data in the code itself and then creating a scriptable object. Whenever I want to create another quest I just replace this code with new data and export to scriptable object again. Both scriptable objects hold their own separate data. I do this because I've created a custom parser that converts CSV file to this exact C# code.

    So, he above code worked well when I used some placeholder data. When I tried to put a sample story, the scriptable objects were being made of zero size and I got the following error in console (I could still run the game):

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Questline..ctor () (at Assets/Questline.cs:8)
    With a little bit of fiddling, I figured out that unity is unable to create the object array instance because of the large data (more than 4 elements of the above data). In another experiment, I noticed that the scriptable object was being fully created when I kept only the first sentence of each element. How do I fix this? Thanks in advance!
     

    Attached Files:

  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Does it have to be SO? Can you not simply use Json for storing data/story etc.?
     
  3. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,066
    I tried to reproduce your error but couldn't. I used a string with 1 million characters and used it for 10 elements, which resulted in an 11MB asset and a pretty unresponsive editor, but it worked. I tried on Unity 2018.4 and 2019.2, both with the new and old scripting backends.

    I suspect your issue is unrelated to string length.

    One possible issue could be that PlayerData.Instance is null when you try to create the scriptable object, which would lead to a NullReferenceException.
     
    Last edited: Aug 24, 2019
    Musafir264 and Yoreki like this.
  4. Musafir264

    Musafir264

    Joined:
    Mar 30, 2019
    Posts:
    2
    You're correct. PlayerData is a Singleton class which is instantiated when the game starts. The error line number confused me. In this case, it's not possible to set player name in the scriptable object beforehand. So I included a "%name%" in the string instead of concatenating a variable and used string.Replace method to show player name while displaying. Thanks a lot for the help!