0

Tried Importing Name from File1 to File 2 so it can be used in Char1 creation but error as it state index out of range Any ideas how can i get the name and roles from file 1 and input it in file 2 ?

Tested with File 1 that the name i input will get saved in the Name List when i tried printing it out but when i shift it over to File 2 its an empty list

File 1:

import pygame
import pygame_menu
import play
import PSB_Game
import random



Roles = [['WARRIOR'], ['TANK']]
Name = []
S_Role = []


   
def get_name(name):
    Name.append(name)
    print("my name is", name)
    print(Name)
def get_role(role):
    S_Role.append(role)
    print('role is',role)
    print(S_Role)





def main():
    pygame.init()
    surface = pygame.display.set_mode((800, 550))


    menu = pygame_menu.Menu('Welcome', 800, 550, theme=pygame_menu.themes.THEME_BLUE)



    menu.add.text_input('Character 1 Name:', onreturn= get_name)
    menu.add.dropselect('Role:', Roles, onchange= get_role)
    




    menu.mainloop(surface)
    pass
    




if __name__ == "__main__":
    main()

File 2:

import PlayerCreate



def main():
   

    
    Char1 = warrior(200, 260, PlayerCreate.Name[0], 100, 5, 3, 'Warrior')
    

    Char1_health_bar = HealthBar(100, screen_height - bottom_panel + 40, Char1.hp, Char1.max_hp)
  



    run = True
    while run:

        clock.tick(fps)

        #draw background
        draw_bg()

        #draw_panel
        draw_panel()
        Char1_health_bar.draw(Char1.hp)

            #draw warrior
        Char1.update()
        Char1.draw()
        



            #player action
        if Char1.alive == True:
            if current_Warrior == 1:
                action_cooldown += 1
                if action_cooldown >= action_wait_time:
                    #look for player action
                    # #attack
                    Char1.attack(Warrior1)
                    current_Warrior += 1
                    action_cooldown = 0

         
     



wanted to create a turn based battle game with the input for user for the char name and print them out 
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

Problem

The problem occurs because you use the if __name__ guard that prevents the running of an entire script when it is imported. Running the PlayerCreate script on its own will run all the code, but only the unguarded parts of the script get executed when it is imported. Running the second script only will result in the creation of the global variables PlayerCreate.Roles, PlayerCreate.Name and PlayerCreate.S_Role and import the functions from the PlayerCreate script. The PlayerCreate.Name list is empty because that is how you instantiated it.

Solution

Changing the function name main into create in the PlayerCreate script and calling the function inside your main function of file 2 would be the cleanest solution. Otherwise, if you want the entire PlayerCreate script to be ran directly when it is imported, you could remove the if __name__ guard.

Timo
  • 357
  • 1
  • 10