why my lifetime var didn't work?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By potatobanana

my var lifetime didn’t work, i dont know why, no error.
here the video

:bust_in_silhouette: Reply From: Dlean Jeans

Check the bullet lifetime in the player scene. From experience I believe it stays the same after you change it in the bullet scene itself.

If so, try setting the lifetime to a default value:

export(float) var lifetime = 1

Then press reset the lifetime value in the Inspector, both in the bullet and player scene.

i dont have lifetime in player, just in player_bullet.
i try put export(float) var lifetime = 1 and reset like you say, but still didn’t work. i also try

$lifetime.wait_time = 0.3

but still wont work, look like i cant even change the value from code .why bullet_speed that use same concept work just fine =.="

i also use

print($lifetime.wait_time)

it show the right value i change, like 0.3. but the lifetime itself still at 1

here file if you see more topdown tank file

potatobanana | 2020-02-20 12:01

:bust_in_silhouette: Reply From: njamster

It’s not clear from the video, but I assume you call start_position after you created the bullet instance and added it to the tree? If so, changing the value of wait_time won’t do anything, because the timer is already running (as autostart is set to true). Try setting time_left instead. Or simply restart the timer after setting ẁait_time.

i call start_position in map scene

func on_tank_shoot(bullet,_position,_rotation):
	var instance_bullet = bullet.instance()
	add_child(instance_bullet)
	instance_bullet.start_position(_position,_rotation)

here i restart as you say, and it work, but i have no idea why it work. can you explain to me ?

func start_position(_position,_direction):
	position = _position
	rotation = _direction.angle()
	$lifetime.set_wait_time(lifetime)
	$lifetime.start()
	velocity = _direction * speed

here file project from google

potatobanana | 2020-02-20 19:14

Imagine your Timer is a boomerang. You can throw it with varied strength, which will determine how long it is away before returning to you. That’s your wait_time. Once you throw the boomerang away (i.e. start the timer), you have to decide for a strength/wait_time. If you change your mind after that point and shout out: “No, wait, actually do this in 0.1 seconds instead!!!”, the boomerang doesn’t care. However, in Godot you’re a wizard and can teleport the boomerang back into your hand at any time! Which gives you another opportunity to throw it and correct your mistake. That’s what restarting the timer does. Setting the wait_time only equals a change of mind, that will affect future throws of the boomerang, not the one currently in progress.

Alternatively you could call start_position before you add the bullet as a child. This way you would first set the wait_time and then start the timer, as the autostart only takes place after the node has been inserted into the tree. Now that I think about it, that’s probably the better approach anyway. But the principle remains the same!

njamster | 2020-02-21 11:26

i hope i near future all people will explain like you do,it make me understand much better. thank you. lets hope godot explain like you do too.

potatobanana | 2020-02-21 13:10