Problem attempt to index field "map"

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
vieuxchat
Posts: 48
Joined: Wed Mar 02, 2016 3:14 pm

Problem attempt to index field "map"

Post by vieuxchat »

Code: Select all

function createMagicScroll()

	local s = spawn("scroll",party.level, party.x, party.y, 0, 0)
	s.scrollitem:setScrollText("Blabla")
	s:createComponent("ItemAction")
	s.itemaction:addConnector("onAttack", "script_entity_scroll_teleport", "teleportSiAuBonEndroit")
	s.item:setSecondaryAction("itemaction")
	
end

function teleportSiAuBonEndroit(self, champion, slot)
	print("Test - Teleport")
end
Here is the code to create a custom scroll that should teleport the party somewhere else. I created that script in the editor, and when I launch I have an error message "attempt to index field 'map'".
But I don't see where my code should try to access the field map of whatever object it may try to access.
The script that creates the scroll runs fine. It just when I use the secondary action of the scroll that I get the error message.
The function attached to the "onAttack" hook don't even fire itself (I don't get the print "Test - Teleport" on the console)

I don't know what to do. I tried touse hook on the ItemComponent, I tried to use the hook on a MeleeAttackComponent after adding one to the scroll (yeah Scroll Attack for the win !) and after each o fthose tries, I got the error message.
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Problem attempt to index field "map"

Post by minmay »

When a connector is triggered it uses the triggering component's GameObject's map field. Because of this, you should never add a connector to any component belonging to an object that may be removed from the map (and therefore have a nil map field), such as items. Instead, put your code in the corresponding hook in the object definition. Should you want to add dynamic hooks to objects, there is already a library to handle that for you that you can use in place of connectors.
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.
vieuxchat
Posts: 48
Joined: Wed Mar 02, 2016 3:14 pm

Re: Problem attempt to index field "map"

Post by vieuxchat »

Oh.
It's a serious limitation.
Isn't there a way to add the "map" field to an item (for instance copy the party.map) ?
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Problem attempt to index field "map"

Post by minmay »

No, you are not going to be able to add a map field to an item that is not on any map.
This isn't a big limitation, I linked a solution that lets you accomplish exactly the same thing as dynamically adding a connector. Just install it, then define an object like:

Code: Select all

defineObject{
    name = "scroll_magic",
    baseObject = "scroll",
    components = {
        fw_addHooks{
            class = 'ItemAction',
        }
    }
}
Then your createMagicScroll() function would be:

Code: Select all

function createMagicScroll()

   local s = spawn("scroll_magic",party.level, party.x, party.y, 0, 0)
   s.scrollitem:setScrollText("Blabla")
   fw.script:set(s.id.."@itemaction.blah.onAttack",function(hook,self) script_entity_scroll_teleport.script.teleportSiAuBonEndroit(self) end)
   s.item:setSecondaryAction("itemaction")
   
end
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.
vieuxchat
Posts: 48
Joined: Wed Mar 02, 2016 3:14 pm

Re: Problem attempt to index field "map"

Post by vieuxchat »

Thank you very much :)
Post Reply