Set Property to Python Variable?


(mindblender) #1

I have have no Python background. My background is in Visual Basic. is there away to take a property set to an object and set it as a variable in Python, then access that variable with another object? Thanks in advance.


(z3r0 d) #2

can you be more specific?

[also, are you talking about for the game engine?]


(mindblender) #3

Oh, sorry, yes the Game Engine. But I just had a revelation. The only problem is I’m not doing something right(wow, go figure). When I say

import GameLogic

it says

Import Error : No Module Named GameLogic

Is there something I’m not doing? Here’s the code I have so far:

import Blender
import sys
import GameLogic

cont=GameLogic.getCurrentController()
own=cont.getOwner()
draw=own.draw
draw=GameLogic.getRandomFloat()*51
draw=draw+1
own.draw=draw

Any help will be appreciated. Thanks.


(z3r0 d) #4

game engine scripts must be run in the game engine using a python controller, hitting alt+p in the text window will not do it

you can access properties of objects containing any logic bricks that are connected to the python controller in the same manner as you are doing now

cont = GameLogic.GetCurrentController()
sensor = cont.getSensors()[0]

me = cont.getOwner()
somebody = sensor.getOwner()

you can have links between logic bricks of different objects


(mindblender) #5

Thanks it’s working now. I was starting the game in the first place, but I restarted Blender and now I’m not having a problem. The only thing is, I have the python connected to a keyboard sensor for the SPACE bar. When I press the SPACE bar, more than one random number is generated. It’s like it generates a random number when I press it down and when I release it. Why is that?


(z3r0 d) #6

scripts are run on both true and false pulses

a keyboard sensor [by default] sends a true pulse when you press the key, and a false one when you release it

some actuators need false pulses for things, like for example a loop stop ipo actuator will use a false pulse to stop the animation.

well anyway, that is why sensors have a .isPositive() method


(mindblender) #7

What does the [0] mean and when I declare the variable “sensor” can I just say

if sensor.ispositive():

or if sensor.ispositive(true):

or in other words, how do I use it to test if it is positive or not? Sorry for nagging you, but I really want to try to learn this. Thanks.


(z3r0 d) #8

sensor1 = cont.getSensor("steve")

sensorList = cont.getSensors()
sensor2 = sensorList[0]

# same as above two lines
sensor3 = cont.getSensors()[0]

if sensor1.isPositive():
   # true pulse
   pass
else:
   # false pulse
   pass

(mindblender) #9

Thank you so much. You have no idea how much you just helped me. Without that I couldn’t have even begun(began/begun?) to start this game. It works perfectly. I even set up some tests to make sure no wrong numbers were coming out and how long on average it takes to get a certain number. At most it took a second and a half. That’s useful information. If you get down to one last card in the deck, which is unlikely for black jack, you don’t want to have to wait for an hour to have the card dealt. But it works. Now I have to finish the other 99.9999997% of the game. But thanks again.


(mindblender) #10

One more question(for now): how do I declare a public variable that is accessible all throughout the game(i.e. with every script)?


(z3r0 d) #11

unfortunately there isn’t a good way to do this

the approach of the moment is simply to set it in gamelogic

for example

GameLogic.blah = 7