2

I'm making a text-based rpg game in Python as a self-project.

I have a class called Character inside the file char.py and a player object is created from it inside the file mapAndclasses.py . For the fight sequence, which I put in a separate file, I need to obtain the Character object from mapAndclasses.py . However, I have no idea how to use objects globally. Is it possible to do so? thank you ;-;!!

Char.py

#Char.py
class character:
def __init__(self,name,job,level,exp,curHp,maxHp,mana,strength,defence,magic,baseCrit,coordsX,coordsY):
    self.name = name
    self.job = job
    self.level = int(level)
    self.exp = float(exp)
    self.curHp = int(curHp)
    self.maxHp = int(maxHp)
    self.mana = int(mana)
    self.strength = int(strength)
    self.defence = int(defence)
    self.magic = int(magic)
    self.baseCrit = int(baseCrit)
    self.coordsX = int(coordsX)
    self.coordsY = int(coordsY)

def getCharacterInfo(self):
    print("Name:", self.name, "\t\tLevel:", self.level)
    print("Job:", self.job, "\t\tExperience:", self.exp)
    print("HP:",self.curHp, "/", self.maxHp)
    print("Mana:", self.mana)
    print("Str:",self.strength)
    print("Def:", self.mana)
    print("Magic:", self.mana)
    print("Base Crit:", self.baseCrit)

mapAndclasses.py

# Load player
def loadPlayer():

saveArray = []
with open("savefile.txt") as f:

    for line in f:
        var,val = line.strip().split("=")
        saveArray.append(val)

    f.close();

global inventory
inventory = Inventory.load()

global player
# player = name, job, level, exp, curhp, maxhp, mana, strength, defence, magic, baseCrit, x, y

player = character(saveArray[0],saveArray[1],saveArray[2],saveArray[3],
saveArray[4],saveArray[5],saveArray[6],saveArray[7],
saveArray[8],saveArray[9],saveArray[10], saveArray[11], saveArray[12])

# playerCoords = [row,column]
# player always starts from the left
global playerCoords
playerCoords = [player.getCoordsX(), player.getCoordsY()]

print("\nWelcome back " + saveArray[0])
3
  • you could make a function that gets the global variable and returns it then if you're using another file to get that variable you can import the function then run it inside the function where you want to get the global variable Commented Oct 5, 2018 at 6:51
  • 1
    You could pass the player object as an argument to the function in the fight sequence file. Since you already have a class, that's perfectly simple. No global variables needed. Commented Oct 5, 2018 at 6:56
  • @Jeronimo alright i see what you mean! I didn't know that the information from that particular object could be passed like that. Thank you so much! Commented Oct 6, 2018 at 3:35

2 Answers 2

2
  1. Create a new, empty file __init__.py in the same directory. (This just marks the directory as a package i.e. tells Python that it can pull imports from here).
  2. At the top of fightSequence.py or whatever you have called it, import your variable by writing the following: import mapAndclasses.py.
  3. Use the variable by referring to it like mapAndclasses.variableName.
Sign up to request clarification or add additional context in comments.

Comments

0

Create a new Python file, like vars.py and put all variables there.

Then import vars and it'll be global. I hope this helped you!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.