PDA

View Full Version : [SOLVED] spawnObject ScriptingTrigger doesn't work with getObjects call



chillblain
07-05-2017, 08:56 PM
When attempting to use a getObjects call on a scripting zone trigger spawned through a spawnObject call, the scripting zone fails to return any objects inside of it.

Here is an example of the function I am using to spawn a scripting zone-



-- Create a new scripting zone during run time
function spawnScriptingZone(position, rotation, scale)
local params = {}
params.type = "ScriptingTrigger"
params.position = position
params.rotation = rotation
params.scale = scale
scriptZone = spawnObject(params)

return scriptZone
end


and here is my script that uses getObjects-



local tempScriptZoneTable = spawnScriptingZone(deck.getPosition(), deck.getRotation(), tileZoneScale).getObjects()
local deckBottomCard = nil
PrintTableContents(tempScriptZoneTable)

...

-- Print table contents, recursively finds the bottom if tables within tables
function PrintTableContents(table)
for k, v in pairs(table) do
if type(v) ~= "table" then
printToAll("Table Key: " .. k ..", Value: " .. type(v), {1.0, 1.0, 1.0})
else
printToAll("!---Table Key: " .. k .." has this table inside it:", {0.5, 0.5, 0.5})
PrintTableContents(v)
end
end
end


I verified that the scripting zone was created, placed, and sized as expected (large enough to contain several cards and in the right spot) and was unable to get any objects that were inside it through a getObjects call. Just to be sure I also experimented with waiting a time and making the call again later as well as with other spawned scripting zones, never being able to get anything back.

MrStump
07-05-2017, 11:26 PM
Are you spawning your scripting zone on the same tick as you are trying to use getObjects? Like all spawned objects, there is a short delay of a few frames between spawning a scripting zone and it actually existing in order to be used.

Also, a simpler solution might be to use a raycast of the box type. The next update is probably going to have some visualizer to help with that, but one of the things you could do with it is detect objects in the area of a box, returning a table of the hits.

chillblain
07-06-2017, 12:26 PM
Yeah, I tried waiting a few frames and even a few seconds before calling getObjects() as well. No go. I did a seperate test with scripting zones I had spawned during game setup calling getObjects() much later during play and they still wouldn't return anything. I suspect there's some kind of start or initialization function that doesn't get called for spawned zones.

dzikakulka
07-06-2017, 01:35 PM
Related?
http://www.berserk-games.com/forums/showthread.php?2503-Spawning-a-custom-object-inside-scripting-zone-breaks-onObjectEnter-Leave-events

chillblain
07-06-2017, 05:05 PM
Doesn't seem like it in this case, the only object I spawn is the scripting zone itself- not the decks or cards I was trying to get access to (these existed since the start of the game). I've tried moving objects into the spawned zone that didn't start inside and still wasn't able to get anything back.

...though it is possible that a the root of the problems behind these two bugs is somehow related.

MrStump
07-06-2017, 09:44 PM
I just tried it using your zone spawning code and a callback. It finds the object in the zone without issue.


-- Create a new scripting zone during run time
function spawnScriptingZone(position, rotation, scale)
local params = {}
params.type = "ScriptingTrigger"
params.position = position
params.rotation = rotation
params.scale = scale
params.callback = "callback_getObjects"
params.callback_owner = self
scriptZone = spawnObject(params)

return scriptZone
end

function onLoad()
zone = spawnScriptingZone(self.getPosition(), {0,0,0}, {5,5,5})
print(zone) --prints the zone
end

function callback_getObjects(obj)
--[[
local objectsInZone = obj.getObjects()
print(#objectsInZone) --prints 1
]]

--[[
local objectsInZone = zone.getObjects()
print(#objectsInZone) --prints 1
]]
end

You can see 2 commented out bits of code in callback_getObjects(obj). I tried both, seeing if it was a problem with the zone being passed before or what. Both worked for me, printing 1 return (the object I had placed this script over top of)

A few possible causes of the issue that I can go off of:

I am miss-understanding the problem.
Your script zone is actually a little bit off the table, so the cards you are trying to find are actually under the script zone, being missed. An easy enough thing to miss. Try making the zone big and see if the problem persists. Also double check its position visually and make sure it is where you want it in general.
You are doing multiple script zone creations at once. If that is a case, it causes problems sometimes if you try to get the spawnObject return of the entity (zone in my script). Instead, using the entity reference (obj in my script) that is provided by the callback might fix that problem. It is always safer to use the callback for the entity reference, although you can sometimes get away without it.
There was some simple error in your code, a miss-spelled variable or whatnot. Make sure that your zone entity reference is valid (if tempScriptZoneTable == nil then print("error") else ... end), things like that.

chillblain
07-06-2017, 11:46 PM
Hmm. I'll have to give it a go when I get a chance, but you may also want to try it outside of OnLoad() though, since I was thinking there was some kind of init phase missed for a spawnObject script zone done in the middle of a running game. I could very well be wrong and just need to take a look at everything though.

All I was trying to do was get cards and/or decks inside of a spawned script zone into a table in my script so I could verify if it was the last card left behind in a deck. Visually, it looked like the script zone was in the right place when I looked, but I'll have to fiddle with it some more just in case.

chillblain
07-08-2017, 07:59 PM
Alright, than, nevermind- it works. I guess I wasn't waiting long enough after all. Trying it with a function callback gives me results. It's possible in trying other iterations of getting it to work I mistyped something or just did something wrong! This can be closed fixed.