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
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.
‘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
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.
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.
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.
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.
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!
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:
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.