godotengine / godotengine/godot

Having free() in a script in the scene tree will throw an error in exported game

Open
#101,124 4 comments 1 reaction 0 assignees View on GitHub
bug topic:core topic:gdscript
Dominant language
C++
Stars
117k
Forks
26.8k
PR merge metrics
PR metrics pending

Description

### Tested versions

Reproducible in 4.3 stable and 4.2.2 stable

### System information

Windows 11, macOS Ventura

### Issue description

If a script has free() anywhere in it (test.gd in this case) and is loaded into the scene tree via a node, the log will show the following error.

USER SCRIPT ERROR: Parse Error: Function "free()" not found in base self.
at: GDScript::reload (res://test.gd:20)
ERROR: Failed to load script "res://test.gd" with error "Parse error".
at: load (modules/gdscript/gdscript.cpp:2936)

It won't crash the exported game, but any node that has this and is added to the scene tree will just be added and not function. In the Editor, this runs normally with no issues or errors.

However, using free() in an unsafe line such as part of a "for loop" doesn't throw this error in the exported game. Example below:

```
for child in $SomeNode.get_children():
child.free()
```

### Steps to reproduce

1. Create a new script for any Node
2. Put the free() method to be called anywhere in the script
3. Have a function that will add this node to the scene tree (it can even be in the main scene script itself)
4. Export the game and run it (be sure to have file logging on)
5. Check the logs to see the parse error

### Minimal reproduction project (MRP)

MRP can be recreated easily using the steps above.

Contributor guide

Open the contributing guide

Research direction

Reproduce the exported-game failure using the described scene-tree steps and compare it with the editor behavior. Start at modules/gdscript/gdscript.cpp:2936 and use test.gd with a direct free() call; done means the script loads and the node functions in the exported game without the parse error.

Written by the indexing model from the issue text.

Assessment

Domain
compilers, game-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.