additem

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
object(10)
Posts: 1
Joined: Thu Sep 08, 2016 12:56 am

additem

Post 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
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: additem

Post 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.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: additem

Post 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.
Post Reply