what's wrong with my code?

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

Look, I’m an amateur, I used a tutorial to create the health, I wanted to add an ammo system so I figured I could do it the same way the health system works but I keep getting an error message "Invalid type in built function clamp cannot convert object 1 from object to float On the line that says “amseg = clamp(value, 0, max_amseg)”, I don’t know how to fix this or make it work, can someone tell my broken ape brain what’s wrong and how to make it work?

extends Control

var segments = 100 setget set_segments
var max_segments = 100 setget set_max_segments
var amseg = 30 setget set_amseg
var max_amseg = 30 setget set_max_amseg

onready var label = $Label
onready var amlab = $Label2

func set_amseg(value):
amseg = clamp(value, 0, max_amseg)
if amlab != null:
amlab.text ="Ammo: " + str(amseg)

func set_max_amseg(value):
max_amseg = max(value, 1)

func set_segments(value):
segments = clamp(value, 0, max_segments)
if label != null:
label.text = "HP: " + str(segments)

func set_max_segments(value):
max_segments = max(value, 1)

func _ready():
self.segments = PlayerStats.max_health
self.segments = PlayerStats.health
self.amseg = PlayerStats.max_ammo
self.amseg = PlayerStats
PlayerStats.connect(“health_changed”, self, “set_segments”)
PlayerStats.connect(“ammo_changed”, self, “set_amseg”)

:bust_in_silhouette: Reply From: Regulus

It seems in your _ready() function you have the line:

self.amseg = PlayerStats

which is setting amseg to an Object (which is causing the error).

I was able to fix it by replacing that line with:

self.amseg = PlayerStats.ammo

or with whatever your ammo variable is called.

thank you so much, I’m a forgetful idiot

IVTheSimple | 2022-09-05 19:08