[solved]Showing countries' info by clicking on cities [python list and object/classes]

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
abysswatcher
Regular
Posts: 35
Joined: Sun Apr 12, 2020 11:50 pm
Projects: To-Live:The Struggle
Organization: Youyu de Shijie(憂鬱的世界)
Github: LuYifeng112
itch: https://luyifeng.itc
Location: New Zealand
Contact:

[solved]Showing countries' info by clicking on cities [python list and object/classes]

#1 Post by abysswatcher » Sat May 16, 2020 7:33 pm

This is the code I have so far:

Code: Select all

init -10 python:          
    class GuoPlace(object):
        def __init__(self, x, y, name, IsActive, country, ID, Port, Capital):
            self.x = x
            self.y = y
            self.name = name
            self.IsActive = IsActive
            self.country = country
            self.ID = ID
            self.Port = Port
            self.Capital = Capital 
            
    class Nation(object):
        def __init__(self, name, ID, leader, politicalID, AlignmentID, rulingparty, factionID, stability, warsupport):
            self.name = name
            self.ID = ID
            self.leader = leader
            self.politicalID = politicalID
            self.AlignmentID = AlignmentID
            self.rulingparty = rulingparty
            self.factionID = factionID
            self.stability = stability
            self.warsupport = warsupport

    TL_GUO_loc = []
    TL_GUO = []
    t = 0
    while t < 50:
        TL_GUO_loc.append(GuoPlace(0,0,"", False, "","", False, False))
        TL_GUO.append(Nation("","","","","","","",0,0))
        t += 1

    TL_GUO_loc[0] = GuoPlace(2819, 2023, "Kwangchow", True, "Republic of China", "ROC", False, False)
    TL_GUO_loc[1] = GuoPlace(2888, 2062, "Hong Kong", True, "United Kingdom", "ENG", True, False)
    TL_GUO_loc[2] = GuoPlace(2826, 2069, "Macau", True, "Portugese Republic", "POR", True, False)
    TL_GUO_loc[3] = GuoPlace(2565, 2167, "Kwang-Chow Wan", True, "French Republic", "FRA", True, False)
    TL_GUO_loc[4] = GuoPlace(3436, 1392, "Shanghai", True, "Republic of China (Interntional Settlement)", "CHI",True, False)
    TL_GUO_loc[5] = GuoPlace(3262, 1341, "NanKing", True, "Republic of China", "CHI", False, True)
    TL_GUO_loc[6] = GuoPlace(4040, 2230, "Peiping", True, "Republic of China", "CHI", False, True)
    TL_GUO_loc[7] = GuoPlace(3276, 1805, "Foochow", True, "Republic of China", "CHI", True, False)
    TL_GUO_loc[8] = GuoPlace(3450, 1492, "Ning-Po", True, "Republic of China", "CHI", True, False)
    TL_GUO_loc[9] = GuoPlace(3042, 1556, "Nan-Kang", True, "Republic of China", "CHI", False, False)
    TL_GUO_loc[10] = GuoPlace(3087, 1462, "An-King", True, "Republic of China", "CHI", False, False)




    
    TL_GUO[0] = Nation("Republic of China", "ROC", "Chiang Chie Shih", "Conservative Junta", "Central Government of Kuomintang", "Kuomintang", "Allied Front", 40, 50)
    TL_GUO[1] = Nation("People's Liberation Army", "PRC", "Mao Tse-Tung", "Marxist Guerillas", "Chinese Soviet Republic", "Chung-kuo Kung-ch'an-tang", "Allied Front", 30, 30)
    TL_GUO[2] = Nation("Empire of Japan", "JAP", "Emperor Hirohito", "Fascist Monarchy", "Co-Prosperity Sphere", "Tohokai", "Co-Properity Sphere", 60, 90)
    TL_GUO[3] = Nation("Empire of Manchukuo", "MAN", "Aisin Gioro Pu-Yi", "Fascist Puppet Monarchy", "Co-Prosperity Sphere", "Manchōwkuó Hsiéhehuì", "Co-Prosperity Sphere", 70, 70)
    TL_GUO[4] = Nation("Reorganized National Government of the Republic of China", "RNG", "Wang Ching-Wei", "Fascist Puppet State", "Co-Prosperity Sphere", "Left Kuomintang", "Co-Prosperity Sphere", 40, 30)
    TL_GUO[5] = Nation("Shaanxi Clique", "SHX", "Yan HsiShan", "Conservative Junta", "Central Government of Kuomintang", "Kuomintang", "Allied Front", 40, 50)
    TL_GUO[6] = Nation("Hsi Bei San Ma", "XSM", "Ma Bu Fang", "Conservative Junta", "Central Government of Kuomintang", "Kuomintang", "Allied Front", 40, 50)
    TL_GUO[7] = Nation("KwangHsi Clique", "GXC", "Li Tsun-Jen", "Conservative Junta", "Central Government of Kuomintang", "Kuomintang", "Allied Front", 40, 50)
    TL_GUO[8] = Nation("Yunnan Clique", "YUN", "Lung Yun", "Conservative Junta", "Central Government of Kuomintang", "Kuomintang", "Allied Front", 40, 50)

