Page 1 of 1

quick item spawn question

Posted: Mon Dec 31, 2012 2:01 am
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 ;)

Re: quick item spawn question

Posted: Mon Dec 31, 2012 2:06 am
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

Re: quick item spawn question

Posted: Mon Dec 31, 2012 2:08 am
by Hariolor
didn't test this yet - but I think you can do

if CONDITION1 and fancybrasskey == nil

then

Whatever you want to do.

Re: quick item spawn question

Posted: Mon Dec 31, 2012 2:25 am
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.

Re: quick item spawn question

Posted: Mon Dec 31, 2012 3:54 am
by stochastic
Interesting,

I cannot get your script to work - or in other words - no key is spawned :/

Re: quick item spawn question

Posted: Mon Dec 31, 2012 5:23 am
by Komag
drop the key =, just spawn it

(I also like using flags, very intuitive to me)

Re: quick item spawn question

Posted: Mon Dec 31, 2012 7:20 am
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!

Re: quick item spawn question

Posted: Wed Jan 02, 2013 6:22 pm
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.

Re: quick item spawn question

Posted: Wed Jan 02, 2013 6:29 pm
by stochastic
(fixed, nevermind)