Unfixable syntax errors Godot 4

88 Views Asked by At

I used ChatGPT to generate a script as I cannot code very well yet it does not know Godot 4 even exists yet so it gives me Godot 3 code, I fixed a lot of things that don't work in Godot 4 and found newer equivalents that will work but this is the one thing I can't figure out at all.

This is the snippet of my script I believe is broken:

extends CharacterBody3D

# Structure to represent an inventory slot
class InventorySlot:
    var background: TextureRect
    var item_icon: TextureRect

    # Constructor for InventorySlot
    func _init():
        background = TextureRect.new()
        item_icon = TextureRect.new()

# Container for inventory slots
class InventorySlotContainer(Node):
    var inventory_slots: Array[InventorySlot]

    # Constructor for InventorySlotContainer
    func _init():
        inventory_slots = []

    # Function to add an inventory slot
    func add_inventory_slot():
        var slot = InventorySlot.new()
        inventory_slots.append(slot)
        add_child(slot.background)
        add_child(slot.item_icon)

# Declare the inventory container
var inventory_container: InventorySlotContainer

@onready var inventory_slot_container = InventorySlotContainer.new()

# Declare the inventory slots
var inventory_slots: Array[InventorySlot]

I have tried changing lots of things but to no avail I get these errors:

Line 14:Expected ":" after class declaration.
Line 14:Unexpected "(" in class body.
Line 15:Unexpected "Indent" in class body.
Line 29:Expected end of file.
1

There are 1 best solutions below

0
On BEST ANSWER

On line 14 the inner class "inventorySlotContainer" has bad syntax. The error says it was expecting ":" but found "(". There is no syntax for inner-classes to utilize parentheses.

#inner-class declaration 
class className:
   ...

See Godot's docs on inner-classes https://docs.godotengine.org/en/4.1/tutorials/scripting/gdscript/gdscript_basics.html#inner-classes

Change "class InventorySlotContainer(Node):" To "class InventorySlotContainer:"