0

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
  • Far too much code. Please spend the time taking the tour and reading the help center pages, especially How to Ask and minimal reproducible example, and then edit your code to provide the absolutely fewest lines of code possible to demonstrate the issue. Also, please rewrite your title to describe the actual problem you're having or question you're asking. What's wrong with my code? is absolutely useless, as is asking that we Please help. It's clear that you need help, or you wouldn't be posting here, and it's up to YOU to tell us what's wrong with your code, and then we'll try to help you fix it. Commented Oct 27, 2023 at 3:07

1 Answer 1

0

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:"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.