I'm having issues with descendants of UIs.
#1
Here's some code, for example:

[lua]
link(tool.Selected, function()
gui = tool.UIFrame:clone()
gui.Parent = tool.Parent.Parent.UI
chosen = gui:getChild("Chosen")
[/lua]

Doesn't find it.

Also,

[lua]
local stuff = gui:getChildren()
print(#stuff)
[/lua]

prints 0. The UI itself clones into the Player and is visible, but I can't access the descendants.
Reply
#2
Could you PM me the game that this is happening on?
Reply
#3
It's just a model -- I'll attach it here. It's almost done anyway -- that UI bug is the only thing that I think is stopping it from being fully functional.


Attached Files
.bsmodel   Paintbrush.bsmodel (Size: 14 KB / Downloads: 133)
Reply
#4
I think I know what the problem is. It has to do with multithreading, which allows a program to do more than one thing at the same time.

In Buildism, the main thread handles physics and input, another thread draws stuff to the screen, and each Lua script is its own thread too. However, a lot of components of the game, like the physics engine and the navigator, don't like being accessed from threads other than the main thread (the physics engine might be asked to add a part while it is in the middle of stepping the simulation, or the navigator might have to add an object while it is already busy adding another.) So instead of doing these things itself, a script thread tells the main thread to do them as soon as it is done with the current frame.

[lua]
game.World.Box.Size = Vec2D(10, 5)
[/lua]

When you change a property (including Parent) with a script, the value of the property is changed immediately (so print(game.World.Box.Size) would always give you 10,5). But the actual change is not guaranteed to be done before the next line in the script, only as soon as possible. Usually this doesn't matter, but when you're cloning a lot of objects and trying to access them immediately afterwards it does.

The solution is to wrap all of the code that needs to access the cloned objects in

[lua]
gameConfusedcheduleTask(function()

end)
[/lua]

This tells the game to run your Lua code on the main thread, and when you change the Parent property it will detect that it's running on the main thread and do the change immediately instead of as soon as possible.
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)