Community Forum

UnloadTriggers

Forum Overview >> Scripting

CategoryScripting
Created07.05.2021 14:53


Tomas Roy (TomRoyo) 07.05.2021 14:53
Hi,

How can I check if a player is in an unload trigger, for example in a silo or cowshed unload trigger?

Thanks in advance guys

Bilbo Beutlin (BBeutlin) 08.05.2021 01:38
Since there's no player collision mask detection by these triggers, you'll need to scan the station/trigger tables whether the player is within a certain range.

Tomas Roy (TomRoyo) 08.05.2021 16:42
Thank you for your response Bilbo. Am I going in the right direction?

for _,trigger in pairs(g_currentMission.unloadTriggers) do
local ax, ay, az= getWorldTranslation(g_currentMission.player)
local bx, by, bz= getWorldTranslation(trigger.triggerId)
local distance = MathUtil.vector3Length(ax-bx, ay-by, az-bz)

if distance < 1 then
-- do something
end
end

Bilbo Beutlin (BBeutlin) 08.05.2021 22:22
no no no .. 'g_currentMission.player' is a table, 'trigger.triggerId' is the user-defined location of the map icon.
Simpliest way is
local distance = calcDistanceFrom(g_currentMission.player.rootNode, trigger.rootNode);

Tomas Roy (TomRoyo) 09.05.2021 22:41
Thank you, Bilbo, for the correction. I have one more question, because if I use :

for _, trigger in pairs(g_currentMission.storageSystem.unloadingStations) do
local distance = calcDistanceFrom(g_currentMission.player.rootNode, trigger.rootNode)
print(distance)
end

the function calculates the player's distance from the building (silo, point of sale) and not from the unload trigger. On the other hand, when I use the function:

for _, trigger in pairs(g_currentMission.unloadTriggers) do
local distance = calcDistanceFrom(g_currentMission.player.rootNode, trigger.rootNode)
print(distance)
end

I get an error: bad argument #1 to 'pairs' (table expected, got nil)

Thanks in advance

Bilbo Beutlin (BBeutlin) 10.05.2021 00:00
The variable 'g_currentMission.unloadTriggers' doesn't exist.
The table 'unloadTriggers' is component of an UnloadingStation. This can have several unloadTriggers.

However there's a table 'g_currentMission.tipTriggers'. This should be suitable (no warranty).

Tomas Roy (TomRoyo) 10.05.2021 11:41
Unfortunately it seems not work, but I guess I made something wrong:

for _, trigger in pairs(g_currentMission.unloadingStations.unloadTriggers) do
local distance = calcDistanceFrom(g_currentMission.player.rootNode, trigger.rootNode)
print(distance)
end

with this I get error -> attempt to index field 'unloadingStations' (a nil value)

and with this:

for _, trigger in pairs(g_currentMission.storageSystem.unloadingStations.unloadTriggers) do
local distance = calcDistanceFrom(g_currentMission.player.rootNode, trigger.rootNode)
print(distance)
end

I get -> bad argument #1 to 'pairs' (table expected, got nil)

but for:

for _, trigger in pairs(g_currentMission.tipTriggers) do
local distance = calcDistanceFrom(g_currentMission.player.rootNode, trigger.rootNode)
print(distance)
end

it do nothing

Bilbo Beutlin (BBeutlin) 10.05.2021 12:18
I'd suggest you print() out some tables to understand the structures, eg. with the function DebugUtil.printTableRecursively()
See https://gdn.giants-software.com/documentation_scripting_fs19.php?version=script&category=6&class=46#printTableRecursively411

In case of unloadingStations you must write:
for _, station in pairs(g_currentMission.storageSystem.unloadingStations) do
for _, trigger in pairs(station.unloadTriggers) do
-- do something

I'd assume the table 'g_currentMission.tipTriggers' contains sub-tables. Examine it.

Tomas Roy (TomRoyo) 10.05.2021 15:55
Thank you Bilbo, it works perfect and DebugUtil.printTableRecursively helped me a lot to understand how it works.


Note: Log in to post. Create a new account here.