Simple python learning issues

So im trying to learning how to use variables and Booleans. Wanted simple so i try this.

 
 
logic import stuff
 
human = True
if this == True:
   human = False

It doesnt work. Throws an error
Id love to be able to change things around easily in scripts like this. Im wondering if im not using it right

or this

ill add a Boolean named ‘Go’ to object

 
go = ob["Go"]
if this == True:
    go = False
    ob["Go"] = go

Doesnt work
still error.

I can change numeric variables in objects fine. Just been using 0 or 1. Would just like to understand be able to change other things. Am i doing it wrong for these types?

IF i use

go = ob[“Go”]
print(go)

it shows true. But when I add the stuff under if’s…it stops working.

Anyone know Y this isnt working. Or knows some very basic scripts for Booleans or changing simple variables/integers in a script.

EDIT: BTW i do know how to do this

 
logic.globalDict["this"] = False
   if thishappens == True:
   logic.globalDict["this"] = True

And it works, But I was Just hoping to avoid globaldict
Many thanks in advance

Is “this” something?

Make a boolean property called This and set it to true in the logic editor. So you’ll have two boolean properties, Go and This both set to true.

In your script you will have:

go = ob[“Go”]
this = ob[“This”]

Then you can write:

if this == True:
go = False
print(go)

Also you can check the little info button on the property block and check Debug Properties from the main Blender menu so you can see the properties onscreen at runtime.

Is this a property on an object? If it is, then you need to tell Blender that it’s an object property.


from bge import logic as g
ob = g.getCurrentController().owner

#script here
if ob['this'] == true:
     #more stuff here


superflip might have been thinking that “this” was a word used by python to evaluate an expression preceding an if statement.

‘this’ is not a propery or anything. Its just in the script. I use it for If global’this’ = true do this. on other scripts on other objects. I can make it do something on a seperate scrtipt. I was trying to just set up how to change a booleans or something simpler. Like, how do i change the ‘Go’ property to False…if event or sensor is true

Just noticed
Sorry I screwed up because i used ‘this’ in 2 seperate examples :smiley:

The whole part about “this” in the last note script is seperate. Its just another way it works. ‘this’ can be anything in that one. The Boolean property is Go. I was more or less wanting to learn this

go = ob["Go"]
if this == True:
    go = False
    ob["Go"] = go

how to change my boolean named ‘Go’ property that is set True, from True to False when ‘this’ happens…‘this’ is whatever event or whatever I want it to be here

Have you set you properties to display at runtime? It makes it easy to see them change if you are changing their value or toggling them with a keypress or something. Sure you can use the system console to test things but sometimes that’s a bit of a hassle at runtime.

Ok if you want to toggle ob[“Go”] between True and False. Use a keyboard sensor, say spacebar attached to your script. After the usual headers your script can have something like:

go = ob[“Go”]

if go == False:
go = True
if go == True:
go = False

The spacebar will toggle it.

Edit: I didn’t format the indents correctly but you know how to do that.

We’re cross posting a little but:

These things can be confusing for the new user because they don’t quite get how logical and simple these things are until they understand them.

“this” is pseudocode, like when someone writes “do something” or “something happens” in a tutorial script. The “something” that happens might be a sensor being fired by a keypress in which case there needn’t be any mention of it in the script. That the script was activated was enough.

Or the “something” might be the value of a property of itself (my spacebar toggle script), the value of another property of the owning object or even the value of the property of another object in the scene.

go = ob[“Go”]

if go == False:
go = True
if go == True:
go = False

Ill try to see if thats it…In a quick try it didnt work. But let me play with it

got it TY…it was as simple as the

go = ob[“Go”]

if go == False:
go = True
if go == True:
go = False

I had

 
thing = owner["Thing"]  ##Thingg being Boolean prop on obj.
 
if 'whatever event' == True:
     logic.globalDict["condition"] = True ####Activating something on another script
     thing = True
     

For some reason I must put thing = True before the globalDict variable. I moved it thing = True on top and it works fine. :smiley:

I have no Idea why

Thanks for the Help Equip

I’m glad things seem to be working but now I’m confused, maybe just tired. My script now looks circular and shouldn’t work. I think I’d forgotten that I may never have completely understood toggling boolean values with if statements. If things are working for now… fine, but I at least will have to get my head around it sooner or later.

your script is circular…
i just used the go = True, thats all i needed. Just to know how to change from True, False.

I think your overthinking : )
You were more help than you know :smiley:


import bge

# function to call with module controller
def main(cont):
    own = cont.owner

    # Toggle prop go
    own['go'] = not own['go']

    # set prop go True
    own['go'] = True

    # Test if prop go is True
    if own['go']:
         # Note: no == True is needed, go is True when it's True!

Good one Lah. Using not is quite elegant.

Still a bit confused about how to trigger it with a sensor. Like a keypress toggles it while the key is held down but returns it to it’s previous boolean value when the key is released. Maybe I’d need to resort to _JUSTRELEASED or something.

I’m sure (?) that I’ve been able to get a key or a mouseclick to add one to a value in the past. In other words trigger a controller to operate just once. More experimenting is needed to settle some simple stuff once and for all.

Normally, controllers are called twice, once on positive pulse, and once on negative pulse (but always sensors do not seam to send negative pulses). If You set pulse mode on the sensor (the … buttons) the controller are called every tick (or every n tick).

You can get the sensors from the controller object - This check the first sensor:


def main(cont):
    if cont.sensors[0].positive:
        cont.owner['go'] = True
    else:
        cont.owner['go'] = False

Or just assign the sensor.positive (it’s boolean after all):


def main(cont):
    cont.owner['go'] = cont.sensors[0].positive

But as the controller is called twice You get away with this (the .get part is if the prop don’t exist to begin with):


def main(cont):
    cont.owner['go'] = not cont.owner.get('go', False)

However, I don’t know why You want to do this - As You can simply check if the key is pressed wherever You would want to check the prop ‘go’.

Brilliant! That’s how I solved it in the past but didn’t remember what it did.

if cont.sensors[0].positive:
<indent>ob[“count”] = ob[“count”] + 1

This way the controller only acts once from a key or mouse sensor and adds 1 to the count. Without the explicit condition of the sensor being in a positive state the property will increase by one with the press and one with the release of a key or mouse button.

thanks guys

Also be aware of the increment operator:
instead of using


ob['count'] = ob['count'] + 1

You can just use:


ob['count']+=1

Yeah, thanks agoose, that’s more elegant and space saving as well.

Also, to toggle a boolean property:
use:


property = True
property = not property

print(property)
&gt;&gt;property = False