I was wondering how would it work if by clicking on a city there would be a screen with this info of the country. I want to know if there is a simple way I can use the country ID's in the city and nation objects so they are "bonded" for a lack of better word. Would it be possible? I've used loops to map my cities in the code below:

Code: Select all

screen guo_map():
    key "K_SPACE" action ShowMenu(main_menu)
    viewport:
        xysize (config.screen_width, config.screen_height)
        child_size (4040, 2230)
        xinitial 2300
        yinitial 500
        draggable True
        edgescroll (700, 700)
        add "maps/GUO_map.png"
        for q in TL_GUO_loc:
            if q.IsActive:
                button:
                    xpos q.x
                    ypos q.y
                    text q.name color "#000000" 
                    action NullAction()
If you have any ideas how this can go about then please raise them up! I'd love to push the limits of what can be done. Thank you!

Edit: The sovled Screen Code
This screen code is a skeleton code to test out the code and wallah it worked.

Code: Select all

screen guo(): #Test if you can actually change the variable, call screen and see a diff country. This would be cool.
    tag menu
    key "K_SPACE" action ShowMenu(main_menu)
    key "K_TAB" action ShowMenu(main_menu)
    key "K_ESCAPE" action Return()
    key "n" action ShowMenu('guo_map')
    key "1" action ShowMenu('guo_map')
    key "g" action ShowMenu('glossary')
    key "p" action ShowMenu('poems')
    key "l" action ShowMenu('historical_event_log')
    key "2" action ShowMenu('glossary')
    key "3" action ShowMenu('poems')
    key "4" action ShowMenu('historical_event_log')
    window:
        style "gm_root"
        add "#141414"
    viewport:
        xysize (config.screen_width, config.screen_height)
        child_size (2000, 1100)
        draggable True
        edgescroll (200, 200)
        
        vbox spacing 25:
            for n in TL_GUO:
                if n.IsActive:
                    textbutton n.name:
                        action SetVariable("nation", n)
    text "[nation.name]" xalign 0.5 size 40 font "fonts/eng_moria/MoriaCitadel.ttf"


screen guo_map():
    tag menu
    key "K_SPACE" action ShowMenu(main_menu)
    key "K_TAB" action ShowMenu(main_menu)
    key "K_ESCAPE" action Return()
    key "5" action ShowMenu ('guo')
    key "n" action Return()
    key "1" action Return()
    key "g" action ShowMenu('glossary')
    key "p" action ShowMenu('poems')
    key "l" action ShowMenu('historical_event_log')
    key "2" action ShowMenu('glossary')
    key "3" action ShowMenu('poems')
    key "4" action ShowMenu('historical_event_log')
    viewport:
        xysize (config.screen_width, config.screen_height)
        child_size (4040, 2230)
        if show_beijing:
            xinitial 2300
            yinitial 500
        else:
            xinitial 2300
            yinitial 500
        draggable True
        edgescroll (700, 700)
        add "maps/GUO_map.png"
        for q in TL_GUO_loc:
            if q.IsActive:
                for n in TL_GUO:
                    if n.ID ==q.ID:
                        $ act = SetVariable('nation', n)
                button:
                    xpos q.x
                    ypos q.y
                    text q.name color "#000000" hover_color "#FF0000" size 20
                    action [act, Show('guo')]
