0 votes

I have player ( with an old diving suit ) and he has flashlight.
I have in the script working - flashlight decrease energy....but increase energy after
flashlight energy is 0 ( empty ) do'nt work.
I have Timer ( battery_time ) child node in the player parent with signal connected
Timer settings : Process Mode - Idle
Wait Time 1
One Shot false
Autostart false
Process - Pause Mode Inherit
Also I have Flashlight action controlled by two keys but one key would be better for a flashlight control ( key - F ).
Thanks for any help guys!
Player script :

extends KinematicBody

var t
var r

var pressed = false
# onready vars
onready var anim = get_node("potapec_anim")
onready var light = get_node("potapec_body/Skeleton/light_bone/potapec_light")
onready var battery = get_node("player_hud/light_battery")
onready var battery_time = get_node("battery_time")

export var motion_speed = 4
export var rotation_speed = 1



func _ready():
    set_process(true)
    set_process_input(true)
#    set_fixed_process(true)
    light.set_enabled(false)
    if pressed == false:
       anim.play("idle")


#func _input(event):
#    if(event.is_action_pressed("light_on")) && event.is_pressed():
#       light.set_enabled(true)

#    if(event.is_action_pressed("light_off")) && event.is_pressed():
#      light.set_enabled(false)


func _process(delta):
    t = get_transform()
    r = get_rotation()

# character controlls
    if(Input.is_action_pressed("ui_up")):
        t.origin += t.basis[2] * - motion_speed * delta
#        anim = ("")

    elif(Input.is_action_pressed("ui_down")):
        t.origin += t.basis[2] * motion_speed * delta
#        anim = ("")

    if(Input.is_action_pressed("ui_right")):
        r += Vector3(0,-1,0) * rotation_speed * delta

    elif(Input.is_action_pressed("ui_left")):
        r += Vector3(0,1,0) * rotation_speed * delta

    set_transform(t)
    set_rotation(r)

# switch light on
    if Input.is_action_pressed("light_on"):
       light.set_enabled(true)
       battery_time.start()

# switch light off
    if Input.is_action_pressed("light_off"):
       light.set_enabled(false)
       battery_time.stop()

# Battery full and loosing energy
func battery_full():
    var delta = get_process_delta_time()
    var value = battery.set_value(battery.get_value()) 
    if value == 100:
       value -= 20 * delta 
       battery_time.start()
       light.set_enabled(true)
       set_process_input(true)

# Recharging battery 
func battery_empty():
    var delta = get_process_delta_time()
    var value = battery.set_value(battery.get_value())            
    if value == 0:
        value += 20 * delta
        light.set_enabled(false)
        set_process_input(false)

 # Battery timeout             
func _on_battery_time_timeout():
    var value = battery.set_value(battery.get_value())
    battery.set_value(battery.get_value() -20)
    if value == 0:
        light.set_enabled(false)
        battery_empty()

Ok ....I work hard :-) and on the basis of some exaples+documentation I write a script for a flashlight.....I use TextureProgress node.

extends Node

var current_power = 100
var max_power = 100
var min_power = 0

onready var flashlight = get_node("flashlight_bar")
onready var light = get_node("light")

func _ready():
    set_process(true)
    set_process_input(true)

#func _input(event):
#   if event.is_action_pressed("flashlight"):

#   if event.is_action_released("flashlight"):


func _process(delta):
    flashlight.set_value(current_power)
    cap_energy()
    if Input.is_action_pressed("flashlight"):
        flashlight()
    else:
        current_power += 10 * delta
        light.set_blend_mode(1)
#   print(current_power)


func flashlight():
    flashlight.get_value()
    var delta = get_process_delta_time()
    if current_power > 0:
        flashlight.set_value(current_power)
        current_power -= 10 * delta
    if current_power < 0:
        light.set_blend_mode(0)

func cap_energy():
    if current_power >= 100:
        current_power = max_power
    if current_power <= 0:
        current_power = min_power

enter image description here

enter image description here

in Engine by (450 points)
edited by

what node type is for "battery"?
are you sure value of "battery" can not have below 0?

battery is ProgressBar node with min 0 and max 100...Value is 100

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.