Passing a variable between classes?

What I’m trying to do is pass a variable that is contained in a class in one file to another.

import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.task.Task import *
from direct.showbase.DirectObject import *
import sys, random, os, math

from direct.interval.FunctionInterval import *

class Input(DirectObject):
	
	def __init__(self):
	
		self.keyMap = {"Left":0}
		
		self.accept("arrow_left",self.setKey, ["Left",1])
		self.accept("arrow_left-up",self.setKey,["Left",0])
		
		self.Left = True
		
		taskMgr.add(self.UInput,"UInput")
		
	def setKey(self, key, value):
		self.keyMap[key] = value
	
	def UInput(self, task):
		if(self.keyMap["Left"]!=0): 
			self.Left = False
		return Task.cont
		
import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.task.Task import *
from direct.showbase.DirectObject import *
import sys, random, os, math

from direct.interval.FunctionInterval import *

import UserInput
import Test

class World(DirectObject):
	
	def __init__(self):
	
		self.UserInput = UserInput.Input()
		
		taskMgr.add(Test.Tester,"Tester")
		
		
w = World()
run()
import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.task.Task import *
from direct.showbase.DirectObject import *
import sys, random, os, math

from direct.interval.FunctionInterval import *

import UserInput

def Tester(task):
	if(UserInput.Input().Left == False):
		print "Hello"
	return Task.cont

All I’m trying to do at the moment is if the left arrow key is pushed it will change the state of the Boolean. Which is then read in another file and if it’s false print it out.

The problem is when I hit the left arrow key nothing happens.

Any help or suggestions would be great!

your problem is that you don’t distinguish between class attributes and instance attributes.

self.Left = True

self indicates, that the attribute will be attached to every instance, but not the class itself, from where you try to read up your value.

try

Input.Left = True

instead or create an instance of the Input class in Test.py.

ps: you must not import DirectStart in all your files, but only in the first one (main.py or however you name it)

This is a slightly old thread, so you may not be monitoring it anymore, but I have a small suggestion.

I’m an artist and not a CS guy, but from my understanding with boolean variables it’s typical that 1 and true are associated, and 0 and false are associated. If this is the case in panda, you might want to switch around your code a bit so that if the key is pressed the value in the key map is set to 1 and the key pressed boolean is set to true, instead of false. It will make the code a little easier for others to follow, I think.