
Originally Posted by
MrStump
[...]When they click the button, you use getAllObjects(), then run a for loop through the table it gives you. Have it check each entry in table for its .tag field. I think all dice and custom dice should come up as "Dice" but you could do tests to be sure. Then subtract the X and Z of the dice from the X and Z of the tray's position, and if it is within a certain X/Z (say, 4 units) then you know the die is in the tray and can be rolled. Then use roll() on them.
It'd probably be much easier to just spawn a scripting zone inside the tray. Avoid looping every object on the table and no need to compare positions manually. Something like:
Code:
function buttonClicked()
pos = self.getPosition()
pos.y = pos.y + 1
zoneSpawn = {position = pos
, scale = { 4, 1, 4 }
, type = 'ScriptingTrigger'
, callback = 'zoneCallback'
, callback_owner = self
, rotation = self.getRotation }
spawnObject(zoneSpawn)
end
function zoneCallback(zone)
for o in zone.getObjects()
if o.tag == 'Dice'
o.roll()
end
end
zone.destruct()
end
I wrote that mostly from memory and haven't tested it, so be aware you may need to tweak a bit. For a working example check out the [URL="http://steamcommunity.com/sharedfiles/filedetails/?id=649618762"]dragonlance mod on the workshop[/URL]. Neither the mod nor original script is mine, I adjusted their script to move the dragons up when the bases grow and send the bases back home (positioned in a neat row) when they would shrink to 0 and they were kind enough to incorporate the changes into the mod.
Also, you'd have to change the exact numbers for the zone's scale and Y position based on the dimensions of the tray. Of course it's only going to work fully for a rectangular tray, but you could theoretically spawn additional smaller zones to fill in a different shape bit by bit. Might hit the same die twice if it ends up being on an edge between two zones, but if you're just rolling it that shouldn't matter.