Last edited by abysswatcher on Sun May 24, 2020 7:41 am, edited 2 times in total.
The goal of the revolution is to achieve the people's rights, but during the course of the revolution, we must stress military power - and the two are mutually contradictory.
-Sun Yat-sen
"Become a Visual Novel writer they said, it will be fun" (little did they know they were right)

User avatar
MaydohMaydoh
Regular
Posts: 165
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: Showing countries' info by clicking on cities [python list and object/classes]

#2 Post by MaydohMaydoh » Sat May 16, 2020 8:45 pm

For the way you have it currently set up, you could use nested loops to check the IDs against each other and then pass the nation object to a new screen.

Code: Select all

screen guo_map():
    for q in TL_GUO_loc:
        if q.IsActive:
            button:
                xpos q.x
                ypos q.y
                text q.name color "#000000" 
                for n in TL_GUO:
                    if n.ID == q.ID:
                        action Show('nation_info', n)
                    
screen nation_info(nation):
    text nation.name
    text nation.leader
    ## etc..

User avatar
abysswatcher
Regular
Posts: 35
Joined: Sun Apr 12, 2020 11:50 pm
Projects: To-Live:The Struggle
Organization: Youyu de Shijie(憂鬱的世界)
Github: LuYifeng112
itch: https://luyifeng.itc
Location: New Zealand
Contact:

Re: Showing countries' info by clicking on cities [python list and object/classes]

#3 Post by abysswatcher » Sun May 17, 2020 1:44 am

MaydohMaydoh wrote:
Sat May 16, 2020 8:45 pm
For the way you have it currently set up, you could use nested loops to check the IDs against each other and then pass the nation object to a new screen.

Code: Select all

screen guo_map():
    for q in TL_GUO_loc:
        if q.IsActive:
            button:
                xpos q.x
                ypos q.y
                text q.name color "#000000" 
                for n in TL_GUO:
                    if n.ID == q.ID:
                        action Show('nation_info', n)
                    
screen nation_info(nation):
    text nation.name
    text nation.leader
    ## etc..
This makes a lot of sense! Thanks for steering me in the right direction!
The goal of the revolution is to achieve the people's rights, but during the course of the revolution, we must stress military power - and the two are mutually contradictory.
-Sun Yat-sen
"Become a Visual Novel writer they said, it will be fun" (little did they know they were right)

User avatar
abysswatcher
Regular
Posts: 35
Joined: Sun Apr 12, 2020 11:50 pm
Projects: To-Live:The Struggle
Organization: Youyu de Shijie(憂鬱的世界)
Github: LuYifeng112
itch: https://luyifeng.itc
Location: New Zealand
Contact:

Re: Showing countries' info by clicking on cities [python list and object/classes]

#4 Post by abysswatcher » Sun May 17, 2020 2:00 am

MaydohMaydoh wrote:
Sat May 16, 2020 8:45 pm
For the way you have it currently set up, you could use nested loops to check the IDs against each other and then pass the nation object to a new screen.

Code: Select all

screen guo_map():
    for q in TL_GUO_loc:
        if q.IsActive:
            button:
                xpos q.x
                ypos q.y
                text q.name color "#000000" 
                for n in TL_GUO:
                    if n.ID == q.ID:
                        action Show('nation_info', n)
                    
screen nation_info(nation):
    text nation.name
    text nation.leader
    ## etc..
