Skip to content

Commit dc7bd0d

Browse files
authored
Prevented overlapping trigger bug (#2)
- Fixed issue #1. It was caused by an overlapping trigger. Trigger 1 would store the original clip distance and reduce the clip distance, then the second trigger would store the reduced clip distance as the original, which would result in an incorrect reset of the clip distance when exiting the trigger. - Added comments to the main functionality of the script. - Updated the comment block at the top of the file.
1 parent 490bd70 commit dc7bd0d

File tree

1 file changed

+59
-14
lines changed

1 file changed

+59
-14
lines changed

clipDistanceControl.lua

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --
2-
-- clipDistanceControl
2+
-- clipDistanceControl v1.1.0.0
33
--
44
-- Purpose: The purpose of this script is to control the 'clip distance' (also known as view distance)
5-
-- of an object that is inside the associated trigger. With how the GIANTS engine currently handles
6-
-- rendering objects, it also renders them if they are not visible to the player (e.g. blocked by a
7-
-- vehicle shed). Reducing the clip distance on them when they are in this shed can greatly increase
8-
-- the performance of the game.
5+
-- of an object that is inside the associated trigger. With how the GIANTS engine currently handles
6+
-- rendering objects, it also renders them if they are not visible to the player (e.g. blocked by a
7+
-- vehicle shed). Reducing the clip distance on them when they are in this shed can greatly increase
8+
-- the performance of the game.
99
--
1010
-- How to use:
1111
-- - Add the trigger shape
@@ -33,13 +33,25 @@
3333
--
3434
-- Authors: Timmiej93
3535
-- Based on the FS15 script 'clipDistanceControl', of which I have been unable to find the author.
36+
-- If you know or are the original author, please let me know, so I can properly credit them.
37+
--
38+
-- Changelog
39+
-- For the changelog, please visit GitHub. This always has the most up to date and most complete
40+
-- changelog available. GitHub information is at the bottom of this comment block.
41+
--
3642
--
3743
-- Copyright (c) Timmiej93, 2018
3844
-- This file can be used in any map without specific permission. It can however not be claimed to be
39-
-- your own work. Crediting me is not required, but it would be nice.
40-
-- For more information on copyright for this mod, please check the readme file on GitHub:
41-
-- GitHub > Timmiej93 > clipDistanceControl
42-
-- https://github.com/Timmiej93/clipDistanceControl
45+
-- your own work. Crediting me is not required, but it would be nice. This comment block (line 1
46+
-- through 56) can NOT be removed (there is also no reason to remove it), to ensure that anyone
47+
-- with questions can find the original author, and can complain to me, instead of you, the map
48+
-- maker.
49+
-- For more information or questions on copyright for this mod, please check the readme file on
50+
-- GitHub. GitHub information is at the bottom of this comment block.
51+
--
52+
-- GitHub information
53+
-- GitHub > Timmiej93 > clipDistanceControl
54+
-- https://github.com/Timmiej93/clipDistanceControl
4355
--
4456
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --
4557

@@ -52,47 +64,80 @@ function clipDistanceControl.onCreate(id)
5264
end
5365

5466
function clipDistanceControl:new(id, customMt)
67+
68+
if (g_currentMission.CDC_savedDistances == nil) then
69+
g_currentMission.CDC_savedDistances = {}
70+
end
71+
5572
local self = {}
5673
if (customMt ~= nil) then
5774
setmetatable(self, customMt)
5875
else
5976
setmetatable(self, clipDistanceControl_mt)
6077
end
61-
78+
6279
self.triggerId = id
6380
addTrigger(id, "clipDistanceControlCallback", self)
6481

65-
self.innerClipDistance = Utils.getNoNil(getUserAttribute(id, "innerClipDistance"), 300)
6682
self.savedCD = {}
83+
self.innerClipDistance = Utils.getNoNil(getUserAttribute(id, "innerClipDistance"), 100)
6784

6885
return self
6986
end
7087

71-
function clipDistanceControl:delete()
88+
function clipDistanceControl:deleteMap()
7289
removeTrigger(self.triggerId)
7390
end
7491

92+
function clipDistanceControl:keyEvent(unicode, sym, modifier, isDown) end
93+
function clipDistanceControl:mouseEvent(posX, posY, isDown, isUp, button) end
94+
function clipDistanceControl:draw() end
95+
function clipDistanceControl:update(dt) end
96+
7597
function clipDistanceControl:clipDistanceControlCallback(triggerId, otherId, onEnter, onLeave, onStay, otherShapeId)
98+
99+
-- Use the otherId to check if it's a vehicle or an object.
76100
local vehicle = g_currentMission.nodeToVehicle[otherId]
77101
local object = g_currentMission.nodeObjects[otherId]
78102

103+
-- Grab a local copy of the table, to prevent humongeous lines.
104+
local gcmData = g_currentMission.CDC_savedDistances
105+
79106
if (vehicle ~= nil or object ~= nil) then
107+
-- If the ID is indeed a vehicle or an ojbect...
80108

81109
if (onEnter) then
110+
-- Get the vehicle's clip distance.
82111
local cd = getClipDistance(otherId)
112+
83113
if (cd ~= nil and self.savedCD[otherId] == nil) then
114+
-- If the found clipdistance isn't nil, and this vehicle/object isn't in this trigger's database yet...
115+
if (gcmData[otherId] == nil) then
116+
-- If this vehicle/object isn't in the global database yet, store it there.
117+
gcmData[otherId] = cd
118+
end
119+
-- Store the clip distance in this trigger's database.
84120
self.savedCD[otherId] = cd
121+
-- Set the vehicle's/object's clip distance to the trigger's distance.
85122
setClipDistance(otherId, self.innerClipDistance)
123+
86124
end
87125
end
126+
88127
if (onLeave) then
89-
local cd = self.savedCD[otherId]
128+
-- Get the stored (original) clip distance from the global database
129+
local cd = gcmData[otherId]
130+
90131
if (cd ~= nil) then
132+
-- If the stored clip distance isn't nil, clear the stored data in this trigger's database and the global
133+
-- database, and then set the clipdistance back to the original clip distance of the vehicle / object.
91134
self.savedCD[otherId] = nil
135+
gcmData[otherId] = nil
92136
setClipDistance(otherId, cd)
93137
end
94138
end
95139
end
96140
end
97141

98-
g_onCreateUtil.addOnCreateFunction("clipDistanceControl", clipDistanceControl.onCreate)
142+
g_onCreateUtil.addOnCreateFunction("clipDistanceControl", clipDistanceControl.onCreate)
143+
addModEventListener(clipDistanceControl)

0 commit comments

Comments
 (0)