Haha.
Solved my own problem in less than 10 minutes.
So what I really wanted to do was to make a simple game modifiable.
So I created this (not necessarily smart or safe) method of making a configuration file that could be used for "modding".
This is what it looks like:
# main.py
import turtle as t
screen = t.Screen()
turt = t.Turtle()
cfg = open("mods.py","r")
cfg = cfg.read()
cfg = cfg.split("\n")
for i in range(len(cfg)):
if i < len(cfg):
exec(cfg[i])
for i in range(8):
turt.forward(100)
turt.right(45)
turt.backward(75)
screen.exitonclick()
and in mods.py:
turt = t.Turtle()
turt.color("red")
Now before everyone gets all up in my face for using exec
, let me just say that for a solution that was thought of quickly, I am okay with it. Plus, it wouldn't be that hard to add some sort of portection, ie if "os" not in cfg[i]: exec(cfg[i])
and all of that would really just be a matter of time consumption.
I had to add if i <len(cfg):
because when I split at the \n
that was adding a blank element at the end of the list it generated. I doubt that would raise an error, but no harm in being a little extra cautious.
Overall, I'm glad with my solution... I'll add security detection and stuff but as of right now it will work, because the game I am working on will not be a public game.