When I put in the code, it seemed to state this error.

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/To_LIVE_GUI/screen_TL_political_tabs.rpy", line 24: u'SetVariable' is not a keyword argument or valid child for the for statement.
    SetVariable('nation_info', n)
               ^

Ren'Py Version: Ren'Py 7.3.5.606
Sun May 17 17:58:08 2020
Would you know what might be off?
The goal of the revolution is to achieve the people's rights, but during the course of the revolution, we must stress military power - and the two are mutually contradictory.
-Sun Yat-sen
"Become a Visual Novel writer they said, it will be fun" (little did they know they were right)

User avatar
MaydohMaydoh
Regular
Posts: 165
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: Showing countries' info by clicking on cities [python list and object/classes]

#5 Post by MaydohMaydoh » Sun May 17, 2020 4:53 am

I guess you can't put the for loop inside the button.

Code: Select all

for q in TL_GUO_loc:
    if q.IsActive:
        for n in TL_GUO:
            if n.ID == q.ID:
                $ act = SetVariable('nation_info', n)
        button:
            xpos q.x
            ypos q.y
            text q.name color "#000000" 
            action act

User avatar
abysswatcher
Regular
Posts: 35
Joined: Sun Apr 12, 2020 11:50 pm
Projects: To-Live:The Struggle
Organization: Youyu de Shijie(憂鬱的世界)
Github: LuYifeng112
itch: https://luyifeng.itc
Location: New Zealand
Contact:

Re: Showing countries' info by clicking on cities [python list and object/classes]

#6 Post by abysswatcher » Sun May 17, 2020 9:00 pm

MaydohMaydoh wrote:
Sun May 17, 2020 4:53 am
I guess you can't put the for loop inside the button.

Code: Select all

for q in TL_GUO_loc:
    if q.IsActive:
        for n in TL_GUO:
            if n.ID == q.ID:
                $ act = SetVariable('nation_info', n)
        button:
            xpos q.x
            ypos q.y
            text q.name color "#000000" 
            action act
I tried out this code and it got along better. I tried a variety of ways to get it to show but everytime it says that nation_info is not defined. So I changed it to SetScreenVariable and it worked but my nation window won't show up. I then added it on this way:

Code: Select all

for q in TL_GUO_loc:
            if q.IsActive:
                for n in TL_GUO:
                    if n.ID == q.ID:
                        $ act = [SetScreenVariable('nation_info', n), SetScreenVariable('nation', n)]
            button:
                xpos q.x
                ypos q.y
                text q.name color "#000000" hover_color "#FF0000"
                action act
It says that 'nation' as a parameter has no value. This has me stumped. Sorry to bother but I'm new to loops and screen gui in renpy.
The goal of the revolution is to achieve the people's rights, but during the course of the revolution, we must stress military power - and the two are mutually contradictory.
-Sun Yat-sen
"Become a Visual Novel writer they said, it will be fun" (little did they know they were right)

philat
Eileen-Class Veteran
Posts: 1759
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Showing countries' info by clicking on cities [python list and object/classes]

#7 Post by philat » Sun May 17, 2020 9:32 pm

You could do this in any number of ways, but, uh, why would you not reference stuff directly (in whichever direction you prefer)?

Code: Select all

TL_GUO_loc[0] = GuoPlace(2819, 2023, "Kwangchow", True, TL_GUO[0], "ROC", False, False)  
e.g., TL_GUO[0], rather than "Republic of China", meaning you could refer to whatever you need by q.country directly, like q.country.name or q.country.stability.

User avatar
abysswatcher
Regular
Posts: 35
Joined: Sun Apr 12, 2020 11:50 pm
Projects: To-Live:The Struggle
Organization: Youyu de Shijie(憂鬱的世界)
Github: LuYifeng112
itch: https://luyifeng.itc
Location: New Zealand
Contact:

Re: Showing countries' info by clicking on cities [python list and object/classes]

#8 Post by abysswatcher » Mon May 18, 2020 5:20 pm

philat wrote:
Sun May 17, 2020 9:32 pm
You could do this in any number of ways, but, uh, why would you not reference stuff directly (in whichever direction you prefer)?

