How to get Sprite from the GO from the file? (SOLVED)

So i wrote

local component = "#Card"
component.go.get("#card-tile","size.x")

but it is wrong, i tryed somthing like that:

local z = msg.post("Card", "go.get(#card-tile, size.x)")

or

local s = go.get("#Card", "Card/#card-tile")

I just need to get Width of sprite on GO that contains on file (not spawned)

The API docs contains an example:

yes, but it if you already have GO. But i need to get GO firstly from file, and after get sprite property, and i don’t know how to find GO from files of my project

I don’t understand the question. Do you have the game object added to a collection? Or is it spawned by a factory? All game objects regardless of where they come from have a unique id and URL.

Have you read the Addressing manual?

Yes i read, but if i won’t to get sprite width of the object that is not spawned by factory and don’t exiest on collection. That GO exiest just in the file, but he have an adress.
image
and i won’t to spawn 3x3 Cards


But i need the width of the Card sprite for coordinates manipulations. And i just wan’t to get them.

You need an instance of Cards.go to read any properties. The Asset panel to the left can contain any number of assets but it is only those that are added somewhere in the scene graph in the editor or spawned at run-time that are accessible by the engine and your scripts.

So i can’t do something like in unity GameObject.Search and find it?

The first thing you need to understand is that your files and your game collection structure are two totally separate things. It doesn’t matter where or what a file is, that’s just stuff for the editor, you won’t read “.go” or “.collection” files at runtime, and the file names also have nothing to do with the object’s URL at runtime (except that the editor may use it as a default ID).

Getting the URL/ID:

If the object is spawned with a factory at runtime: The factory.create function will return the object’s ID. If you want to use the object later, then you probably want to save this ID somewhere. There is no way to get it again later, other than collision messages or messages sent from the spawned object itself.

If the object is NOT spawned at runtime, if it’s added to a collection in the editor, then you set the ID yourself in the editor. If you want to use the object, then you just hard-code in the URL.

No, with Defold there’s no way to traverse the scene-tree. There’s no get_parent or get_children. If you really need that stuff then you can set it up yourself, but generally you just need to change how you structure your code.


Defold is not Unity or anything like Unity. You could say that Defold does as few things for you as possible. This doesn’t mean it’s “bad”, but it may mean it’s not the engine for you. What it does mean is that you never have to make hacky workarounds or buy things on an asset store to get around built-in features that are buggy or just don’t work the way you want them to.

3 Likes

No. You need to create an instance of it to be able to access properties. Or store the width and height of the sprite manually.

thank you

th

Ok, if i created an object, how i can get acces to sprite on him, if in defold we don’t have Parent/Child ?image
image
local card = go.get("#card-tile","size.x")

Unfortunately, it isn’t possible (not yet anyways) to get the size of a sprite at runtime.
Perhaps this is something we wish to add (@britzl).

But, as always, before adding new features, we’d like to know more about your actual use case. (Sometimes there are other ways of solving a problem.)

Yeah, th. But if you know, please tell me the other way.

Well, I don’t know what you are trying to do with the size?

I need the size for creating the map of cards like this
image
I have a start position and i need a size for step.

I did it, but it is not automatic and smart.

local SizeOfSprite = {
	Card={width=120,height=160}
}

function generateTheCards(self)
	local p = go.get_position()
	p.x = p.x-200
	local x = 0
	local y = 0
	for i = 1, 9 do
		if x==3 then
			y = y + 1
			x = 0
		end
		local scale = 50
		local d = vmath.vector3(p.x+x*(SizeOfSprite["Card"]["width"] + scale),p.y+y*(SizeOfSprite["Card"]["height"] + scale),0)
		factory.create("#Card", d, nil, { score = 10 }, 1.0)
		x=x+1
	end
end

Perhaps, but it’s pragmatic and gets the job done.

1 Like

Devlopers, will be greate when i can have acces to sprite on runtime after creating a temple objects near sceen
image
And i see that you already have information about sprites

Shure)