Page 1 of 1

Lua, How Do you see if KeyPressed

Posted: Fri Sep 12, 2014 10:45 pm
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.

Re: Lua, How to see if KeyPressed

Posted: Fri Sep 12, 2014 10:46 pm
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.

Re: Lua, How Do you see if KeyPressed

Posted: Mon Sep 15, 2014 8:19 am
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)

Re: Lua, How Do you see if KeyPressed

Posted: Tue Sep 30, 2014 10:45 pm
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)

Re: Lua, How Do you see if KeyPressed

Posted: Wed Oct 01, 2014 12:34 am
by Isaac
What kind of call from the Party object? [specifically]

Re: Lua, How Do you see if KeyPressed

Posted: Thu Oct 02, 2014 5:51 am
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

Re: Lua, How Do you see if KeyPressed

Posted: Thu Oct 02, 2014 10:05 pm
by SnowyOwl47
@Diarmuid

Thanks! it works but...
there is one error on toggle ahead it kind find such a thing as fix.

Re: Lua, How Do you see if KeyPressed

Posted: Thu Oct 02, 2014 10:39 pm
by Diarmuid

Re: Lua, How Do you see if KeyPressed

Posted: Thu Oct 02, 2014 10:48 pm
by SnowyOwl47
@Diarmuid Thanks its fully functional now!