meta data for this page
Objects/ToggleObject.lua
ToggleObjects are used whenever you want to enable/disable an object depending on a control switch or a control button.
24 ToggleObject = ToggleObject or {}; 25 local ToggleObjectClass = Class(ToggleObject);
ToggleObject:load(switchId, objectId, inverseId, switchName, switchOff, switchOn, isButton)
Gets called by onCreate events and initializes a new toggle object using the following parameters:
switchId
(transform id): Object id of the control switch or control buttonobjectId
(transform id): Object id of the object that shall be enabled/disabled.inverseId
(transform id): Object id of the object that shall be disabled/enabled inversely toobjectId
(useful if you want to switch between two different states of a single object)switchName
(string): Name of the switch
If you want to initialize a control switch, you can use following parameters:
switchOff
(string): Labelling of the control switch position, at which the object will be invisibleswitchOn
(string): Labelling of the control switch position, at which the object will be visibleisButton
(optional bool): must benil
orfalse
If you want to initialize a control button, following parameters are used instead:
switchOff
: unused (i.e.nil
)switchOn
: unused (i.e.nil
)isButton
(bool): must betrue
45 function ToggleObject:load(switchId, objectId, inverseId, switchName, switchOff, switchOn, isButton) 46 -- first store the main id 47 self.switchId = switchId; 48 self.objectId = objectId; 49 self.inverseId = inverseId; 50 51 self.textOn = textOn; 52 self.textOff = textOff; 53 54 self.state = false; 55 self.isButton = isButton; 56 57 if isButton then 58 -- this is a button -> in this case we only need switchName 59 ControlElement.new(switchId); 60 ControlElement.setCallback(switchId, 1, function() 61 self:setState(not self.state); 62 end); 63 ControlElement.setName(switchId, l10n.getDollar(switchName or "Unknown")); 64 65 else 66 -- this is a control switch 67 68 ControlSwitch.new(switchId); 69 ControlSwitch.addPosition(switchId, 0, l10n.getDollar(switchOff or "Unknown")); 70 ControlSwitch.addPosition(switchId, 60, l10n.getDollar(switchOn or "Unknown")); 71 ControlSwitch.setCallback(switchId, function(pos) self:setState(pos == 1); end); 72 ControlSwitch.setName(switchId, l10n.getDollar(switchName or "Unknown")); 73 end; 74 75 self:setState(false); 76 77 -- prepare loading/saving 78 local x,y,z = getWorldPosition(switchId); 79 self.savegameKey = "toggleObject" .. string.format(":%s @ (%.4f, %.4f, %.4f)", switchName or "unnamed", x,y,z); 80 81 -- this will call our destroy event 82 g_scenario:addObjectAndLoad(self, switchId); 83 end;
ToggleObject:destroy()
Destroys the lua instance if the object is destroyed (e.g. deleted using the in-game editor).
87 function ToggleObject:destroy() 88 -- as always: unregister from objects 89 g_scenario:removeObject(self); 90 end;
ToggleObject:saveToTable(savegame)
Saves the information from this toggle object into the savegame table.
94 function ToggleObject:saveToTable(savegame) 95 if savegame[self.savegameKey] ~= nil then 96 print("Warning while saving to table: Multiple ToggleObjects are using the key " .. tostring(self.savegameKey) .. ". All of them will have the same state when loading the savegame. Please avoid ToggleObjects that are named the same and placed at the same spot."); 97 return; 98 end; 99 savegame[self.savegameKey] = self.state; 100 end;
ToggleObject:loadFromTable(savegame)
Retrieves information from the savegame.
104 function ToggleObject:loadFromTable(savegame) 105 if savegame == nil then return end; 106 107 local state = savegame[self.savegameKey]; 108 if state ~= nil then 109 self:setState(state); 110 111 if not self.isButton then 112 ControlSwitch.setPosition(self.switchId, state and 1 or 0); 113 end; 114 end; 115 end;
ToggleObject:setState(state)
Sets the state of the toggle object to either true
or false
.
119 function ToggleObject:setState(state) 120 self.state = state or false; 121 122 if self.objectId ~= nil then setActive(self.objectId, self.state); end; 123 if self.inverseId ~= nil then setActive(self.inverseId, not self.state); end; 124 end;
Copyright
All contents of this page may be used for modding use for Winter Resort Simulator only. Any use exceeding this regulation is not permitted.
Copyright (C) HR Innoways, 2020. All Rights Reserved.