Code: Select all

TL_GUO_loc[0] = GuoPlace(2819, 2023, "Kwangchow", True, TL_GUO[0], "ROC", False, False)  
e.g., TL_GUO[0], rather than "Republic of China", meaning you could refer to whatever you need by q.country directly, like q.country.name or q.country.stability.
Thats genius! Thanks for pushing me in the right direction again :)
The goal of the revolution is to achieve the people's rights, but during the course of the revolution, we must stress military power - and the two are mutually contradictory.
-Sun Yat-sen
"Become a Visual Novel writer they said, it will be fun" (little did they know they were right)

User avatar
abysswatcher
Regular
Posts: 35
Joined: Sun Apr 12, 2020 11:50 pm
Projects: To-Live:The Struggle
Organization: Youyu de Shijie(憂鬱的世界)
Github: LuYifeng112
itch: https://luyifeng.itc
Location: New Zealand
Contact:

Re: Showing countries' info by clicking on cities [python list and object/classes]

#9 Post by abysswatcher » Sun May 24, 2020 7:39 am

So I did some experimentation and I got it working. I just used variables instead of screen parameters for now so I can understand the code better. Here's a copy of the solved screen code.

Code: Select all

screen guo(): #Test if you can actually change the variable, call screen and see a diff country. This would be cool.
    tag menu
    key "K_SPACE" action ShowMenu(main_menu)
    key "K_TAB" action ShowMenu(main_menu)
    key "K_ESCAPE" action Return()
    key "n" action ShowMenu('guo_map')
    key "1" action ShowMenu('guo_map')
    key "g" action ShowMenu('glossary')
    key "p" action ShowMenu('poems')
    key "l" action ShowMenu('historical_event_log')
    key "2" action ShowMenu('glossary')
    key "3" action ShowMenu('poems')
    key "4" action ShowMenu('historical_event_log')
    window:
        style "gm_root"
        add "#141414"
    viewport:
        xysize (config.screen_width, config.screen_height)
        child_size (2000, 1100)
        draggable True
        edgescroll (200, 200)
        
        vbox spacing 25:
            for n in TL_GUO:
                if n.IsActive:
                    textbutton n.name:
                        action SetVariable("nation", n)
    text "[nation.name]" xalign 0.5 size 40 font "fonts/eng_moria/MoriaCitadel.ttf"


screen guo_map():
    tag menu
    key "K_SPACE" action ShowMenu(main_menu)
    key "K_TAB" action ShowMenu(main_menu)
    key "K_ESCAPE" action Return()
    key "5" action ShowMenu ('guo')
    key "n" action Return()
    key "1" action Return()
    key "g" action ShowMenu('glossary')
    key "p" action ShowMenu('poems')
    key "l" action ShowMenu('historical_event_log')
    key "2" action ShowMenu('glossary')
    key "3" action ShowMenu('poems')
    key "4" action ShowMenu('historical_event_log')
    viewport:
        xysize (config.screen_width, config.screen_height)
        child_size (4040, 2230)
        if show_beijing:
            xinitial 2300
            yinitial 500
        else:
            xinitial 2300
            yinitial 500
        draggable True
        edgescroll (700, 700)
        add "maps/GUO_map.png"
        for q in TL_GUO_loc:
            if q.IsActive:
                for n in TL_GUO:
                    if n.ID ==q.ID:
                        $ act = SetVariable('nation', n)
                button:
                    xpos q.x
                    ypos q.y
                    text q.name color "#000000" hover_color "#FF0000" size 20
                    action [act, Show('guo')]
By setting variables and then showing the screen it does what I needed. :)
The goal of the revolution is to achieve the people's rights, but during the course of the revolution, we must stress military power - and the two are mutually contradictory.
-Sun Yat-sen
"Become a Visual Novel writer they said, it will be fun" (little did they know they were right)

Post Reply

Who is online

Users browsing this forum: Bing [Bot]