mirror of
https://github.com/stan220/godot-docs.git
synced 2026-09-08 18:29:11 +00:00
Step by Step: Resources -> Remove outdated info
Dictionaries are refcounted now, this closes https://github.com/godotengine/godot-docs/issues/1962
This commit is contained in:
@@ -180,38 +180,6 @@ and :ref:`Resource <class_Resource>` features:
|
||||
|
||||
- They can extend **other** resource types besides just the base Resource.
|
||||
|
||||
.. warning::
|
||||
|
||||
Resources and Dictionaries are both passed by reference, but only Resources are
|
||||
reference-counted. This means that if a Dictionary is passed between objects and
|
||||
the first object is deleted, all other objects' references to the Dictionary will
|
||||
be invalidated. Conversely, Resources will not be freed from memory until *all* the
|
||||
objects are deleted.
|
||||
|
||||
.. tabs::
|
||||
.. code-tab:: gdscript GDScript
|
||||
|
||||
extends Node
|
||||
|
||||
class MyObject:
|
||||
extends Object
|
||||
var dict = {}
|
||||
|
||||
func _ready():
|
||||
var obj1 = MyObject.new()
|
||||
var obj2 = MyObject.new()
|
||||
obj1.dict.greeting = "hello"
|
||||
obj2.dict = obj1.dict # 'obj2.dict' now references 'obj1's Dictionary.
|
||||
obj1.free() # 'obj1' is freed and the Dictionary too!
|
||||
print(obj2.dict.greeting) # Error! 'greeting' index accessed on null instance!
|
||||
|
||||
# To avoid this, we must manually duplicate the Dictionary.
|
||||
obj1 = MyObject.new()
|
||||
obj1.dict.greeting = "hello"
|
||||
obj2.dict = obj1.dict.duplicate() # Now we are passing a copy, not a reference.
|
||||
obj1.free() # obj2's Dictionary still exists.
|
||||
print(obj2.dict.greeting) # Prints 'hello'.
|
||||
|
||||
Godot makes it easy to create custom Resources in the Inspector.
|
||||
|
||||
1. Create a plain Resource object in the Inspector. This can even be a type that derives Resource, so long as your script is extending that type.
|
||||
|
||||
Reference in New Issue
Block a user