Keyboard controls

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Keyboard controls

Post by minmay »

So here's some really, really basic keyboard control code than can be pasted directly into party.onDrawGui:

Code: Select all

local keybindings = {
	attack = {
		{'y','u'},
		{'i','o'},
		{'h','j'},
		{'k','l'},
	},
}

for cind = 1,4 do
	local champ = p:getChampion(cind)
	if champ:getEnabled() then
		for slot = 1,2 do
			if champ:isReadyToAttack(slot) then
				if context.keyDown(keybindings["attack"][cind][slot]) then
					local item = champ:getItem(slot)
					if not item then
						champ:attack(slot,false)
					else
						local actionname = item:getPrimaryAction()

						local action = nil
						if actionname then action = item.go:getComponent(actionname) end

						if action then champ:attack(slot,false) end
					end
				end
			end
		end
	end
end
Thanks to Champion:attack(), it works with melee weapons, missile weapons, firearms, etc. It even brings up the rune panel, but of course you can't draw runes with the keyboard. You can hold down the key to attack as fast as possible, but you can easily change the script to require pressing+releasing the key if you want.
Obviously this is not yet suitable for actual use - at the very least it needs rebinding and weapon swapping, and could also be a lot more efficient. I'm posting because there are a few issues the community needs to figure out if we're going to put keyboard controls in mods:
- how to handle special attacks. Triggering the secondary action is no problem at all, just use Champion:attack(slot, true) instead of Champion:attack(slot, false) and manually address the energy cost, requirements, etc. The problem is the build-up time; you can certainly require holding down the key to charge the attack, and block other attacks during it, but recreating the visual effect seems difficult.
- how to handle spellcasting - it's easy to draw your own rune panel and have players select runes on that with keys, but I don't think you can do it with the built-in rune panel, *and* I don't know of a way to detect whether the rune panel is open. You could activate an "isCastingSpell" flag every time a rune panel item is used, but I don't know of a way to do that for bare hands, and how would you detect closing the rune panel?
- this is more an unavoidable annoyance than a problem to be solved, but it seems there are a lot of keys that you'd expect to be able to use, but seemingly can't, such as ';'.

Also, additional keyboard controls make it way more obvious how clunky the "input queue" style of party movement is (you can't hold down keys), so be warned. Unfortunately, there's no PartyComponent:move() or PartyComponent:turn() that could be used to fix this.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Post Reply