Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

After update can't load files in build.

Discussion in 'Scripting' started by Bellwar, Aug 31, 2018.

  1. Bellwar

    Bellwar

    Joined:
    Aug 15, 2018
    Posts:
    8
    Where I am. I have an XML file with different weapons. I want to load this file from assets and I used Application.dataPath + "/MyFolder/Weapons.xml" in FileStream. It worked in an editor and after in a build too. Now after a while, I build it again and run it. It didn't load and logs say it looks for the file in data... Why it isn't working anymore.

    I tried to find the problem but everything looks like I should use Resources and then there is written to not use them and work around that. What should I use then. And mostly why it worked and now it doesn't... Need help to find a point from where I can go. I am really confused if I should rewrite it to resources or there is a mistake on my side in code and can still use this.

    EDIT: Ok, I fixed it by using streamingAssets. But that doesn't hepl since the whole thing is exposed in the data file as XML. I read Resources is ok but there is something when you want to update the game.
     
    Last edited: Aug 31, 2018
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    Most likely you have changed something. Either in code, or XML file. Or you relocated else where.

    You most likely should replace XML with JSON. See JsonUtility. Plenty topics on that too.
     
  3. Bellwar

    Bellwar

    Joined:
    Aug 15, 2018
    Posts:
    8
    Is JSON that better? I can't find good documentation on JSON. I am not good at programming, just learning as a hobby and XML looks it's not as good but it's ok. But as I see I should look into JSON as I see now. Thanks for the opinion.

    And I really don't know. I changed a lot it just... never changed the part with the files. Secondly, I read Unity want build in text based files, but how can it be before it did. I am really confused about that part.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    So first of all, get familiar with JsonUtility APi.

    https://docs.unity3d.com/ScriptReference/JsonUtility.html

    There are free methods, but you really are interested with
    FromJson
    and
    ToJson

    This utility is very nice and smooth tool. Basically you have your class with stored data. You can turn it into string and then whatever you want with string. I.e. save to file, or send to server.
    Then retrieve json string and turning it back to data.
    Is parsing data, in similar concept as XML. But is cleaner solution.

    Search forum for JsonUtility. You will find tones of topics. Pick a choose.
     
  5. Bellwar

    Bellwar

    Joined:
    Aug 15, 2018
    Posts:
    8
    Ok, now I start to feel annoying, I just.... If I would want to save player data as JSON, I would need a server right? Storing data as JSON inside players computer they can modify it easily right? Same for my items, the only thing is Resources are compiled by Unity so I don't have to be worried.
    Do I understand it right?
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    No.
    I mean you can make it to save on server, but as well as store on local machines.
    Either is beyond JsonUtility scope itself. So is really up to you, what you want to do with json string. just like xml string, before saving anywhere. JsonUtility only converts data type to string, or from string (with json format) back to its data type.

    Wondering, what made you think, is only server based?

    Mostly yes. Unless you make it harder somehow. But that beyond this scope.
     
  7. Bellwar

    Bellwar

    Joined:
    Aug 15, 2018
    Posts:
    8
    No, what I mean is. Let's say I want to save players HP. One way is putting it into JSON and sending to a server. Right? But when I want to save the HP in players computer, putting it into JSON would make it readable easily. So. For save should I just make a class with all data (like HP) and just format that into a file using binary formatter thats in Unity?

    And I still don't know where to save the Items. I feel like I shouldn't be using resources folder since Unity wrote it in their own manual, people should awoid that. But other then getting server and make the game get it from it, I see no way to store the items in JSON.
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,559
    You can create StreamingAssets folder, which allows you to save files, in the game related directory as files.

    However, I got impression, you are worry about players hacking save files? Are you making online game? If so, you should store all critical data on authoritative server. Otherwise, if storing anything on player PC, specially when is single player, there is no point encrypting save files. There is always someone, which will break encryption.

    Yes you need such class. Formatted is optional. Is up to you, how you handle data.

    Similarly as save files. You can store them as per created class, then save to json and save in StreamingAssets folder. If you want less accessible to players (but not impossible), then have the data hard coded. Or get items critical data from server alternatively.
     
  9. Bellwar

    Bellwar

    Joined:
    Aug 15, 2018
    Posts:
    8
    Ok, thanks that makes sense. I know about the online part and I'm just making singleplayer. I just feel like when you put something into txt/JSON it's so much easier to change numbers, where formatting, even when it will probably take 10 minutes to google how to open the file, is more discouraging.

    Anyway, thank you SO SO SO much for all the info. It helped me so much.
     
    Antypodish likes this.