So this code had been working, I freshened it up a bit and edited some parts, and now suddenly a part of the code that wasn't having an issue is returning an error. I am unsure where the error lies. It is invalid get index '0' on base Array[node], it is an error on the first line of move_turn().

extends Node2D

@onready var arr_ship = get_tree().get_nodes_in_group("spaceships")
var i = 0

func start_turn():
	arr_ship = get_tree().get_nodes_in_group("spaceships")
	for ship in arr_ship:
		ship.movement = ship.movement_max

func move_turn():
	if arr_ship[i].active == true:
		accel.visible = true
		decel.visible = true
		turn_l.visible = true
		turn_r.visible = true
	elif arr_ship[i].active != true:
		accel.visible = false
		decel.visible = false
		turn_l.visible = false
		turn_r.visible = false
  • When move_turn() is called, either there aren't any nodes in the group "spaceships", or the array arr_ship[] hasn't been initialized yet. You could add a check:

    if arr_ship.is_empty():
        return

    Also:

    if arr_ship[i].active == true:
        ...
    elif arr_ship[i].active != true:
       ...

    can be simplified as:

    if arr_ship[i].active:
        ...
    else:
        ...

    You could combine those two suggestions:

    if not arr_ship.is_empty() and arr_ship[i].active:
        ...
    else:
        ...

When move_turn() is called, either there aren't any nodes in the group "spaceships", or the array arr_ship[] hasn't been initialized yet. You could add a check:

if arr_ship.is_empty():
    return

Also:

if arr_ship[i].active == true:
    ...
elif arr_ship[i].active != true:
   ...

can be simplified as:

if arr_ship[i].active:
    ...
else:
    ...

You could combine those two suggestions:

if not arr_ship.is_empty() and arr_ship[i].active:
    ...
else:
    ...

    @DaveTheCoder The last example isn't correct. The else branch still accesses the array. But the else branch will be entered if the not is_empty() test fails (i.e. if the array is empty).

    I would write it like this

    if not arr_ship.is_empty():
        accel.visible = arr_ship[i].active
        decel.visible = arr_ship[i].active
        turn_l.visible = arr_ship[i].active
        turn_r.visible = arr_ship[i].active

    There is really no point in going if and else, if you are just assigning a boolean value anyway.

      Zini So, the reason I had the two lines is in order to make the buttons invisible when the ship isn't active, and visible when active. If I wrote it as you suggested wouldn't it make it visible as long as arr_ship isn't empty. Which wouldn't work since there will be times it isn't empty that I don't want the buttons visible.

      • Zini replied to this.

        xRegnarokx Nope. My code does exactly what you describe. The visibility state is set to the active state (either true or false). Booleans are no different from any other variable or value. You can assign them just as easy as an int for example.

          DaveTheCoder Thanks! With the is_empty and active to combine those would mean I'd have to duplicate is_empty in order to initiate a reset func that I have in place, so for me it is best to keep those seperate but to streamline them like you suggested. Thanks so much!

          Zini The last example isn't correct. The else branch still accesses the array.

          No, the else branch does not access the array.

          Here's another way:

          var active: bool = not arr_ship.is_empty() and arr_ship[i].active
          accel.visible = active
          decel.visible = active
          turn_l.visible = active
          turn_r.visible = active
          • Zini replied to this.

            @xRegnarokx You could benefit from taking a basic programming course, preferably one that's focused on Python programming language whose syntax is very similar to GDScript. I'm saying this because it seems like you constantly struggle with fundamental logic and control flow of your program. Focusing only on those basic aspects without the burden of game engine features/concepts may lead to quicker improvement in your overall coding skills.

              Zini I understand that aspect of boolean, but the question is what makes it become invisible again? If you set visible = true, won't it just stay true?

              • Zini replied to this.

                xyz That is a good suggestion. The issue is I struggle learning when I don't have a concrete objective, and I have yet to find a programming course that does that. Also I tend to learn better by doing/seeing the results than just taking a course. But, I'll do some research to see if I can find a foundational Python course.

                • xyz replied to this.

                  xyz I'm interested on good tutorials in python as well! "automate the boring stuff with python" proceeds to not automate any boring task, and basically teach python syntax as usual. I'm interested on finding a good hands-on teaching python courses for our juniors, I did a quick search last year but was left unconvinced. Does anybody have some gold tutorials in mind? Maybe my expectations are unrealistic.

                  I love the way tiangolo documents for example. And fowler is another icon.

                  • xyz replied to this.

                    quimnuss I'm interested on good tutorials in python as well! "automate the boring stuff with python" proceeds to not automate any boring task, and basically teach python syntax as usual.

                    It's full of practical examples and the second half is literally just projects that automate boring tasks, but it's a book not a tutorial. The majority preference for "quick and easy" tutorials is precisely what got as where we are - a lot of people lacking the most fundamental knowledge while attempting to execute relatively ambitious projects.

                    There's an ages old wisdom. If you want to learn something proper - read a book about it. It takes time but it works 🙂

                      xyz aaah i see, i scrolled down to read the table of contents, which is the TOC of the udemy course, not the book! I'll definetely check it out