Jump to content

Guide to the Godot game engine/Creating resources

From Wikibooks, open books for an open world

To make a new Resource, you need to make a new script (that is not attached to a node). Here is an example:

tool
extends Resource

export (int, INT, STRING) var property_type = 0 setget set_type
var property

func _get_property_list():
  var out = [
    {
       "name": "Property",
       "type": [TYPE_INT, TYPE_STRING][property_type]
    }
  ]
  return out

func _get(what):
  if what == "Property":
    return property

func _set(what, to):
  if what == "Property":
    what = to

func set_type(value):
  property_type = value
  match value:
    0:
       if !property is int:
         property = int(property)
    1:
       if !property is String:
         property = String(property)

A simple example that shows a resource that can store either an int or a String.


The _get_property_list() must return an Array of Dictionarys, each with the following values:

  • name (String): the property's name.
  • type (int): A constant that begins with TYPE_*.

The following are optional:

  • hint (int): PROPERTY_HINT_* constants.
  • hint_string (String)
  • usage (int): PROPERTY_USAGE_* constants.

property_type is too simple to need to be changed or defined with _get_property_list(). Instead, export is used, along with setget to process when it is changed.

When property_type is set, the type of property is changed.

Real-world examples include storing keys or images. A resource just holds data for other scripts, it doesn't actually process it itself.


Guide to the Godot game engine

Getting started [edit]
Installation
What is a node?
Programming
Resources and importing
Signals and methods
Your first game
Making it work
Debugging
Input
Physics
Saving and loading
Multiplayer
Making it look good
UI skinning
Animation
Advanced help
Servers (singletons)
Platform specific
Optimisation
Encryption
Exporting
Plugins
Miscellaneous
Helpful links
Authors and contributors
Print version


<-- previous back to top next -->