Remapped Keyboard and Keypress Scripts

Make games! Discuss those games here.

Moderators: Bob the Hamster, marionline, SDHawk

Post Reply
User avatar
Ravancloak
Red Slime
Posts: 51
Joined: Wed Jul 31, 2019 1:49 am
Location: Venus
Contact:

Remapped Keyboard and Keypress Scripts

Post by Ravancloak »

Here's some scripts related to handling input and movement I wrote that someone might find helpful, idk. Diagonal movement is kinda cool. Note that you should use suspendPlayer before doing anything with custom movement. Also, I wrote this for a game running at 60fps, move speeds should be changed for lower. I highly recommend toggling off the bitset "remap joystick keys to keyboard keys". If I need to add more comments lmk. Enjoy :D

Globals needed by the below script. Note that I didn't add in the up/down/right/left globals to any scripts.

Code: Select all

globalVariable, begin
	#remap variables
	50, board:a
	51, board:b
	52, board:X
	53, board:Y
	54, board:up
	55, board:down
	56, board:left
	57, board:right
	58, cont:a
	59, cont:b
	60, cont:X
	61, cont:Y
	62, cont:up
	63, cont:down
	64, cont:left
	65, cont:right
end
Useful replacements for keypress, newKeypress, keyIsPressed, and waitForKey. Just run reset controls in your new game script or to reset controls. I hope to add in a player input remap script at some point.

Code: Select all

script, resetControls, begin
	cont:A	:= joy:A
	cont:B	:= joy:B
	cont:X	:= joy:Y
	cont:Y	:= joy:X
	
	board:A	:= key:x
	board:B	:= key:z
	board:X	:= key:Enter
	board:Y	:= key:rightShift
end

script, waitForAnyKey, begin
	while (anyKeyCheck==false) do (specwait)
end
script, waitForNewKey, begin
	while (newKeyCheck==false) do (specwait)
end
script, anyKeyCheck, begin
	if (keypressA||keypressB||keypressX||keypressY|| keypress(key:up)|| keypress(key:down)|| keypress(key:left)|| keypress(key:right)) then (return (true))
end
script, newKeyCheck, begin
	if (newkeypressA||newkeypressB||newkeypressX||newkeypressY|| newkeypress(key:up)|| newkeypress(key:down)|| newkeypress(key:left)|| newkeypress(key:right)) then (return (true))
end

script, keypressA, begin
	if (keypress(cont:A) || keypress(board:A)) then (return (true)) else (return (false))
end
script, keypressB, begin
	if (keypress(cont:B) || keypress(board:B)) then (return(true)) else (return (false))
end
script, keypressX, begin
	if (keypress(cont:X) || keypress(board:X)) then (return(true)) else (return (false))
end
script, keypressY, begin
	if (keypress(cont:Y) || keypress(board:Y)) then (return(true)) else (return (false))
end

script, newkeypressA, begin
	if (newkeypress(cont:A) || newkeypress(board:A)) then (return (true)) else (return (false))
end
script, newkeypressB, begin
	if (newkeypress(cont:B) || newkeypress(board:B)) then (return(true)) else (return (false))
end
script, newkeypressX, begin
	if (newkeypress(cont:X) || newkeypress(board:X)) then (return(true)) else (return (false))
end
script, newkeypressY, begin
	if (newkeypress(cont:Y) || newkeypress(board:Y)) then (return(true)) else (return (false))
end

script, keyIsPressedA, begin
	if (keyIsPressed(cont:A) || keyIsPressed(board:A)) then (return (true)) else (return (false))
end
script, keyIsPressedB, begin
	if (keyIsPressed(cont:B) || keyIsPressed(board:B)) then (return(true)) else (return (false))
end
script, keyIsPressedX, begin
	if (keyIsPressed(cont:X) || keyIsPressed(board:X)) then (return(true)) else (return (false))
end
script, keyIsPressedY, begin
	if (keyIsPressed(cont:Y) || keyIsPressed(board:Y)) then (return(true)) else (return (false))
end
Keypress script (set as default keypress or include it in your default keypress script). Includes dashing. Touch NPCS are changed to be activated by walking into them. IF you don't use the above script, just replace the scripts above with the ones in the dictionary; i.e replace "newkeypressA" with "new keypress (use key)".

Code: Select all

script, keypressMovement, begin
	variable (x,y, who, cur map, var)
	if (movementAllowed==false) then (exitScript)
	#checks to see if player should stop for npc event
	cur map := current map
	x := heroX
	y := heroY
	if (npc at spot (x,y)) then (suspendMovement, usenpcatspot(), specwait, resumemovement)

	if(hero is walking(0)==false && hero is walking(1)==false && hero is walking(2)==false && hero is walking(3)==false) then (
		set hero speed(me, 1)
		#b button, dashing
		if (keyIsPressedB) then (
			set hero speed(me, 2)
		)
	)

	#a button,  use stuff
	if (newkeypressA && hero is walking(me) == false) then (
		x := hero X + dirX(hero direction)
		y := hero Y + dirY(hero direction)
		if (NPC at spot (x,y)) then (
			if (activatableNPC(NPC at spot (x,y))) then (
				usenpcatspot
				exit script
			)
		)
		exit script
	)

	if (newkeypressX) then (
		#mm()
		opensettingsmenu()
		exitscript
	)
	#arrow keys, move character
	if (hero is walking(me) == false) then (
		if (key is pressed(up key)&&key is pressed(left key)) then (
			if (read wall bit (heroX(me)--1, heroY(me)--1, south wall)==false&&read wall bit (heroX(me)--1, heroY(me)--1, east wall)==false) then (
				walk hero(me, north, 1)
				walk hero(me, west, 1), exit script
			)
		)
		if (key is pressed(up key)&&key is pressed(right key)) then (
			if (read wall bit (heroX(me)+1, heroY(me)--1, south wall)==false&&read wall bit (heroX(me)+1, heroY(me)--1, west wall)==false) then (
				walk hero(me, north, 1)
				walk hero(me, east, 1), exit script
			)
		)

		if (key is pressed(down key)&&key is pressed(left key)) then (
			if (read wall bit (heroX(me)--1, heroY(me)+1, north wall)==false&&read wall bit (heroX(me)--1, heroY(me)+1, east wall)==false) then (
				walk hero(me, south, 1)
				walk hero(me, west, 1), exit script
			)
		)
		if (key is pressed(down key)&&key is pressed(right key)) then (
			if (read wall bit (heroX(me)+1, heroY(me)+1, north wall)==false&&read wall bit (heroX(me)+1, heroY(me)+1, west wall)==false) then (
				walk hero(me, south, 1)
				walk hero(me, east, 1), exit script
			)
		)
	)

	if (hero is walking(me) == false) then (
		if (keypress(up key)) then (
			x := heroX(me)
			y := heroY(me)--1
			if (NPC at spot (x,y)) then (
				who := npc at spot (x,y)
				if (activatableNPC(who) && read NPC (who, NPCstat:activation)==NPCactivation:touch) then (
					set hero direction (me, up)
					usenpcatspot
				)
				exit script
			)
		)
		if (keypress(down key)) then (
			x := heroX(me)
			y := heroY(me)+1
			if (NPC at spot (x,y)) then (
				who := npc at spot (x,y)
				if (activatableNPC(who) && read NPC (who, NPCstat:activation)==NPCactivation:touch) then (
					set hero direction (me, down)
					usenpcatspot
				)
				exit script
			)
		)
		if (keypress(left key)) then (
			x := heroX(me)--1
			y := heroY(me)
			if (NPC at spot (x,y)) then (
				who := npc at spot (x,y)
				if (activatableNPC(who) && read NPC (who, NPCstat:activation)==NPCactivation:touch) then (
					set hero direction (me, left)
					usenpcatspot
				)
				exit script
			)
		)
		if (keypress(right key)) then (
			x := heroX(me)+1
			y := heroY(me)
			if (NPC at spot (x,y)) then (
				who := npc at spot (x,y)
				if (activatableNPC(who) && read NPC (who, NPCstat:activation)==NPCactivation:touch) then (
					set hero direction (me, right)
					usenpcatspot
				)
				exit script
			)
		)
	)


	if (hero is walking(me) == false) then (
		if (key is pressed(up key)) then (walk hero(me, up, 1), exit script)
		if (key is pressed(left key)) then (walk hero(me, left, 1), exit script)
		if (key is pressed(down key)) then (walk hero(me, down, 1), exit script)
		if (key is pressed(right key)) then (walk hero(me, right, 1), exit script)
	)

	subscript, usenpcatspot, begin
		who := (NPC at spot(x,y))
		suspendMovement
		if (NPC is walking (who) == false) then (
			if (read NPC (who,NPCstat:when activated) <> NPCwhenactivated:donotfaceplayer) then (
				switch(hero direction(me)) do, (
					case (up) do (set NPC direction (who, down))
					case (down) do (set NPC direction (who, up))
					case (left) do (set NPC direction (who, right))
					case (right) do (set NPC direction (who, left))
				)
			)
			use npc (who)
		)
		resumeMovement
	end
end

script, activatableNPC, who, begin
	if (read NPC (who,NPCstat:activation) == NPCactivation:use) then (return (true))
end
Have a flower! ^-^~* (she/her)
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Re: Remapped Keyboard and Keypress Scripts

Post by TMC »

Neat! Remapping/overriding controls sure is a popular thing. Hopefully someone will find these useful.
It ouhgt to be possible to override just hero movement, to implement diagonal movement, but let the engine handle NPC activation (not diagonally).
I did make some more progress towards builtin control remapping a couple weeks ago; will return to it later.

As they are, the scripts currently aren't very drop-in because there's a lot of scripts missing: suspendMovement, resumemovement, specwait (what's that one for?), dirX, dirX, and (obviously game-specific) opensettingsmenu scripts, and also movementAllowed global.
(This reminds me that usenpc ought to have some kind of argument, or a new command, to handle turning towards the hero. Not obvious how that should work.)

I see you're not checking two of the expected walls bits for each of the four diagonal movement directions, so anyone who wants to use the diagonal movement script needs to be aware that they need to set up the walls on their map in a certain way for it to work. It gets quite messy to check those bits. You could more easily instead use the "check wall collision x/y" commands to do the wallchecking to ensure they're all checked.

For what it's worth,

Code: Select all

if (keypress(cont:A) || keypress(board:A)) then (return (true)) else (return (false))
is equivalent to

Code: Select all

return (keypress(cont:A) || keypress(board:A))
Post Reply