Lua, How Do you see if KeyPressed

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
SnowyOwl47
Posts: 148
Joined: Fri Sep 12, 2014 10:41 pm

Lua, How Do you see if KeyPressed

Post by SnowyOwl47 »

Hey guys i'm very new to lua but learning pretty fast and I've been working on a dungeon named The Deep Flask. I've got a lot of the Legend of grimrock custom code down but now i want to move bigger. I was wondering if i could implement into my dungeon cheats buy clicking keys basically exactly like the Dungeon Editor ones. So i can test my dungeon without being inside dungeon editor easily. And i also want it for people to be able to Use it them selfs if they'd like.
Last edited by SnowyOwl47 on Fri Sep 12, 2014 10:53 pm, edited 1 time in total.
User avatar
SnowyOwl47
Posts: 148
Joined: Fri Sep 12, 2014 10:41 pm

Re: Lua, How to see if KeyPressed

Post by SnowyOwl47 »

One more thing is there a way to have a GUI buttons that when you start the dungeon it asks you would you like cheats allowed: Button: Yes Button: No then you probably know where this is going.
User avatar
Diarmuid
Posts: 807
Joined: Thu Nov 22, 2012 6:59 am
Location: Montreal, Canada
Contact:

Re: Lua, How Do you see if KeyPressed

Post by Diarmuid »

Hi!

Sure, that's quite handy. Here are emulations of the Z (open door), K (kill) and H (heal) functions:

Code: Select all

-- Set this to true before exporting the DAT, or false while in the editor
activateHooks = true;



keysDown = {};

-- redirect a call to this function from the party object's onDrawGui(g) hook
function party_onDrawGui(g)

	if not(activateHooks) then return; end

	-- Open door key hook
	if g.keyDown("z") then
		if not(keysDown["z"]) then
			toggleAhead();
			keysDown["z"] = true;
		end
	else
		if keysDown["z"] then
			keysDown["z"] = false;
		end
	end
	
	-- Kill key hook
	if g.keyDown("k") then
		if not(keysDown["k"]) then
			killMonsterAhead();
			keysDown["k"] = true;
		end
	else
		if keysDown["k"] then
			keysDown["k"] = false;
		end
	end
	
	-- Heal key hook
	if g.keyDown("h") then
		if not(keysDown["h"]) then
			party:heal();
			keysDown["h"] = true;
		end
	else
		if keysDown["h"] then
			keysDown["h"] = false;
		end
	end

end

function toggleAhead()
	local dx, dy = getForward(party.facing);
	for e in fix.newEntitiesAt(party.level, party.x, party.y) do
		if e.setDoorState and e.facing == party.facing then e:toggle();	end
	end
	for e in fix.newEntitiesAt(party.level, party.x + dx, party.y + dy) do
		if e.setDoorState and e.facing == (party.facing + 2) % 4 then e:toggle(); end
		if e.setPitState then e:toggle(); end
	end
end

function killMonsterAhead()
	local dx, dy = getForward(party.facing);
	damageTile(party.level, party.x + dx, party.y + dy, party.facing, 125, "physical", 9999);
end
They are even better as Z also toggles pits, and kill gives experience to chars. Notice that the code above uses the "fixed" entitiesAt code (no map markers bug) which you should use anyway in your mod, search the forums for it.

To use that, put it in a script entity, named for example "testShortcuts", and send a call to that from the party object, like this: testShortcuts.party_onDrawGui(g)
User avatar
SnowyOwl47
Posts: 148
Joined: Fri Sep 12, 2014 10:41 pm

Re: Lua, How Do you see if KeyPressed

Post by SnowyOwl47 »

@Diarmuid
Thanks for the help but I can't find anything on how to "and send a call to that from the party object, like this: testShortcuts.party_onDrawGui(g)" nothing works... i copy pasted your script and named the script_entity to the exact testShortcuts
Diarmuid wrote:Hi!

Sure, that's quite handy. Here are emulations of the Z (open door), K (kill) and H (heal) functions:

Code: Select all

-- Set this to true before exporting the DAT, or false while in the editor
activateHooks = true;



keysDown = {};

-- redirect a call to this function from the party object's onDrawGui(g) hook
function party_onDrawGui(g)

	if not(activateHooks) then return; end

	-- Open door key hook
	if g.keyDown("z") then
		if not(keysDown["z"]) then
			toggleAhead();
			keysDown["z"] = true;
		end
	else
		if keysDown["z"] then
			keysDown["z"] = false;
		end
	end
	
	-- Kill key hook
	if g.keyDown("k") then
		if not(keysDown["k"]) then
			killMonsterAhead();
			keysDown["k"] = true;
		end
	else
		if keysDown["k"] then
			keysDown["k"] = false;
		end
	end
	
	-- Heal key hook
	if g.keyDown("h") then
		if not(keysDown["h"]) then
			party:heal();
			keysDown["h"] = true;
		end
	else
		if keysDown["h"] then
			keysDown["h"] = false;
		end
	end

end

function toggleAhead()
	local dx, dy = getForward(party.facing);
	for e in fix.newEntitiesAt(party.level, party.x, party.y) do
		if e.setDoorState and e.facing == party.facing then e:toggle();	end
	end
	for e in fix.newEntitiesAt(party.level, party.x + dx, party.y + dy) do
		if e.setDoorState and e.facing == (party.facing + 2) % 4 then e:toggle(); end
		if e.setPitState then e:toggle(); end
	end
end

function killMonsterAhead()
	local dx, dy = getForward(party.facing);
	damageTile(party.level, party.x + dx, party.y + dy, party.facing, 125, "physical", 9999);
end
They are even better as Z also toggles pits, and kill gives experience to chars. Notice that the code above uses the "fixed" entitiesAt code (no map markers bug) which you should use anyway in your mod, search the forums for it.

To use that, put it in a script entity, named for example "testShortcuts", and send a call to that from the party object, like this: testShortcuts.party_onDrawGui(g)
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Lua, How Do you see if KeyPressed

Post by Isaac »

What kind of call from the Party object? [specifically]
User avatar
Diarmuid
Posts: 807
Joined: Thu Nov 22, 2012 6:59 am
Location: Montreal, Canada
Contact:

Re: Lua, How Do you see if KeyPressed

Post by Diarmuid »

Basically you need to cloneObject the party in one of the .lua files (like init.lua, or anything else you import from init.lua), and add to it the onDrawGui hook, and place the call inside this.

Problem is, you can ever only clone the party ONCE. Every subsequent clone overwrites the previous one. So if you already have a clone definition of the party, ad the onDrawGui to that.

Here, I provided a handy template that allows you to only use 1 clone which redirects all hooks to a script entity, where you can put everything you like, like that keypresses hook in onDrawGui:

viewtopic.php?f=14&t=5803&hilit=+hooks
User avatar
SnowyOwl47
Posts: 148
Joined: Fri Sep 12, 2014 10:41 pm

Re: Lua, How Do you see if KeyPressed

Post by SnowyOwl47 »

@Diarmuid

Thanks! it works but...
there is one error on toggle ahead it kind find such a thing as fix.
User avatar
Diarmuid
Posts: 807
Joined: Thu Nov 22, 2012 6:59 am
Location: Montreal, Canada
Contact:

Re: Lua, How Do you see if KeyPressed

Post by Diarmuid »

User avatar
SnowyOwl47
Posts: 148
Joined: Fri Sep 12, 2014 10:41 pm

Re: Lua, How Do you see if KeyPressed

Post by SnowyOwl47 »

@Diarmuid Thanks its fully functional now!
Post Reply