1
$\begingroup$

So i'm making a game in bge and i've came across a problem. It's a driving game and i noticed for turning if i just put a certain number for rotation in the actuator the steering would be messed up. So i tried coding it and came across a problem. Idk how to make the rotation speed go up slowly. sorry. Code:

import bge
cont = bge.logic.getCurrentController()
own = cont.owner
#Sensors
leftSensor = cont.sensors["Left"]
rightSensor = cont.sensors["Right"]
#Actuator
turningSensor = cont.actuators["Turning"]
turning = 0 
if leftSensor.positive:
    turning = turning - .03
if rightSensor.positive:
    turning = turning + .03
own.applyRotation((0, 0, turning))

I have tried using a while loop instead of a if so it will loop(like i want it to) but a while command will completly crash bge. How do i do this

$\endgroup$
3
  • 1
    $\begingroup$ Do not use use loops to describe timed sequences. This will cause your put your game on hold until your code finishes the loop. Your controller should describe how to perform a single step rather than the whole journey. $\endgroup$
    – Monster
    Commented Aug 19, 2016 at 3:48
  • $\begingroup$ @Monster ...what? $\endgroup$
    – gamer103
    Commented Aug 19, 2016 at 21:22
  • 1
    $\begingroup$ Simply do not use "while" (until you understand the implications). $\endgroup$
    – Monster
    Commented Aug 22, 2016 at 5:40

2 Answers 2

1
$\begingroup$

Timing

A logic brick represents the behavior of a single step. This step should be as fast as possible. It does not describe behavior that lasts longer than a single frame.

Therefore you should not try to describe a whole journey unless you want to finish it with one step. Even with that it has to be a fast calculation.

Bricks and Python

Your code reads an actuator but does not use it. Either the code is obsolete or it is missing something. I guess your original design was to reconfigure the actuator's parameter. Later you decided to use the applyRotation() making the actuator to act as actuator. Both ways are possible.

They do not make a difference regarding your question.

Model

A model describes the inner status of a specific aspect. In your case the aspect is how much to turn. So the model is "turning".

( A model can be much more complex. e.g. it can describe the status of a complete car including, speed, position, if doors are open, what lights are on ...).

Here the model describes how much to turn (single value).

Lifetime

You chose a variable as model. Please be aware that a variable exist only with the current processing of the code. It will not survive the exit of the controller. In other words you get a new variable at each single frame.

Thinking about a car model: your get a brand new car with each single frame . (This might be a nice idea at the first sight, but it the car is always located at the car dealers place, without gas ).

You can see that at the console when you add print(turning).

It is better to keep the same model as long as the car exist. You need a model that survives when the controller's completed it's processing at the current frame.

There are several different ways to achieve that.

Property as storage

I suggest you use an object property. It will bind the "turning model" to the game object that performs turning. This allows different game object that all can perform individual turns.

The change to your code is minimal but has major impact:

import bge
controller = bge.logic.getCurrentController()
owner = controller.owner

PROPERTY_TURNING = "turning"

#Sensors
leftSensor = controller.sensors["Left"]
rightSensor = controller.sensors["Right"]

turning = owner.get(PROPERTY_TURNING , 0.0) # get your model from storage

if leftSensor.positive:
    turning = turning - .03
if rightSensor.positive:
    turning = turning + .03

owner[PROPERTY_TURNING] = turning # update the storage with the modified model

own.applyRotation((0, 0, turning))

I hope it helps

$\endgroup$
4
  • $\begingroup$ I'm sorry i don't think i worded this question right. what i want is the person to just be able to hold down the key for the rotation to happen not for u to have to spam the turn key $\endgroup$
    – gamer103
    Commented Aug 19, 2016 at 21:27
  • 1
    $\begingroup$ This code will process "turning" as long as one of the sensors is positive. Ensure the sensors remain positive as long as you hold the key (=no TAP!). $\endgroup$
    – Monster
    Commented Aug 22, 2016 at 5:38
  • $\begingroup$ ...have u legitimately tried this out. for me i have to tap everytime. wait. sorry. i'm a newb at blender since i have like no experience. u mentioned a object property. is this something i have to set up outside of the script? $\endgroup$
    – gamer103
    Commented Aug 22, 2016 at 21:57
  • 1
    $\begingroup$ This depends on your sensor configuration. You need to enable [True Level Triggering] otherwise the controller runs only when you press a key or release it. $\endgroup$
    – Monster
    Commented Aug 23, 2016 at 5:57
-1
$\begingroup$

+= is your friend. something like turning += 0.3 will incrementally keep adding 0.3 atop of 0.3 on and on until the script stops.

$\endgroup$
3
  • 1
    $\begingroup$ a += 1 is the same as a += 1. There is no functional difference. $\endgroup$
    – Monster
    Commented Aug 19, 2016 at 3:49
  • 1
    $\begingroup$ "a += 1 is the same as a += 1." ok.... $\endgroup$ Commented Aug 19, 2016 at 4:29
  • 1
    $\begingroup$ Sorry i meant a=a+1 is the same as a+=1 :D $\endgroup$
    – Monster
    Commented Aug 19, 2016 at 4:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .