Skip to content

P3: Stop scripted button presses repeating in Lua additive button mode - #8074

Open
dustinspace217 wants to merge 1 commit into
scp-fs2open:masterfrom
dustinspace217:input/lua-additive-buttons
Open

dustinspace217 wants to merge 1 commit into
scp-fs2open:masterfrom
dustinspace217:input/lua-additive-buttons

Conversation

@dustinspace217

Copy link
Copy Markdown
Contributor

What?

In LUA_ADDITIVE_BUTTON_CONTROL mode, a button press now acts once instead of repeating forever.

Why?

With a script that turns on additive button mode, pressing almost any key (target next ship, ETS, countermeasure) makes the action repeat on its own every frame (the report says it even carries into the next mission). One press of T in the first FS2 mission kept cycling the target through every ship. A scripted press from useButtonControl does the same thing. The only way out today is for the script to call clearLuaButtonInfo every frame.

How?

After the script's buttons are merged into the player's, the script's buffer is cleared instead of having the merged result copied back into it. Scripts set buttons from hooks that run after game_process_keys, so a scripted press still gets merged on the next frame before the clear. That answers the worry in the issue thread that clearing it would disable scripted controls. A button a script wants held still has to be set every frame, same as with the workaround, and scripts that already call clearLuaButtonInfo every frame keep sending buttons as before. From reading the code, a press made right as a mission ends now fires once in the next mission instead of forever.

One change for scripts that read buttons: in additive mode, getButtonInfo / accessButtonInfo no longer show the player's own presses, only what the script has set since the last frame. Before, they showed the merged bits, which never cleared. That includes workaround scripts that read the buttons before clearing them: they now see only their own bits. From reading the code, pollAllButtons still reports the player's buttons (I didn't run it). Normal and override modes are untouched.

Testing?

Release and Debug builds on Fedora, 0 compiler warnings in both; unit tests 358 passed, 1 skipped. Master (94d278a; the Debug comparison used the commit before it) vs this branch (cdf7b09), OpenGL, 1280x720, mission SM1-01, MediaVPs 5.0.3 plus the GOTY Mission Pack 1.0.3 (MediaVPs 5.0.3 requires it), and a test mod holding only this table (the issue's script plus a frame hook that counts target changes):

#Conditional Hooks
$Application: FS2_Open

$On Game Init:
[
	ba.setButtonControlMode(LUA_ADDITIVE_BUTTON_CONTROL)
]

$On Frame:
[
	I4108_FRAMES = (I4108_FRAMES or 0) + 1
	if I4108_EF_UNTIL ~= nil then
		if I4108_FRAMES <= I4108_EF_UNTIL then
			ba.getControlInfo().useButtonControl(-1, "TARGET_NEXT")
		elseif I4108_EF_END == nil then
			I4108_EF_END = I4108_CHANGES or 0
		end
	end
	if hv.Player ~= nil and hv.Player:isValid() then
		local t = hv.Player.Target
		local name = "none"
		if t ~= nil and t:isValid() then
			name = t.Name
		end
		if name ~= I4108_LAST then
			I4108_LAST = name
			I4108_CHANGES = (I4108_CHANGES or 0) + 1
		end
	end
]

#End
  • One T press in additive mode: master kept cycling (97 target changes over about 100 frames on Debug), branch changed once.
  • One scripted useButtonControl(-1, "TARGET_NEXT"): master kept cycling, branch changed once.
  • Script setting the button every frame for 30 frames: 30 target changes on the branch, then none after it stopped; master kept going.
  • Normal and override modes: same results on both builds.
  • A retail mission played for a minute with no scripts: no difference, and the Debug logs show the same warnings on both builds.
  • A script setting a button and reading it back with getButtonInfo in the same frame sees it set; it reads clear after the merge, with one target change.
  • One boot under -vulkan with the T press test: same result as OpenGL.
  • Not tested: multiplayer.

Anything Else?

I used Claude Code to help write and test this.

Fixes #4108

In LUA_ADDITIVE_BUTTON_CONTROL mode, game_process_keys merged the script's
buttons into the player's and then copied the merged result back into the
script's buffer, so any press (the player's or the script's) stayed set and
fired again every frame. Clear the script's buffer after the merge instead,
so a scripted press acts once like a key press. Scripts set buttons from
hooks that run after game_process_keys, so their presses are still merged on
the next call before the clear.

Fixes scp-fs2open#4108
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LUA_ADDITIVE_BUTTON_CONTROL causes keypresses to repeat infinitely

1 participant