Welcome!

You are just one step away from creating awesome games!

You need a Google account to use Defold

- Select ref document -

Resource API documentation

version 1.2.117

Functions and constants to access resources.

Functions

resource.get_current_manifest

resource.get_current_manifest()

Return a reference to the Manifest that is currently loaded.

Returns

manifest_reference

number reference to the Manifest that is currently loaded

manifest_reference

number reference to the Manifest that is currently loaded

resource.load

resource.load(path)

Loads the resource data for a specific resource.

Parameters

path

string | hash The path to the resource

path

string | hash The path to the resource

Returns

buffer

buffer Returns the buffer stored on disc

buffer

buffer Returns the buffer stored on disc

Examples

-- read custom resource data into buffer
local buffer = resource.load("/resources/datafile")

In order for the engine to include custom resources in the build process, you need to specify them in the "game.project" settings file:

[project]
title = My project
version = 0.1
custom_resources = resources/,assets/level_data.json

resource.set

resource.set(path, buffer)

Sets the resource data for a specific resource

Parameters

path

string | hash The path to the resource

buffer

buffer The buffer of precreated data, suitable for the intended resource type

path

string | hash The path to the resource

buffer

buffer The buffer of precreated data, suitable for the intended resource type

Examples

Assuming the folder "/res" is added to the project custom resources:

-- load a texture resource and set it on a sprite
local buffer = resource.load("/res/new.texturec")
resource.set(go.get("#sprite", "texture0"), buffer)

resource.set_texture

resource.set_texture(path, table, buffer)

Sets the pixel data for a specific texture.

Parameters

path

hash | string The path to the resource

table

table A table containing info about the texture. Supported entries:

type
number The texture type. Supported values:
  • resource.TEXTURE_TYPE_2D
width
number The width of the texture (in pixels)
height
number The width of the texture (in pixels)
format
number The texture format. Supported values:
  • resource.TEXTURE_FORMAT_LUMINANCE
  • resource.TEXTURE_FORMAT_RGB
  • resource.TEXTURE_FORMAT_RGBA
buffer

buffer The buffer of precreated pixel data

Currently, only 1 mipmap is generated.

path

hash | string The path to the resource

table

table A table containing info about the texture. Supported entries:

type
number The texture type. Supported values:
  • resource.TEXTURE_TYPE_2D
width
number The width of the texture (in pixels)
height
number The width of the texture (in pixels)
format
number The texture format. Supported values:
  • resource.TEXTURE_FORMAT_LUMINANCE
  • resource.TEXTURE_FORMAT_RGB
  • resource.TEXTURE_FORMAT_RGBA
buffer

buffer The buffer of precreated pixel data

Currently, only 1 mipmap is generated.

Examples

How to set all pixels of an atlas

function init(self)
  self.height = 128
  self.width = 128
  self.buffer = buffer.create(self.width * self.height, { {name=hash("rgb"), type=buffer.VALUE_TYPE_UINT8, count=3} } )
  self.stream = buffer.get_stream(self.buffer, hash("rgb"))

  for y=1,self.height do
      for x=1,self.width do
          local index = (y-1) * self.width * 3 + (x-1) * 3 + 1
          self.stream[index + 0] = 0xff
          self.stream[index + 1] = 0x80
          self.stream[index + 2] = 0x10
      end
  end

  local resource_path = go.get("#sprite", "texture0")
  local header = { width=self.width, height=self.height, type=resource.TEXTURE_TYPE_2D, format=resource.TEXTURE_FORMAT_RGB, num_mip_maps=1 }
  resource.set_texture( resource_path, header, self.buffer )
end

resource.store_resource

resource.store_resource(manifest_reference, data, hexdigest, callback)

add a resource to the data archive and runtime index. The resource will be verified internally before being added to the data archive.

Parameters

manifest_reference

number The manifest to check against.

data

string The resource data that should be stored.

hexdigest

string The expected hash for the resource, retrieved through collectionproxy.missing_resources.

callback

function(self, hexdigest, status) The callback function that is executed once the engine has been attempted to store the resource.

self
object The current object.
hexdigest
string The hexdigest of the resource.
status
boolean Whether or not the resource was successfully stored.
manifest_reference

number The manifest to check against.

data

string The resource data that should be stored.

hexdigest

string The expected hash for the resource, retrieved through collectionproxy.missing_resources.

callback

function(self, hexdigest, status) The callback function that is executed once the engine has been attempted to store the resource.

self
object The current object.
hexdigest
string The hexdigest of the resource.
status
boolean Whether or not the resource was successfully stored.

Examples

function init(self)
    self.manifest = resource.get_current_manifest()
end

local function callback_store_resource(self, hexdigest, status)
     if status == true then
          print("Successfully stored resource: " .. hexdigest)
     else
          print("Failed to store resource: " .. hexdigest)
     end
end

local function load_resources(self, target)
     local resources = collectionproxy.missing_resources(target)
     for _, resource_hash in ipairs(resources) do
          local baseurl = "http://example.defold.com:8000/"
          http.request(baseurl .. resource_hash, "GET", function(self, id, response)
               if response.status == 200 then
                    resource.store_resource(self.manifest, response.response, resource_hash, callback_store_resource)
               else
                    print("Failed to download resource: " .. resource_hash)
               end
          end)
     end
end

Constants

resource.TEXTURE_FORMAT_LUMINANCE

resource.TEXTURE_FORMAT_RGB

resource.TEXTURE_FORMAT_RGBA

resource.TEXTURE_TYPE_2D


I want to report anonymously.