Resource properties in 1.2.163

We will release a new feature called resource properties in Defold 1.2.163. It has been hinted at many times on the forum and now it’s finally time to release it! Resource properties expands on the existing script properties system and it allows you to define a new set of script properties that can be used to reference resources. Most of you are probably familiar with this:

go.property("hostile", true)
go.property("speed", 42)
go.property("direction", vmath.vector3(1, 0.3, 0.0))
go.property("color", vmath.vector4(1.0, 1.0, 0.0, 0.5))
go.property("attack", hash("slash"))
go.property("trigger", msg.url("some:/other#system"))

New script property types are:

go.property("jungle", resource.tile_source())
go.property("glow", resource.material())
go.property("alert", resource.font())
go.property("light", resource.texture())
go.property("weapons", resource.atlas())

They will show up in the properties panel as resource fields where you can browse for the specific resource type among your project assets:

47
You use them like this:

-- change sprite, tilemap, model, spinemodel and gui material
go.set("#sprite", "material", self.glow)
go.set("#tilemap", "material", self.glow)
go.set("#model", "material", self.glow)
go.set("#spinemodel", "material", self.glow)
go.set("#gui", "material", self.glow)

-- change sprite image
go.set("#sprite", "image", self.jungle)
go.set("#sprite", "image", self.weapons)

-- change tilemap tilesource
go.set("#tilemap", "tile_source", self.jungle)

-- change label font
go.set("#label", "font", self.alert)

-- change model texture
go.set("#model", "texture0", self.light)

If you’d like to play around with this feature before release it is available on the alpha channel:

31 Likes

NOTE! We recommend that you install the alpha version of the editor in a separate folder instead of replacing your existing installation.

Also worth pointing out is that you can provide default values:

go.property("material", resource.material("/assets/materials/foo.material"))
3 Likes

Awesome. Thanks. ))
Can I change that property from other go context? go.set("/another_go#sprite",“image”, self.atlas)? Or only inside current go?

1 Like

You should be able to do it from another go in the same collection.

5 Likes

Wow, so many possibilities! :heart:

1 Like

Oh dear… I’m crying now :smiley:

Is it on your radar somehow to be able to specify a list of these resources and values, so that the UI presents a dropdown menu? :wink:

4 Likes

No, I’m afraid not.

1 Like

Owwwww.

I’ve submitted a feature request for that just in case

Thanks.

3 Likes

These are really cool and useful! Can references to resources be passed around to other script instances? Like being stored in a table or sent through a message?

3 Likes

:open_mouth: Awesome :open_mouth:
Defold is getting even more fun every moment :grinning:

Is it possible to include lua modules as resource properties too? Will be very useful to make game templates for Defold. :smiley:

1 Like

Oh! They’re implemented as hashes, so yes, they can be passed around anywhere. Cool!

3 Likes

Currently not possible. It doesn’t really fit with how these resources are referenced by the script property system and how they are assigned/set on components. Perhaps we could do something with script files though.

2 Likes

How does the lifecycle of resources work? They’re loaded the moment a collection containing a component/script with the resource assigned on a property is loaded, right? And then ref-counted as usual. So if we want to selectively load resources, we still have to use dynamically loaded factories.

1 Like

I’m not 100% sure of the implementation details, but what you describe sounds accurate. @Mathias_Westerdahl or @jhonny.goransson knows more.

1 Like

Correct, the resource properties are just hashed paths to the resources they bring into the game.
And currently, there’s no way to increment/decrement resource when using these.
We might implement such a feature in the future though.

5 Likes

Yes, but I assume that if I set an atlas on a sprite, that will increase the refcount of the atlas until the sprite gets deleted or another image gets set. In which case, you can still create a script with a resource from a factory, assign that resource to a sprite and then immediately destroy the game object with the script, without affecting the sprite. For now, I don’t feel the need to manage the refcount manually.

1 Like

If the resources are in the same collection, they are already ref-counted. As you said, they’re hashes, not actual objects.

The mechanism I was referring to was if you’d want to load a collection, grab a reference to one of its resources, then unload the collection.

Yeah, but you still can have the same mechanism with dynamically loaded factories (And it seems to work from what I tested). factory.load(), then factory.create(), then set the resource on a sprite external to the factory, then delete the GO created by the factory and factory.unload().

In the latest 1.2.163:
58
if test.script contains non-empty resource property (material), I get this:
18

I’m not an expert on this topic, but quick question, what does the About dialog say? What is the engine and editor hashes?