How do I run multiple sections of code at the same time? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do I run multiple sections of code at the same time?

I'm trying to make an rpg game, however I need to make an xp system that can constantly be active and xp and lvs can increase at any time. All of this while other sections of code are running, like a battle or a basic output.

7th Jun 2023, 7:08 PM
Dylan Scully
Dylan Scully - avatar
9 Answers
+ 4
Modern computers are equipped with multiple processors (CPU cores), meaning that they can do several tasks at the same time. An operating system solves the separation of these tasks with "threads", which are independent processes. Python is inherently very bad at multithreading because of its architecture, the Global Interpreter Lock (GIL). The threading and multiprocessing libraries can help solve these problems. https://realpython.com/python-gil/ A more modern approach is using asynchronous operations. https://realpython.com/async-io-python/ A small example showing how parallel threads work: https://code.sololearn.com/c3vnjMxf5RL9/?ref=app
7th Jun 2023, 8:42 PM
Tibor Santa
Tibor Santa - avatar
+ 3
The answer to the question you're asking is multithreading, but that also doesn't seem like the right tool for this job. It's not as though xp gains can be triggered unexpectedly, here, and a dedicated xp thread running constantly in the background would just spend most of its time waiting in the background anyway. Personally I'd probably solve this by just writing a global "gainXP" function that also checks for level-up any time you call it. Or, since only player characters can usually gain xp, you could even just add a gainXP method to your character class. (in fact now that I think about it, that just sounds like a basic setter)
7th Jun 2023, 7:22 PM
Orin Cook
Orin Cook - avatar
+ 1
(also technically thanks to the GIL Python doesn't actually do true multithreading, it just pretends. So you'd probably want to switch to a better threading language like Go)
7th Jun 2023, 8:38 PM
Orin Cook
Orin Cook - avatar
+ 1
Python threading enables you to run different parts of your program at the same time. You can learn more about threading here.https://realpython.com/intro-to-python-threading/ Before we get to that, have you programmed any simple games that keep track of levels, experience points, or lives?
7th Jun 2023, 10:18 PM
Chris Coder
Chris Coder - avatar
0
But then I'd have to keep declaring the function so is there a way where whenever I input something like stats it will output them because I can work with that
7th Jun 2023, 8:04 PM
Dylan Scully
Dylan Scully - avatar
0
I'm not sure how you think you're going to tell the computer that something has gained xp without declaring *something* (which may as well be the function). Are you imagining a background process that watches for certain types of events and independently assigns xp for them? Because that sounds like a nightmare to code and debug, and also like you're just begging for race conditions.
7th Jun 2023, 8:20 PM
Orin Cook
Orin Cook - avatar
0
Yep
7th Jun 2023, 11:02 PM
Dylan Scully
Dylan Scully - avatar
0
Help to make a game like pubg mobile
9th Jun 2023, 7:00 AM
Total Pompu
Total Pompu - avatar
0
Use Threads: Make two threads and then make them run at simultaneous time, use the first thread to run the first operation and use the second thread to run the second operation and so on. This is known as Multithreading
9th Jun 2023, 9:55 AM
Rishabh Srivastava
Rishabh Srivastava - avatar