Read C# file as text file in runtime

Long story short, I have a script file where I define a character’s abilities and I want to use that to populate a movesheet where you can read more about them (properties, frame data, etc.).

In other words, I am trying to read a script file as a text file in runtime, but this is having some issues:

I first used StreamReader to read the files, but since the assets are not accessible in the build that does not work.

I then tried Resources.Load (for reference I have already been using this multiple times so I am familiar with the method). Generally reading text files uses the TextAsset object, but reading the forums informed me that .cs files cannot be converted to TextAsset objects.

I also am not aware of making text files out of script files in runtime either.

Does anyone have any solution to this problem?

Script files are Monoscript type assets, which inherits from TextAsset. Though it’s an editor only type. Still you can reference them as a TextAsset and see if that works in a build, though I’m guessing probably not.

You really just need to just translate this data into a form you can use at runtime, such as a scriptable object, or JSON format data in a text file.

1 Like

There is an alternative of just copy and pasting it to a text file and then reading that. This is not a perfect solution and requires some overhead but it technically works because I do not need to update it in runtime. I also already made a huge amount of code reading the text file and I really do not feel like doing that over again.

I mean I would hope that the difficulty in this would help realise that a rethink to your approach is in order. Having all this purely defined in code is not a great idea. Code is ideally not your data.

Having your data expressed as well, data, makes all this a non-issue. Move all these properties, frama data, etc, into an asset you can easily read from; most likely a scriptable object.

Then anything else can easily access said data. Whether that be the gameplay or UI systems.

An approach that requires the least refractoring would be to allow your code to express said data required for the UI to present it. Because to be frank, trying to read a script file to do this is a very bad approach.

1 Like

That’s not the correct way to look at the problem.

What actually happened was you tried one solution and found that it was lacking in some fundamental ways.

That’s okay. This is something I do every single day when I am coding.

Forcing yourself to go ahead with a whacky read-and-parse solution just because you spent so much time on it is only dooming yourself to more unnecessary work and hassle going forward.

Instead, learn from the mistake and refactor.

Ideally code does NOT contain data, certainly not “content” data.

The only purpose of code is to transform data, not BE the data. Put the data in an appropriate container and write code to modify the data as necessary.

ScriptableObjects or JSON are fantastic ways to store content data, as Spiney points out above.

2 Likes

I just want to clarify something fundamental here. Yes, script files are considered Assets inside the Unity editor. They are TextAssets / MonoScripts but only exist inside the editor. All your scripts are compiled into one or multiple binary assembies which are actually shipped with the game. None of your script code is shipped with your game as text. That’s why any attempt to manually read script files makes no sense.

As it was mentioned a couple of times, JSON is a pure data format that can be parsed in various ways. It’s also possible to use that data to change the behaviour of your code. This is generally called data-driven design. So you don’t need to change the code when you want to change certain aspects.

While it is possible to use hardcoded data in code, there would never by any reason to manually parse through the source code. When you define / create certain classes in code, you would have those classes / instances available and can use them. Your question talks about a quite abstract “way” to store data in your code. What exactly have you done? Can you actually share an example? The more concrete your question is, the more concrete we can be in our answers.

3 Likes

^ ^ ^ This… and if you have already PUT a bunch of data in your code, now is your chance to write just a few more lines of code to load that data into structured objects of some kind and write them out so that you can move to separate code and data from now on.

Again, ScriptableObjects and/or JSON are both fantastic solutions. Pick one, doesn’t matter, and thunder ahead, because once you’ve picked one and moved ahead, you will a) be making progress, and b) it isn’t very hard to change directions in the future, assuming you centralize ALL data reading to a few data-containing classes.

IOW, if you choose JSON and decide you don’t like it, you can write some code to convert all your JSON into ScriptableObjects. Yes, it is work, but it’s also a) an opportunity make things better, and b) an important lesson as to why we structure things one way or another.

1 Like

Ultimately i was in between reading the script file and making it an object but there were issues that kept the scriptable object from being intuitive. There are delegate functions that each ability calls and I have to read those for the frame data.

I’m aware that conceptually reading the script files is kind of stupid but all im saying was that for my circumstance scriptable objects were not as simple and sensible as you think.

Then maybe this needs a refactor? This could probably be easily written to be data driven. I feel like it could be a collection of objects that define their timing and what happens at each point in time.

You’ll find being data-driven will make iteration easier as you don’t need to recompile to make changes.

1 Like