Page 1 of 1
Problem attempt to index field "map"
Posted: Sat Mar 05, 2016 1:46 am
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.
Re: Problem attempt to index field "map"
Posted: Sat Mar 05, 2016 2:38 am
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.
Re: Problem attempt to index field "map"
Posted: Sat Mar 05, 2016 9:40 am
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) ?
Re: Problem attempt to index field "map"
Posted: Sat Mar 05, 2016 10:46 am
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
Re: Problem attempt to index field "map"
Posted: Sun Mar 06, 2016 11:06 am
by vieuxchat
Thank you very much
