Page 1 of 6

Spike trap script help

Posted: Tue Apr 14, 2015 12:36 am
by Killcannon
I'm setting up a spawned spike trap system and have run into a roadblock with the damage. This is what I have so far. I have the room where the spikes are, and a lever that activates and deactivates the spike trap script. I then have hidden pressure plates put down at the spot where the spike traps are and when a player/monster walks over them they take damage. Here's the problem: when a monster takes damage it crashes the game and produces an error "attempt to index a nil value". I know I could disable the traps from triggering when a monster walks over it, but I actually want monsters to take damage via the traps.

Here's the related scripts:

Spike trap activation/deactivation
SpoilerShow
-- spawns spike traps and despawns them with lever press

function spiketrap()
if lever_4:getLeverState() == "activated" then
spawn("dm_spikes_floortrap", 1, 11, 8, 0, "spikes1")
spawn("dm_spikes_floortrap", 1, 12, 8, 0, "spikes2")
spawn("dm_spikes_floortrap", 1, 13, 8, 0, "spikes3")
spawn("dm_spikes_floortrap", 1, 11, 10, 0, "spikes4")
spawn("dm_spikes_floortrap", 1, 12, 10, 0, "spikes5")
spawn("dm_spikes_floortrap", 1, 13, 10, 0, "spikes6")
spawn("dm_spikes_floortrap", 1, 11, 9, 0, "spikes7")
spawn("dm_spikes_floortrap", 1, 13, 9, 0, "spikes8")
playSound("pressure_plate_pressed")
else
if lever_4:getLeverState() == "deactivated" then
spikes1:destroy()
spikes2:destroy()
spikes3:destroy()
spikes4:destroy()
spikes5:destroy()
spikes6:destroy()
spikes7:destroy()
spikes8:destroy()
playSound("pressure_plate_released")
else
return false
end
end
end
spike trap damage
SpoilerShow
-- spike damage script

function spikedamage()

local spikes = findEntity("spikes1");
if (spikes ~= nil) then
damageTile(1, 11, 8, 0, 6, "physical", 30)
damageTile(1, 12, 8, 0, 6, "physical", 30)
damageTile(1, 13, 8, 0, 6, "physical", 30)
damageTile(1, 11, 10, 0, 6, "physical", 30)
damageTile(1, 12, 10, 0, 6, "physical", 30)
damageTile(1, 13, 10, 0, 6, "physical", 30)
damageTile(1, 11, 9, 0, 6, "physical", 30)
damageTile(1, 13, 9, 0, 6, "physical", 30)
else
return false
end
end
My testing has revealed that this only happens when a monster crosses that tile, and it does not matter what type of monster; even custom monsters will cause the crash. Player characters take damage as normal. The error itself is being produced on the DamageTile line itself. My guess is that since I have all the spikes activating the damage even if that tile doesn't have anything on it; the blank tiles are causing the error as there's no entity to damage; but if that was true why does it not crash when a player crosses over it? Any advice would be great!

Re: Spike trap script help

Posted: Tue Apr 14, 2015 5:34 am
by minmay
Grimrock 1 has a bug where physical damageTile on monsters usually crashes. I just use poison damage with the ignore immunities flag (bit 6) on. This doesn't work for damaging obstacles but I believe it's indistinguishable from physical damage against monsters and the party.

Re: Spike trap script help

Posted: Tue Apr 14, 2015 11:59 pm
by Killcannon
Thanks Minmay! Changing the damage type worked perfectly! Thank you so much for the assistance! Back to setting up puzzles and traps!

Re: Spike trap script help

Posted: Thu Apr 16, 2015 10:24 am
by maneus
I have no problems using "physical" damage in game.

I use this:
damageTile(level, x, y, direction, 1, "physical", 30)

Have tested it and got no crash.

Re: Spike trap script help

Posted: Thu Apr 16, 2015 10:59 am
by minmay
Are you certain that never causes a crash on monsters? I would not expect that damage flag (or any of the others) to prevent it.

Re: Spike trap script help

Posted: Thu Apr 16, 2015 10:10 pm
by maneus
I have connected a hidden_pressure_plate to a timer which is connected to the damage script.
I have not tested it for a larger time. Only 4-5 activations by a monster.

EDIT: Have tested it for a longer time and got no crash in normal game. So all works fine.

Re: Spike trap script help

Posted: Sun Apr 19, 2015 9:39 pm
by Killcannon
Here's what I'm running because it still is crashing for me whenever I set the damage setting to physical. Perhaps it's in how I setup the scripting?

