Passing constructed variables to another script

rpg

#1

I have an initial script that allows the user to build all the desired values for their playable character (I'm coding a retro RPG).

Next, I pass these values to a constructor which builds a character class, for example from charCreator script:

if buildButton.collidepoint(pos):
    player =  playerCharacter(str(inputStringTemp),classNameString,lvl,(0,255,0)," ",vig,fin,met,awr,kno,cha,
                                             cnd,health,evasion,speed,ath,plt,rig,spc,dec,cyb,[""],[""])

    import battleCode

Next, I import combatCode.py, which which starts a combat event. From the other script, I do:

    import sys
    import os
    import pygame
    import random
    from charCreator import player

    #Set display
    screen_size = [1280,700]
    screen = pygame.display.set_mode(screen_size)

    # Game Captions
    pygame.display.set_caption('Battle Code')
    
    #ect

When I execute the combat code script, the charCreator script launches. If I execute charCreator.py first, everything works , except that the charCreator script keeps running even after I've imported the combat script.

Why is this happening? is there a better way to pass variables built from other scripts? What are my options if all I need is the constructed player variable once it's built?

Thank you


#2

When you import a module you won't want it to have a life of its own. It should just initialise, not "start a combat event". Don't treat import as a function call.


#3

So then I should limit the battle code to function definitions and constants and simply call those from the character creator's def main()?

Alternately, would it be possible to output/write the built class and it's values to an external file and simply open it again from another script?

(Still, kind of new to coding, so I much appreciate the help!)


#4

You could have a third module that orchestrates what's going on at any given time, calling main functions from other modules. Running one script and then another? Sure, if you think of them as that separate but that's just a matter of how they're supposed to be used.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.