Hello, can a good will explain me what's wrong with my code here, since I feel it's toddler level and I'm unable to complete it, here is my code:
extends GridContainer func _ready() -> void: for i in 25: print(i) add_inventory_item() func add_inventory_item() -> void: # Create an instance of InventoryItem.tscn. # Add it as a child of this node by calling the add_child() function. var item := preload("InventoryItem.tscn").instance() add_child(item)
And, the test fail since it tells (and I see) 26 elements...
I can solve it by putting "for in 24"... but it makes no sense to me
Also, does anyone knows if the preload is no-op after the first time ? It sounds utterly strange to me to preload (not writting "preload", but really, telling the engine to get the scene from where it is, or reuse it) 25 times instead of loading it once
Thanks a lot
No need to feel dumb, everyone starts somewhere, and we all learn at our own pace! Take it from me - my published games only came to fruition because of determination and creativity haha.
EDIT - See Nathan's answer below:
As for your question, I'm not sure on the preload bit, but you're getting 26 (I'm nearly certain) because your for loop iterates from 0 to 25, with 0 counting, resulting in 26 (often called zero-based indexing). You'll see this absolutely everywhere in programming, and eventually get used to it.
Did you add any item to the game's scene? Like so:
If so, this is where your 26th item would come from. The code is correct.
Then, anything loaded with preload() is only loaded once at compile time (upon parsing/reading the script's code). Then Godot internally replaces the call to preload with a direct reference to the loaded resource to maximize performance.
GGemfruit when iterating over a range, numbers range from 0 to the number you picked - 1, so for i in 25 will loop 25 times.
Thanks for your answers,
GGemfruit that's why I added the print(i) to check if there was really only 25 iteration of the loop (and yes, it goes from 0 to 24), I even tried to replace it with for i in range(25) (because I did not know of the for i in 25 but it does not change anything)
I don't think having added something, but you're right, even with the loop being empty (0 iteration), I have one element... I've tried to "revert" multiple times, but it does not change anything, I guess I'll try to re-import from the full practice from start...
Thanks a lot for the confirmation it's not inherent to the code (you know how it goes, sometime there is a mistake you read twenty billion time the same until someone just point out what you read is not what you wrote)
GGemfruit I just saw your edit, I think GDScript is like python, the upper bound is not included so, for i in 3 is like for i in range(3) and results in 0 1 2, thank you for your explanations
I mark your answer as correct since replacing the scene file Inventory.tscn (even if I can not tell what's the difference by the pristine one solve the project)
[ext_resource path="res://Scoreboard/Inventory/Inventory.gd" type="Script" id=1] [ext_resource path="res://Scoreboard/Inventory/InventoryItem.tscn" type="PackedScene" id=2] ... [node name="InventoryItem" parent="." instance=ExtResource( 2 )] anchor_right = 0.0 anchor_bottom = 0.0 margin_right = 140.0 margin_bottom = 140.0
I don't remember having done anything, but I'm pretty sure it did not happens itself, so... I guess I clicked somewhere I should not, and I guess the problem is the InventoryItem reference (really have to work waaaay more with Godot to avoid making mistakes)
Once again, thanks a lot to you two for your answers
ppotens I've heard GDScript and Python have a lot of similarities, and learning Python has been on my todo list for awhile, so I recently made the choice to learn GDScript and leave C# behind for a bit. That, and the tighter integration is always nice. It'll take some getting used to, but I'm looking forward to it!
If you already know a language or two, you'll fly in Python, I don't know any language easier than that (not to master, but to study and be efficient at it). The difficulty I can foresee is, learning GDScript and Python at the same time and not mix them (similar, but different)