godotengine / godotengine/godot

Modifying a static variable in a extended script modifies the parent script static variable instead

Open
#87,629 1 comment 2 reactions 0 assignees View on GitHub
discussion topic:gdscript
Dominant language
C++
Stars
117k
Forks
26.8k
PR merge metrics
PR metrics pending

Description

### Tested versions

- Reproducible since 4.1-dev2

### System information

Godot v4.3.dev2 - Windows 10.0.22621 - Vulkan (Forward+) - dedicated NVIDIA GeForce GTX 1650 SUPER (NVIDIA; 31.0.15.4617) - Intel(R) Core(TM) i5-9400F CPU @ 2.90GHz (6 Threads)

### Issue description

The way static variables are handled in GDScript differ from other programming languages, where they're not inherited to children GDScript classes. Instead, the parent class controls the static variables of all children.

### Steps to reproduce

The MRP has the following structure:

**Parent.gd**
```
class_name Parent
extends Node

static var PROPERTY_1 : int
static var PROPERTY_2 : String
```

**Child1.gd**
```
class_name Child1
extends Parent

static func _static_init():
PROPERTY_1 = 10
PROPERTY_2 = "Child1"
```

**Child2.gd**
```
class_name Child2
extends Parent

static func _static_init():
PROPERTY_1 = 5
PROPERTY_2 = "Child2"
```

**Main.gd**
```
extends Node2D

func _ready():
print(Child1.PROPERTY_1)
print(Child1.PROPERTY_2)

print(Child2.PROPERTY_1)
print(Child2.PROPERTY_2)
```

Main.gd is included in the main scene.
The assumption goes that the result in the console would look like this
```
10
Child1
5
Child2
```
But instead it prints the following.
```
5
Child2
5
Child2
```

### Minimal reproduction project (MRP)

[MRP.zip](https://github.com/godotengine/godot/files/14068840/MRP.zip)

Contributor guide

Open the contributing guide

Research direction

Run the attached MRP and inspect Parent.gd, Child1.gd, Child2.gd, and Main.gd to reproduce the shared static-variable output. Determine and document the intended behavior for static variables in extended GDScript classes, then verify that the result matches the stated expected output for both children.

Written by the indexing model from the issue text.

Assessment

Domain
game-dev
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.