Forum Archive

How can i pass data between different scenes and/or ui views

walterwang

I have a login ui view that when authenticated will show a setup scene. This scene is added to the root view with SceneView(). However, I would like the scene view to get some information from ui view. But I am not sure how to pass that information from the ui to the scene. Any help would be greatly appreciated

mikael

You can e.g. pass information as parameters to your Scene init, or directly set values on the Scene or call it’s methods.

Maybe if you share some code we can provide more exact suggestions?

walterwang

What I have kinda works right now, but I am not sure if its best practice. For example setting the view to hidden, still results in the scene being updated. Since its updated before the value that I want to pass is set, I have to put it in a try block so it won’t have unreferenced error

import ui
from scene import *
import requests
import time
server_ip='http://0.0.0.0:5000/'

class MyScene(Scene):
    def setup(self):
        self.background_color = 'midnightblue'
        self.querysetup = True
    def update(self):
        try:
            if client.login_request is not None:
               r = requests.get(server_ip+'get_setup',cookies=client.login_request.cookies).content
        except:
            pass

class SwitchViews(ui.View):
    def __init__(self):
        self.login_view = self.create_login_view()
        self.scene_view = SceneView()
        self.scene_view.scene = MyScene()
        self.add_subview(self.scene_view)
        self.add_subview(self.login_view)
        self.background_color = 'white'
        self.present(style = 'sheet', hide_title_bar=True)
        self.scene_view.hidden = True
        self.login_request = None

    def create_login_view(self):
        def login_tapped(sender):
            self.login_request = requests.post(server_ip+'login', data = {'username':view['username'].text, 'password':view['password'].text})
            if 'authenticated' in str(self.login_request.content):
                self.scene_view.hidden = False
                self.login_view.hidden = True

        def register_tapped(sender):
            r = requests.post(server_ip+'register', data = {'username':view['username'].text, 'password':view['password'].text})
            print(r.content)

        view = ui.View(frame=(0,0,736, 414))
        view.background_color = 'white'
        username = ui.TextField(frame=(view.width/2-70,view.height/2-80,140,25))
        username.name = 'username'
        username.alignment = ui.ALIGN_CENTER
        username.placeholder = 'username'
        view.add_subview(username)

        password = ui.TextField(frame=(view.width/2-70, view.height/2-50, 140, 25))
        password.name = 'password'
        password.secure = True
        password.alignment = ui.ALIGN_CENTER
        password.placeholder = 'password'
        view.add_subview(password)

        login_button = ui.Button(name = 'login', frame=(view.width/2-70, view.height/2-20, 65, 25))
        login_button.title = 'Login'
        login_button.border_color = 'blue'
        login_button.border_width = 1 
        login_button.corner_radius = 5
        login_button.action = login_tapped
        view.add_subview(login_button)

        register_button = ui.Button(name = 'Register', frame = (view.width/2, view.height/2-20, 65, 25))
        register_button.title = 'Register'
        register_button.border_color = 'blue'
        register_button.border_width = 1
        register_button.corner_radius = 5
        register_button.action = register_tapped
        view.add_subview(register_button) 
        return view
client = SwitchViews()  


JonB

Another option is to only set the sceneview.scene attribute when you are ready for the scene to run, and set it to None when you want it to stop.

JonB

@walterwang You can also set SceneView.paused=True to prevent the scene from running.

Actually, Im not really sure you want to be using requests within scene.update... that gets called at 60 fps, unless you have set the sceneview.frame_interval. Do you really want to be clobberring the server as fast as you can poll it?

walterwang

I get a completely black screen when set pause to true and then set it to false

enceladus

Look at the brickbreaker example in examples/games folder (BrickBraker.py in Examples/Games). It uses 'pause and present_modal_scene for displaying menu and I hope it will be useful in understanding . (BTW, you can also use global variables to pass data.)

JonB

https://gist.github.com/6dacbc24e874bf4458772cc4a2bc4aad
This is an example of using pause in a sceneview

mikael

@walterwang, could you briefly explain why you want to run the blocking login request in Scene?

walterwang

@mikael I have a login screen which when authenticated gets a request.cookie that is used to identify the user as a session in flask. The setup screen requires information from the user's account, and therefore cannot be updated or rendered until the user logs in from the login screen. But once logged in, I only need to post the request once to get the needed information. So far I have a try block and a setup_query_once = True boolean, which is set to false after setting up once in the update function.

JonB

why not just handle evrything in your login code, i.e inside the ui callback? it seems really clunky to do that within update.

walterwang

@JonB, I can do that with the request cookies part of the login, but there is also additional information that needs to be rendered only once. This information is only available once you log in.

JonB

What I mean is, the login code should be run in the UI portion of the code. Then once you are logged in, begin rendering the scene (using pause). Did you try the example above that showed use of pause?

I would just worry about running anything blocking or time consuming within the update loop. I think your original code would have kept trying to login -- you should only send a single login request, and only retry with a timeout.

walterwang

Hi JonB thanks for the advice. sorry the code I posted here was too simplified, (My actual code was just a single if statement testing a boolean variable) But I have changed it to a better way of adding subview, I don't know why it didn't work at first. So the login code is in the setup now. I haven't tried using pause yet, so far I am just using add_subview when initializing the scene for the first time, and scene.hidden =True to hide the previous menu.

Thanks for all the help!

JonB

out of curiosity ... what is this going to become? a multiplayer game?

walterwang

yes, its a turn based multiplayer game. It will be a lot like chess.