Stormworks: Build and Rescue

Stormworks: Build and Rescue

Stormworks Custom Content Workshop
Find, rate and try out the best community vehicles in Stormworks!
Doomkat 1 7 Sep, 2024 @ 1:52pm
First time with Lua, I may need some help
I'm trying to set up a reload for a turret using a turret ring, but the ring doesn't rotate. It should face forwards as long as the button is held. My current code is this:

function onTick()
Current_rotation = input.getNumber(2)
Input_rotation = input.getNumber(3)
Reload_input = input.getBool(4)
if Reload_input == True:
if Current_rotation >= 0.25:
output.setNumber(1, -1)
elseif Current_rotation > 0:
output.setNumber(1, -0.1)
elseif Current_rotation <= -0.25:
output.setNumber(1, 1)
elseif Current_rotation < 0:
output.setNumber(1, 0.1)
else:
output.setNumber(1, 0)
else:
output.setNumber(1, Input_rotation)
end

Could someone help me with getting this to work?
< >
Showing 1-2 of 2 comments
function onTick()
Current_rotation = input.getNumber(2)
Input_rotation = input.getNumber(3)
Reload_input = input.getBool(4)

if Reload_input then
local target = math.floor(Current_rotation + 0.5)
local diff = target - Current_rotation

if math.abs(diff) > 0.01 then
local speed = math.min(math.max(math.abs(diff)*10,0.1), 1)
output.setNumber(1, (diff > 0.01) and speed or -speed)
else
output.setNumber(1, 0)
end
else
output.setNumber(1, Input_rotation)
end
end


Here's some fixes, and improvements just so it autocorrects to position stabely.

You seem to either be familiar with a different language, or have no prior experience, as lua uses 'then' following every 'elseif' / 'if' statement, with nothing following 'else'. Also, an 'end' is required to follow the last 'if' / 'elseif' statement that there is.

Goodluck on the rest of your creation! (Just realized this was a 2024 discussion, hope it still helps!)
Last edited by Spectrum99999; 23 May @ 3:13pm
Originally posted by Spectrum99999:
function onTick()
Current_rotation = input.getNumber(2)
Input_rotation = input.getNumber(3)
Reload_input = input.getBool(4)

if Reload_input then
local target = math.floor(Current_rotation + 0.5)
local diff = target - Current_rotation

if math.abs(diff) > 0.01 then
local speed = math.min(math.max(math.abs(diff)*10,0.1), 1)
output.setNumber(1, (diff > 0.01) and speed or -speed)
else
output.setNumber(1, 0)
end
else
output.setNumber(1, Input_rotation)
end
end


Here's some fixes, and improvements just so it autocorrects to position stabely.

You seem to either be familiar with a different language, or have no prior experience, as lua uses 'then' following every 'elseif' / 'if' statement, with nothing following 'else'. Also, an 'end' is required to follow the last 'if' / 'elseif' statement that there is.

Goodluck on the rest of your creation! (Just realized this was a 2024 discussion, hope it still helps!)
Thanks for the reply, and I may use this in another creation. I ended up just using a PID for that one.
< >
Showing 1-2 of 2 comments
Per page: 1530 50