Cannot call non-static function "get()".... Make an instance instead.
Hey again, and thanks for your answers !
Using Godot 3 may be a good idea, but I can't help but think it would a waste of the acquired experience if I don't use 4.0 right from the beginning x)
I more or less solved my issue. I don't use the var Cardname
anymore, instead I write the card's name directly in CardInfo : @onready var CardInfo = CardDatabase.DATA[CardDatabase.Footman]
, yet it results in a bit of a mess later as expected (when I want to switch card, I have to modify the big CardInfo line, instead of just the var Cardname
I still have a hardtime understanding why @onready var CardInfo = CardDatabase.DATA[CardDatabase.Footman]
does work, while @onready var CardInfo = CardDatabase.DATA[CardDatabase.Cardname]
with var Cardname = "Footman"
doesn't and results in the following error : Invalid get index 'Cardname'(on base : 'Nil').
Lalo Weird. What is the output of something like this:
func _ready():
print(Cardname)
print(CardDatabase.Footman)
print(CardDatabase.Cardname)
print(CardDatabase.DATA)
print(CardDatabase.DATA[CardDatabase.Footman])
print(CardDatabase.DATA[CardDatabase.Cardname])
Lalo Using Godot 3 may be a good idea, but I can't help but think it would a waste of the acquired experience if I don't use 4.0 right from the beginning x)
Well, the only input I have on this is that I started with Godot two months ago and have only ever used version 4.1.1 and I never ran into any issues in all that time. And after doing the tutorials in the Godot docs and reading the manual (especially on GDScript) I could easily convert anything Godot 3 related that I saw in older tutorials into the proper Godot 4 form (but I am also not new to programming, so that helps). Anyways, my point is: I started with Godot 4 and I'm doing fine.
- Edited
Lalo Using Godot 3 may be a good idea, but I can't help but think it would a waste of the acquired experience if I don't use 4.0 right from the beginning x)
No, it wouldn't. It's exactly the same engine with all the same features. All the things and concepts you learn with 3.x apply directly in 4.x. There were changes in function names in GDScript in 4.x, which are rather minimal but significant enough to stutter beginners attempting to "port" tutorials as they do them.
So again, either find a 4.x tutorial or do a 3.x tutorial in 3.x. A lot less headache and confusion that way. And you want to avoid confusion when learning something.
with @onready var Cardname = "Footman"
func _ready():
print(Cardname) -> Footman
print(CardDatabase.Footman) -> 0
print(CardDatabase.Cardname) -> error : Invalid get index 'Cardname" (on base "Nil")
print(CardDatabase.DATA) -> all Data from CardDatabase.gd
print(CardDatabase.DATA[CardDatabase.Footman]) -> Footman Data only
print(CardDatabase.DATA[CardDatabase.Cardname]) -> error : Invalid get index 'Cardname" (on base "Nil")
I also tried print(CardDatabase.DATA[0])
-> Footman Data only too (which is good), and with @onready var Cardname = CardDatabase.Footman"
, print(Cardname)
-> 0, and then print(CardDatabase.DATA[Cardname])
-> Footman Data only (which is good too)
So for some reason, when Cardname = "Footman"
, CardInfo = CardDatabase.DATA[CardDatabase.Cardname]
doesn't work because Cardname = Footman (while it should be = to 0)
Or am I wrong ?
print(CardDatabase.Footman) -> 0
print(CardDatabase.Cardname) -> error : Invalid get index 'Cardname" (on base "Nil")
Does your code look exactly like this? First the first line, immediately followed by the second line? This would mean that CardDatabase is not null in the first line and somehow then turns null in the second. Either your CardDatabase does something really weird with getters or you found a bug in Godot. I don't see any other explanation how that could possibly happen.
Just to be sure, can you post your CardDatabase script?
- Edited
Hey, i just started this tutorial and found that this solution seems to work.
@onready var cardDatabase = preload("res://Assets/Cards/CardsDatabase.gd")
var cardName = 'Footman'
@onready var cardIndex = cardDatabase[cardName]
@onready var cardInfo = cardDatabase.DATA[cardIndex]
hope this helps
Okay, so what was making a mess was the Autoload on my CardDatabase.gd (yeah, forgot to mention I put this script on Autoload ><)
So, with the preload line instead of the Autoload, it works, and you don't even need the var cardIndex
(@crowing)
Soooo, the only thing that was not working well in my very first post here was actually the .get(Cardname)
in @onready var CardInfo = CardDatabase.DATA[CardDatabase.get(Cardname)]
Facepalming a lot rn >< Thanks everyone !
Well, shooting yourself in the foot is always the best way to learn.
Also, everyone started this way and also everyone still does it, the difference is just the scale.
Hey Guys,
I have started watching the tutorial on Youtube and this is where Iam with epsisode one
I get this when I run the game in 4.2.1
extends MarginContainer
# Declare member variables here. Examples:
@onready var CardDatabase = preload("res://Assets/Cards/CardsDatabase.gd")
var Cardname = 'Footman'
@onready var CardInfo = CardDatabase.DATA[Cardname]
@onready var CardImg = str("res://Assets/Cards/",CardInfo[0],"/",Cardname,".png")
# Called when the node enters the scene tree for the first time.
func _ready():
#print(CardInfo)
var CardSize = size
$Border.scale *= CardSize/$Border.texture.get_size()
$Card.texture = load(CardImg)
$Card.scale *= CardSize/$Card.texture.get_size()
var Attack = str(CardInfo[1])
var Retaliation = str(CardInfo[2])
var Health = str(CardInfo[3])
var Cost = str(CardInfo[4])
var SpecialText = str(CardInfo[6])
$Bars/TopBar/Name/CenterContainer/Name.text = Cardname
$Bars/TopBar/Cost/CenterContainer/Cost.text = Cost
$Bars/SpecialText/Text/CenterContainer/Type.text = SpecialText
$Bars/BottomBar/Health/CenterContainer/Health.text = Health
$Bars/BottomBar/Attack/CenterContainer/AandR.text = str(Attack,'/',Retaliation)
Lalo I found the issue. The Enums thing is wrong. And a bad way of doing it.
In Godot:
var d = {4: 5, "A key": "A value", 28: [1, 2, 3]}
d[4] == 5
d["A key"] == "A value"
d[28] == [1, 2, 3]
So because this guy is using enums each element of the array defaults to a basic number representing the element of the array
ex:
enum {
Bear
}
const DATA = {
Bear:
["Is deadly",5,2,10]
}
can be accessed by doing DATA[0]
Whereas
const DATA {
"Bear":
["Is deadly",5,2,10]
}
can be accessed by doing DATA["Bear"]