Jump to content

LUA Question, Probably Simple


IAmKane

Recommended Posts

Like the title says, this is probably a simple question but I'm having an issue getting any kind of custom tweaking done in my ship. Hopefully one of you knowledgeable folks can let me know where I've gone wrong.

 

Currently my ship is setup as a flying construct with the mouse-point control method. I figured I'd start with some 'simple' adjustments so that the ship behaved differently when below a certain speed to simplify the landing process. Apparently I was too ambitious, because when I try to fly the ship it locks up the controls and I get a lovely red "LUA Error" message.

 

I did my best to stick to pretty simple code based on what was already in the script so I'm pretty sure my syntax is correct, but I'm new to LUA so I could be way off base.

 

Here's the code I've tried starting with:

local autoRollVelThreshold = 50.0 --my created variable
local constructVelocity = vec3(core.getWorldVelocity()) --pre-existing variable 

-- rolls wings level if velocity is below a certain limit defined by the variable autoRollVelThreshold
if constructVelocity < autoRollVelThreshold then
  local targetRollDeg = utils.clamp(0,currentRollDegAbs-30, currentRollDegAbs+30);  -- we go back to 0 within a certain limit
  if (rollPID == nil) then
     rollPID = pid.new(autoRollFactor * 0.01, 0, autoRollFactor * 0.1) -- magic number tweaked to have a default factor in the 1-10 range
  end
     rollPID:inject(targetRollDeg - currentRollDeg)
     local autoRollInput = rollPID:get()
     targetAngularVelocity = targetAngularVelocity + autoRollInput * constructForward
end

It's based on the pre-existing code for the auto-roll parameter, except instead of using the true/false autoRoll variable I'm using the local variable constructVelocity defined elsewhere in flush() alongside my local variable autoRollVelThreshold which sets the speed at which the action takes place.  

 

I thought this would be a simple start to something more complex but it appears I've got some basic issues I still need to get sorted. Any help would be greatly appreciated, thanks.

Link to comment
Share on other sites

14 hours ago, Helediron said:

constructVelocity is a vector and not a number. You can get speed out from it by constructVelocity:len()

That did the trick, thanks! Why does it work that way, though? I thought 'len' was for getting the length of a string.

Link to comment
Share on other sites

It works because it's coded so. You can look it yourself. Go to game install directory, game\data\lua\cpml and vec3.lua. String has len(), so it's just natural to use the same name for similar purpose. There is much more inside.

Link to comment
Share on other sites

21 hours ago, PlasmaFlow said:

Can you post the final working code?

It's the same as in the OP, just with constructVelocity:len() replacing constructVelocity.  It goes into the flush() of my ship inside of the if-then for determining whether it is in atmosphere or not when it is complied as a 'flying' type construct.

 

Basically like this:


	local autoRollVelThreshold = 50.0 --export: velocity below which the creaft will auto-roll the wings level to aid in landing =====NEW=====

-- In atmosphere?
	if worldVertical:len() > 0.01 and unit.getAtmosphereDensity() > 0.0 then
		local autoRollRollThreshold = 1.0
		-- autoRoll on AND currentRollDeg is big enough AND player is not rolling
		if autoRoll == true and currentRollDegAbs > autoRollRollThreshold and finalRollInput == 0 then
			local targetRollDeg = utils.clamp(0,currentRollDegAbs-30, currentRollDegAbs+30);  -- we go back to 0 within a certain limit
			if (rollPID == nil) then
				rollPID = pid.new(autoRollFactor * 0.01, 0, autoRollFactor * 0.1) -- magic number tweaked to have a default factor in the 1-10 range
			end
			rollPID:inject(targetRollDeg - currentRollDeg)
			local autoRollInput = rollPID:get()

			targetAngularVelocity = targetAngularVelocity + autoRollInput * constructForward
		end
		-- =====START NEW=====autolevel
		-- rolls wings level if velocity is below a certain limit
		if constructVelocity:len() < autoRollVelThreshold then
			local targetRollDeg = utils.clamp(0,currentRollDegAbs-30, currentRollDegAbs+30);  -- we go back to 0 within a certain limit
			if (rollPID == nil) then
				rollPID = pid.new(autoRollFactor * 0.01, 0, autoRollFactor * 0.1) -- magic number tweaked to have a default factor in the 1-10 range
			end
			rollPID:inject(targetRollDeg - currentRollDeg)
			local autoRollInput = rollPID:get()

			targetAngularVelocity = targetAngularVelocity + autoRollInput * constructForward
		end
		-- =====STOP NEW=====autolevel
		local turnAssistRollThreshold = 20.0
		-- turnAssist AND currentRollDeg is big enough AND player is not pitching or yawing
		if turnAssist == true and currentRollDegAbs > turnAssistRollThreshold and finalPitchInput == 0 and finalYawInput == 0 then
			local rollToPitchFactor = turnAssistFactor * 0.1 -- magic number tweaked to have a default factor in the 1-10 range
			local rollToYawFactor = turnAssistFactor * 0.025 -- magic number tweaked to have a default factor in the 1-10 range

			-- rescale (turnAssistRollThreshold -> 180) to (0 -> 180)
			local rescaleRollDegAbs = ((currentRollDegAbs - turnAssistRollThreshold) / (180 - turnAssistRollThreshold)) * 180
			local rollVerticalRatio = 0
			if rescaleRollDegAbs < 90 then
				rollVerticalRatio = rescaleRollDegAbs / 90
			elseif rescaleRollDegAbs < 180 then
				rollVerticalRatio = (180 - rescaleRollDegAbs) / 90
			end

			rollVerticalRatio = rollVerticalRatio * rollVerticalRatio

			local turnAssistYawInput = - currentRollDegSign * rollToYawFactor * (1.0 - rollVerticalRatio)
			local turnAssistPitchInput = rollToPitchFactor * rollVerticalRatio

			targetAngularVelocity = targetAngularVelocity
								+ turnAssistPitchInput * constructRight
								+ turnAssistYawInput * constructUp
		end
	end

Sorry it's so messy, the auto-formatting really didn't like this snippet.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...