I have some sort of bug, but I can't understand why it works so. Press mean shift gear up, - shift gear down. 0-off, 1,2,3 - shifts. thrust limit by 0 shift - 0, 1-st shift - 200, 2-nd shift - 400, 3-rd shift = 600.
Code below work proper if acceleration_time=0.5 or 1 or 1.95 or 2.1 or 3.3 Thrust don't work if acceleration_time=2 or 3 or 4.... Thrust just stay 0 and that's all. I don't understand why?!
extends Node2D
var shift=0
var thrust=0
var acceleration_time=1 #in seconds between shifts limits
var thrust_limits=[0,200,400,600] #shift0, shift1, shift2, shift3
func _process(delta):
if Input.is_action_just_pressed("throttleUP"):
if shift<3:
shift =1
if Input.is_action_just_pressed("throttleDown"):
if shift>0:
shift-=1
if thrust<thrust_limits[shift]:
thrust =200*delta*(1/acceleration_time)
if thrust>thrust_limits[shift]:
thrust-=200*delta*(1/acceleration_time)
CodePudding user response:
I think you are running against an integer division.
You are defining acceleration_time as 1 which is an int (unlike 1.0 which is a float). And you are dividing 1/acceleration_time, which would be int divided by int… and that gives you an int.
Thus, when you have acceleration_time = 2, you are doing 1/2 and that is 0, which nullifies the increment you apply to thrust.
You can use literals with a decimal point for float: 1.0/acceleration_time or you can type your variables:
var acceleration_time:float = 1
Or:
var acceleration_time:float = 1.0
Or:
var acceleration_time := 1.0
Which types the variable with the type of what you used to initialize it.
Yes, GDScript has types. Use them. These are not just hints. I repeat: these are not just hints.
