quick item spawn question

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
stochastic
Posts: 15
Joined: Fri Dec 28, 2012 11:59 pm

quick item spawn question

Post by stochastic »

--Spawn key
function l1rspwnkey()
if knap_text_5:isDown() == true and
<NEED SOMETHING HERE>

then hudPrint("Oh look! There is a key on the floor.")
key = spawn("brass_key", self.level, 8, 6, 3, "FANCYBRASSKEY")
end
end

Comment:
The problem with this script is, I want it to check, if there already is a key by that name - and if "yes" it shall not spawn the key and hudprint.

(If I don't do that, the game will crash for spawning 2 items with the same ID) - so this has to work anyway ;)
xbdj
Posts: 32
Joined: Sun Dec 30, 2012 7:43 pm

Re: quick item spawn question

Post by xbdj »

Can't you fix it with 2 if-blocks?

function l1rspwnkey()
if knap_text_5:isDown() == true then
if (new condition)
then hudPrint("Oh look! There is a key on the floor.")
key = spawn("brass_key", self.level, 8, 6, 3, "FANCYBRASSKEY")
end
end
end
Hariolor
Posts: 42
Joined: Sun Apr 15, 2012 5:59 am

Re: quick item spawn question

Post by Hariolor »

didn't test this yet - but I think you can do

if CONDITION1 and fancybrasskey == nil

then

Whatever you want to do.
dasarby
Posts: 28
Joined: Sun Dec 30, 2012 10:46 pm

Re: quick item spawn question

Post by dasarby »

I tend to use a script level variable as a flag for this purpose:

Code: Select all

keySpawned = false
function l1rspwnkey()
	if knap_text_5:isDown() == true and not keySpawned then 
		hudPrint("Oh look! There is a key on the floor.")
		key = spawn("brass_key", self.level, 8, 6, 3, "FANCYBRASSKEY")
		keySpawned = true
	end
end
the keySpawned variable will be persisted in the save.
stochastic
Posts: 15
Joined: Fri Dec 28, 2012 11:59 pm

Re: quick item spawn question

Post by stochastic »

Interesting,

I cannot get your script to work - or in other words - no key is spawned :/
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: quick item spawn question

Post by Komag »

drop the key =, just spawn it

(I also like using flags, very intuitive to me)
Finished Dungeons - complete mods to play
dasarby
Posts: 28
Joined: Sun Dec 30, 2012 10:46 pm

Re: quick item spawn question

Post by dasarby »

Hmm. The script works as is for me, assuming that you have pressure plate called knap_text_5... (though Komag is right, you don't need the 'key =' part).

Maybe you don't actually have the script connected to the pressure plate? You need to add a connector from the plate object to the script, and set the action to "l1spwnkey" Then your function will only run when the plate is activated, and the flag will stop the function from running twice. In this case, since the function is only every ever called when the plate is activated, you don't need to check isDown(), simplifying your script to:

Code: Select all

keySpawned = false
function l1rspwnkey()
   if not keySpawned then 
      hudPrint("Oh look! There is a key on the floor.")
      spawn("brass_key", self.level, 13, 16, 3, "FANCYBRASSKEY")
      keySpawned = true
   end
end
Furthermore, you can spawn the key at the location of the plate by referencing the optional parameter to your function. When the connector is triggered, the pressure plate is passed into the function, then you can pull the level, x, and y values from the location of the plate.

Code: Select all

keySpawned = false
function l1rspwnkey(plate)
   if not keySpawned then 
      hudPrint("Oh look! There is a key on the floor.")
      key = spawn("brass_key", plate.level, plate.x, plate.y, 3, "FANCYBRASSKEY")
      keySpawned = true
   end
end
This has the added advantage that you can move the pressure plate around (even across levels) and the key will still spawn!
stochastic
Posts: 15
Joined: Fri Dec 28, 2012 11:59 pm

Re: quick item spawn question

Post by stochastic »

Yeah thanks, I think that will work, and I have used the self.x, self.y or plate.x, plate.y a few times in my dungeon. I must say for someone like me, who is new to scripting in general, this seems quite logic, when you know the functions. The tricky part is just to set it up right ;) - thanks for your help and time.
stochastic
Posts: 15
Joined: Fri Dec 28, 2012 11:59 pm

Re: quick item spawn question

Post by stochastic »

(fixed, nevermind)
Post Reply