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.
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.
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.
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.
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.)
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