I needed a script to spawn the actual trap as the trap is activated/deactivated via a lever and the spikes do not have a deactivate function in themselves.
SpoilerShow
-- spawns spike traps and despawns them with lever press

function spiketrap()
if lever_4:getLeverState() == "activated" then
spawn("dm_spikes_floortrap", 1, 11, 8, 0, "spikes1")
spawn("dm_spikes_floortrap", 1, 12, 8, 0, "spikes2")
spawn("dm_spikes_floortrap", 1, 13, 8, 0, "spikes3")
spawn("dm_spikes_floortrap", 1, 11, 10, 0, "spikes4")
spawn("dm_spikes_floortrap", 1, 12, 10, 0, "spikes5")
spawn("dm_spikes_floortrap", 1, 13, 10, 0, "spikes6")
spawn("dm_spikes_floortrap", 1, 11, 9, 0, "spikes7")
spawn("dm_spikes_floortrap", 1, 13, 9, 0, "spikes8")
playSound("pressure_plate_pressed")
else
if lever_4:getLeverState() == "deactivated" then
spikes1:destroy()
spikes2:destroy()
spikes3:destroy()
spikes4:destroy()
spikes5:destroy()
spikes6:destroy()
spikes7:destroy()
spikes8:destroy()
playSound("pressure_plate_released")
else
return false
end
end
end
Then the Damage Script
SpoilerShow
-- spike damage script

function spikedamage()

local spikes = findEntity("spikes1");
if (spikes ~= nil) then
damageTile(1, 11, 8, 0, 6, "poison", 30)
damageTile(1, 12, 8, 0, 6, "poison", 30)
damageTile(1, 13, 8, 0, 6, "poison", 30)
damageTile(1, 11, 10, 0, 6, "poison", 30)
damageTile(1, 12, 10, 0, 6, "poison", 30)
damageTile(1, 13, 10, 0, 6, "poison", 30)
damageTile(1, 11, 9, 0, 6, "poison", 30)
damageTile(1, 13, 9, 0, 6, "poison", 30)
else
return false
end
end
Essentially the point of this is to check to see if the spikes are spawned and if they are to damage player/monster that is on that tile. Yes it's intentional for monsters to be damaged by this. ^.^ It's quite basic as well, but I'm still learning; so there's most definitely better ways to do this I've just not figured it out yet. Also what I'd like to do is have the damage run 'per tile' without having to do individual scripts for each tile. I'm sure there's a way to do so, but don't know enough about it to do so.

Re: Spike trap script help

Posted: Sun Apr 19, 2015 11:52 pm
by Isaac
Killcannon wrote:I needed a script to spawn the actual trap as the trap is activated/deactivated via a lever and the spikes do not have a deactivate function in themselves.
You should be able to either set the plates to not respond to items/party/ and monsters; or use a script as proxy to the connection, and implement an on/off switch for your spike trap in that script, with an if condition (enclosing your trap code), and a boolean. Set the boolean to false, and the plate triggers, but nothing happens.
Also what I'd like to do is have the damage run 'per tile' without having to do individual scripts for each tile. I'm sure there's a way to do so, but don't know enough about it to do so.
You can connect all of your traps to a single 'trap' script that examines what object calls it, and uses that object for parameters to the damageTile function. Objects all have a unique id, and your script can check for that, and then have access to the x/y location of the trap.

*However, if you are using a proxy script, then that script should either be the trap script itself, or pass along what triggered it.

Re: Spike trap script help

Posted: Mon Apr 20, 2015 4:16 am
by Killcannon
Okay I figured out how to turn on/off the 'activate by' property of the traps itself via a script for a lever and can setup the boolean table without too much issue.
You can connect all of your traps to a single 'trap' script that examines what object calls it, and uses that object for parameters to the damageTile function. Objects all have a unique id, and your script can
check for that, and then have access to the x/y location of the trap.
Here's the problem I'm having now. How do I get my Damagetile to only damage the tile that it's being activated on? I don't want the party to randomly die because some monster stepped over an active pitfall trap elsewhere in the dungeon. Would someone mind providing an example of how this works so I can look at it; or a link to an example?

Re: Spike trap script help

Posted: Mon Apr 20, 2015 5:44 am
by Isaac

Code: Select all

--trap_script
armed = true

function setTrap(set)
	if set then
		armed = true
	 else armed = false	
	end
end

function trap(t)
	if armed and t.x ~= nil then
		damageTile(t.level, t.x, t.y, 0, 64 , "poison", 1)
	end
end
Demo map: https://www.dropbox.com/s/o5ehl651416iu ... e.zip?dl=0