Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

Klasa Wariant

O Godocie

Wariant jest najważniejszym typem danych w Godot, jest najważniejszą klasą w silniku. Wariant zajmuje tylko 20 bajtów i może przechowywać wewnątrz prawie każdy typ danych. Warianty są rzadko używanie do przechowywania informacji przez długi okres, zamiast tego są używane głównie do komunikacji, edycji, serializacji i ogólnie przenoszenia danych.

A Variant can:

  • Przechowywać prawie każdy typ danych

  • Perform operations between many variants (GDScript uses Variant as its atomic/native datatype).

  • Mieć obliczony hash, więc może być szybko porównywany z innymi

  • Jest używany do bezpiecznej konwersji między typami

  • Be used to abstract calling methods and their arguments (Godot exports all its functions through variants)

  • Be used to defer calls or move data between threads.

  • Być serializowany jako binarne dane zapisywane na dysk lub przesyłane przez sieć.

  • Be serialized to text and use it for printing values and editable settings.

  • Work as an exported property, so the editor can edit it universally.

  • Używane dla słowników, tablic, parserów itp.

Basically, thanks to the Variant class, writing Godot itself was a much, much easier task, as it allows for highly dynamic constructs not common of C++ with little effort. Become a friend of Variant today.

Odniesienia:

Containers: Dictionary and Array

Both are implemented using variants. A Dictionary can match any datatype used as key to any other datatype. An Array just holds an array of Variants. Of course, a Variant can also hold a Dictionary and an Array inside, making it even more flexible.

Modifications to a container will modify all references to it. A Mutex should be created to lock it if multi threaded access is desired.

Copy-on-write (COW) mode support for containers was dropped with Godot 3.0.

Odniesienia: