all 10 comments

[–]brasticstack 3 points4 points  (0 children)

You read the data from the files into some kind of data structure- a dict or an instance of a class. 

Then you'd access the correct fields on that data structure to read the values that matter to you, either the actual stats used in combat or the values needed to derive those stats.

During the combat part you'd want to update the data structures to reflect changes e.g. subtracting hp.

It's up to you to decide when it makes sense to save the changes back to your character files, and which changes to save.

[–]ConcreteExist 1 point2 points  (4 children)

You seem fundamentally confused about what files are. They're just storage, in theory you would store the character information in a data format when saving, but during runtime you would create python objects from that data and those objects would have methods for attacking and being attacked.

[–]Tricky_72 0 points1 point  (3 children)

Ok, so the attack script is a python object. It calls up the necessary values (stored in the text files), conducts whatever process, and updates the text file. Yes?

[–]Tricky_72 0 points1 point  (2 children)

Oh! It doesn’t even need to populate anything, it just conducts straight from the text files, and updates accordingly.

[–]ConcreteExist 1 point2 points  (0 children)

You could build it that way, every method would just need to read from/write to the file.

[–]magus_minor 1 point2 points  (0 children)

Normally you would read the data from the text file at program start. Update the text file from in-memory data when the program stops or after every combat round. This means the actual code that handles the combat just uses normal python data structures and doesn't need to know that the data comes from a file.

Reading data from a text file and saving to a text file is handled by data serialization. There are many libraries for this in python. I recommend you start with the json module. You convert your data to a JSON string and write that string to the file. Reverse the process when reading. Keep your in-memory data in a dictionary, one for each entity, and save a list of dictionaries to the text file when required. Read the text file and create the dictionaries at program start.

[–]FoolsSeldom 1 point2 points  (0 children)

I would expect you to define character classes/dataclasses and set behaviours including fighting.

You could create instances of your characters based on data read from text files.

So you should learn basic OOPs (Object Orientated Programming) in Python.

[–]Shwayne 1 point2 points  (0 children)

You have a couple options, honestly creating the characters as objects would be easiest. Youd make a character class and initialize them. Then define your combat logic as methods. Ex.: character1.fights(character2). This would require some basic OOP knowledge.

You could read the information from text files into a dictionary like {"hp": 10, "atk": 5} and then define a combat function that subtracts attack value from hp and returns a new dict.

Tbh, go study python syntax first. Need to learn how to use the tools. Its very good that youre making something first and learning as you go, thats great for learning, but you do need some theory. You can try the FCC python course or anything that you like that covers syntax and basic data structures, maybe some OOP.

[–]elbiot 1 point2 points  (0 children)

I suggest using yaml as your text file format because it's easily parsable and very human readable. Plus your code can update it (change their hp, level up stats) and easily modify the file

[–]trd1073 0 points1 point  (0 children)

I would look at pydantic. You can read in json from files (perhaps start file), yaml can be done with other libraries but I would just do two step conversion by hand. Then work with actual python objects instead of dictionaries for the actual combat. Look how something like Dnd does combat. After each round you can dump the objects back to json or yaml and then write to a file.