Page 1 of 1

additem

Posted: Thu Sep 08, 2016 1:21 am
by object(10)
Hello,
I've started modding and was having an issue with my spawn script.

function spawntest()

spawn("spider", 1, 4, 18, 0):addItem(spawn("brass_key"))

end

something about the additem part is wrong because if I remove it the spider spawns as expected. With the code it says - attempt to call method 'addItem' (a nil value)

Any ideas?
Thanks

Re: additem

Posted: Thu Sep 08, 2016 3:53 am
by minmay
Read the scripting reference. This isn't Grimrock 1; spawn() won't return a Monster, it returns a GameObject, which is a container for Components. You need to call MonsterComponent:addItem() on the object's MonsterComponent, not the parent GameObject. Similarly, you need to pass an ItemComponent to MonsterComponent:addItem(), not a GameObject, even if that GameObject happens to contain an ItemComponent.
Also remember to specify elevation in your spawn call.

Code: Select all

spawn("spider",1,4,18,0,0).monster:addItem(spawn("brass_key").item)
You can find a more complete list of Component methods here, although it is still missing some.

Re: additem

Posted: Thu Sep 08, 2016 4:58 am
by Isaac
The scripting model in LoG2 is an unexpected change, and confuses many on their first attempts; coming from Grimrock 1. On the plus side, the system is far more powerful and versatile. On the downside... Much of what one thinks they know [from experience with LoG1], does not work in LoG2 without modification. Foremost: all game objects now contain component functionality. Buttons have a button component, floor plates have a floor_trigger component... and even the party has a "party" component; which means that interaction with the champions looks like this: party.party:getChampion(1)

One of the best things you can do first, is to download the Grimrock 2 asset pack, and read the contained scripts. It shows how the official asset behaviors are scripted.