Invalid get index 'self' (on base: 'Node (stats.gd)').
Asked Answered
C

14

0

I'm trying to make sure that my 'Hearts' counter is able to update, however when I try to load the game all I get is this error
Invalid get index 'self' (on base: 'Node (stats.gd)').

Here is my code and thank you in advance for your help!

extends Control

@onready var label = $Label

var hearts = 4:
	get:
		return hearts
	set(value):
		hearts = clamp(value, 0, max_hearts)

var max_hearts = 4:
	get:
		return max_hearts
	set(value):
		max_hearts = max(value, 1)

func _ready():
	self.max_hearts = PlayerStats.max_health
	self.hearts = PlayerStats.health
	self.PlayerStats.connect("health_changed","set_hearts")
Crissycrist answered 8/12, 2023 at 21:51 Comment(0)
O
0

Crissycrist Which line generates the error?

Omnibus answered 8/12, 2023 at 22:19 Comment(0)
M
0

is PlayerStats an autoload?
I guess the last line should be:

PlayerStats.connect("health_changed","set_hearts") as there is no PlayerStats inside your Control Node.

Malliemallin answered 8/12, 2023 at 22:22 Comment(0)
C
0

Omnibus

The line

self.PlayerStats.connect("health_changed","set_hearts")

seems to be giving the error

Crissycrist answered 9/12, 2023 at 2:8 Comment(0)
O
0

Crissycrist See Transliterate's answer.

Omnibus answered 9/12, 2023 at 2:11 Comment(0)
C
0

Malliemallin
When I tried this it gave this error instead Parser Error: Cannot pass a value of type "String" as "Callable".
PlayerStats is a variable from my 'player' scene and the tutorial I was using didn't say to make it an autoload and there was no one in the comments saying they ran into similar problems either.

Crissycrist answered 9/12, 2023 at 2:11 Comment(0)
O
0

Crissycrist connect() takes function reference (Callable) as the second argument, not function name string.

Omnibus answered 9/12, 2023 at 2:36 Comment(0)
C
0

Omnibus
When I do PlayerStats.connect("health_changed","set_hearts") though, it still causes my game to not function, so I still am not sure how to fix my code so that it stops my game from crashing on launch.

Crissycrist answered 9/12, 2023 at 2:59 Comment(0)
L
0

Why aren't you using the Signal class method for connecting a signal?

Lorielorien answered 9/12, 2023 at 4:30 Comment(0)
C
0

Lorielorien It's not a signal that I'm trying to connect. I'm trying to connect the PlayerStats of the player to "health_changed" and "set_hearts" and ultimately have them display the correct data in my control node, but I might have messed up my setget due to my tutorial being outdated.

Crissycrist answered 9/12, 2023 at 4:54 Comment(0)
L
0

That is a signal.

Lorielorien answered 9/12, 2023 at 5:4 Comment(0)
C
0

Lorielorien Okay- well- I'll take that L.
All in all, what I am using did not ask me to go into node and set up a signal, it told me to use connect() but it gives the error of 'Invalid get index 'PlayerStats' (on base: 'Control (health_ui.gd)').' and I do not know if something in this line in particular, my setget, or a piece of code in a different scene are the issue or not (this one seems least likely since the only scene it calls is a label).

Crissycrist answered 9/12, 2023 at 5:25 Comment(0)
O
0

Crissycrist Looks like you have some fundamental misunderstanding of how things work. You say you're not trying to connect a signal yet connect() method's only purpose is to connect signals. It's hard to help you without seeing the whole context. The code you posted is not sufficient as we don't exactly see what PlayerStats, health_changes and set_hearts are, and where are they situated.

Try to describe your intent and the existing situation in more detail - post all involved scripts and snapshots of relevant scene hierarchies.

Also when citing errors, always copy-paste the exact error message and specify at which line in which script it occurs.

Otherwise, you're wasting yours and everyone else's time.

Omnibus answered 9/12, 2023 at 9:15 Comment(0)
C
0

Crissycrist

After getting some help from a friend of mine, she helped me learn that the solution to this problem was

PlayerStats.health_changed.connect(set_hearts)
Crissycrist answered 9/12, 2023 at 20:46 Comment(0)
L
0

I gave you an example like that in your previous post:
https://mcmap.net/q/322/godot-need-help-with-getting-my-stats-to-connect-so-my-player-can-die

Lorielorien answered 9/12, 2023 at 20:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.