Barotrauma

Barotrauma

RuyiLuaComponent
whosyourdaddy  [developer] 24 Apr @ 7:09am
Code Example
Enter the password to open the door
function inp(pin, value) -- If input pin 1 received signal if pin == 1 then -- If input pin 1 received signal value "9527" if value == "9527" then -- Open the keypad door out[1] = 1 end end end

At the start of the round, generate a random number
function onMapLoaded() -- send a generated random number ranged in 0~10000 from output pin 1 when all items have been loaded out[1] = math.random() * 10000.0 end

Record campaign playtime
local campaignPlaytime function update(deltaTime) campaignPlaytime = campaignPlaytime + deltaTime local hours = math.floor(campaignPlaytime / 3600) local minutes = math.floor(campaignPlaytime / 60) % 60 local seconds = math.floor(campaignPlaytime) % 60 -- outputs formatted playtime out[1] = ("%i:%i:%i"):format(hours, minutes, seconds) end function onItemLoaded() -- read the last saved campaign playtime when the item has finished loading -- note: if the stored data cannot be converted to a number, reset the playtime to 0.0 campaignPlaytime = tonumber(readMemory()) or 0.0 end function onSave() -- saved the current campaign playtime when the component starts being saved. writeMemory(tostring(campaignPlaytime)) end

Print welcome message in multiplayer

function update()
-- we do the initialization on update for ensure stability

if isMultiplayer then
if isClient then
-- When you load into the game, you send the message ​​"Hi everyone! I’m in!"​​ to the server.
netSend "Hi everyone! I’m in!"

-- When you receive the message from the server, it gets printed to your console.
function netReceive(message)
print(message)
end
end

if isServer then
function netReceive(message, client)
-- Upon receiving the message from the player, the server broadcasts their message to all other players.
netSend(client.Name .. " said: " .. message)
-- output to the text displayer
out[1] = client.Name .. " said: " .. message
end
end
end

-- set nil, do not need to execute the initialization again
update = nil
end

Teleport character
inp = {} inpSenders = {} function update() -- If the input pin 3 received a signal if inp[3] then local signalSource = inpSenders[3] -- If the signal takes a source if signalSource then -- Teleport the signal source to the position of this component signalSource.TeleportTo(ruyiLua.Item.WorldPosition) -- reset the signal source of the input pin 3 inpSenders[3] = nil end -- reset input pin 3 inp[3] = nil end end

Control engine set_force via key presses
inpSenders = {} -- Implementation Method 1 function inp(pin, value) -- When input pin 1 receives a control signal from the periscope if pin == 1 then -- Get the signal sender who is using the periscope local signalSource = inpSenders[pin] if signalSource then -- Detect the controller's key presses. if signalSource.IsKeyDown(InputType.Left) then -- If the controller is pressing the Left mouse button -- Output pin 1​ sends a value of ​​-100​ to the engine, making the submarine move ​backward. out[1] = -100 elseif signalSource.IsKeyDown(InputType.Right) then -- If the controller is pressing the Right mouse button -- Output pin 1​ sends a value of ​100​ to the engine, making the submarine move ​forward. out[1] = 100 end -- clear the table clear(inpSenders) end end end -- Implementation Method 2 inp = {} function update() local engineHasBeenControledViaKeyPresses = false if inp[1] then local signalSource = inpSenders[1] if signalSource then if signalSource.IsKeyDown(InputType.Left) then -- Set the signal sender of the output pin 1 as the signal source of that input pin 1 outSender = signalSource out[1] = -100 outSender = nil engineHasBeenControledViaKeyPresses = true elseif signalSource.IsKeyDown(InputType.Right) then outSender = signalSource out[1] = 100 outSender = nil engineHasBeenControledViaKeyPresses = true end clear(inpSenders) end clear(inp) end -- If there is not any controllers using the periscope to control the engine, reset its set_force if not engineHasBeenControledViaKeyPresses then out[1] = 0 end end