WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Initializing/Changing Variables (https://forum.worldviz.com/showthread.php?t=2492)

jassel41 02-02-2010 09:47 AM

Initializing/Changing Variables
 
I am currently working on a program for a learning video game for students. The scenario right now is that they see a traffic light blinking incorrectly and are supposed to input appropriate timing constraints for the light. For now I have the lights blinking on a timing loop where two are simply invisible if the other is on.

I want to give the students the option to change the value of the variable controlling these lights, but I do not know how to initialize a variable to one value and then be able to change it later without it conflicting with the initialization statement.

Can anyone provide any tips on how to start with a variable at one value and then be able to give the user control to change it if they want without being reset by the first initialization statement?

Thank you
- Jassel41

Jeff 02-03-2010 09:28 AM

You can modify a variable within a function by using the global keyword. Here pressing the spacebar modifies a variable and then prints it.
Code:

import viz
import vizact

viz.go()

var = 1

def addTwo():
        global var
        var += 2
        print var
       
vizact.onkeydown(' ',addTwo)



All times are GMT -7. The time now is 02:41 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC