Page 1 of 2
rare "bad item" problem, script loading order?
Posted: Thu Nov 29, 2012 1:16 pm
by Komag
Every so often when I run the preview I get a somewhat random "bad item" crash pointing to some item I have in the dungeon at dungeon start, via one of my script entities, such as this one:
Code: Select all
dungeon_alcove_36:addItem(spawn("scroll"):setScrollText([[
Restore Energy
1 flask
1 blooddrop blossom
]]))
it's just script_entity_28 sitting next to the alcove, set to fill it with that scroll at dungeon start (no function, no delay).
I have a bunch of these set up, all over the place. I only get the "bad item" crash, maybe, 1 in 30 previews, and I always just hit preview Play again and it works fine after that.
Is this a situation of dungeon loading stuff in not the same reliable order every time? What should I do?
Re: rare "bad item" problem, script loading order?
Posted: Thu Nov 29, 2012 1:25 pm
by Xanathar
If (and I'm not sure, but let's suppose so) it's a problem of creation order, you can either
Solution #1 - The GrimQ solution (shameless promotion
)
- Just setup grimq in your dungeon, and put the code you want to be executed in an autoexec() method in whatever scripting entity you want.
Solution #2 - The manual solution
For each case of code you want to auto run at startup, put it into a function, and then put this code in the same entity outside that funcion:
Code: Select all
spawn("pressure_plate_hidden", party.level, party.x, party.y, 0)
:setTriggeredByParty(true)
:setTriggeredByMonster(false)
:setTriggeredByItem(false)
:setActivateOnce(true)
:setSilent(true)
:addConnector("activate", self.id, "methodname")
Hope this helps
Disclaimer: I'm at work so I can't test now
Re: rare "bad item" problem, script loading order?
Posted: Thu Nov 29, 2012 6:43 pm
by Ancylus
I encountered similar trouble with spawning items into containers earlier, but didn't have time to look into it at the time. Now I decided to experiment a bit. I created an new dungeon, placed a script entity, and gave it the following code:
Code: Select all
for i = 1, 1000 do
local box = spawn("wooden_box")
box:addItem(spawn("rock"))
box:addItem(spawn("rock"))
box:addItem(spawn("rock"))
box:addItem(spawn("rock"))
box:addItem(spawn("rock"))
box:addItem(spawn("rock"))
box:addItem(spawn("rock"))
box:addItem(spawn("rock"))
box:addItem(spawn("rock"))
box:addItem(spawn("rock"))
end
The result is that the script crashes much of the time when I press Play, but not always the same way. I've observed both "bad self" and "bad argument #1 to 'addItem' (bad object)" errors at the spawning of various different rocks. And sometimes nothing goes wrong and the game starts fine.
Considering that my test dungeon contains nothing but the starting location, the initial torch holder and this script, and the script never refers to the other entities, I doubt the creation order is an issue here. My best guess is that there's a bug in the LUA interpreter or the spawn method.
Re: rare "bad item" problem, script loading order?
Posted: Thu Nov 29, 2012 6:51 pm
by Xanathar
I tried,
I confirm it happens, and it also happens when linked to a connector (so well after everything is being initialized).
The error - "bad object" - is something usually encountered when referring to a destroyed object... maybe the garbage collector is being a little too aggressive (as in, bug) ?
Re: rare "bad item" problem, script loading order?
Posted: Thu Nov 29, 2012 7:19 pm
by Komag
yeah, I tested this as well, the exact same test, and then I stuck it in a function that fired from a timer (0.1seconds) after the map starts, same results, often an error, but not always.
I raised the count to 50000, which takes 9 seconds on my computer to start! I thought this would increase the likelihood of getting the error tremendously, but it doesn't seem to have any noticeable effect on how often the error shows up. Interestingly, whenever the error DID show up, it was ALWAYS within the first second after clicking Play, never after that, such as in second 3 or 7.
10 - never got error, 200 times tested starting dungeon
100 - same, no errors in 200 tests
1000 - got 24 errors in 100 tests
1000 got 29 in another 100
500 - 4 in 100
500 - 1 in 100
500 - 2 in 100
10000 - 11 in 40
but sometimes weird stretches would occur with no errors. My last test of 10000 listed there, all 11 errors were in the first 20 tests, then the next 20 tests had none at all. earlier I tested 1000, got lots of errors regularly, then got like 50 in a row with no errors! So I don't know what's going on.
----------------
hopefully this is something that gets "cooked" out during export, so there isn't any chance end players will see the error when trying to start playing a dungeon.dat
Re: rare "bad item" problem, script loading order?
Posted: Thu Nov 29, 2012 9:02 pm
by petri
This is a known issue with the garbage collector & object reference system. To prevent collecting the wooden box it should be anchored after spawning to something. Either spawn it in the level, insert it to inventory slot or make a monster carry it. It's a bit inconvenient and unfortunately difficult to fix without big changes to the engine.
Re: rare "bad item" problem, script loading order?
Posted: Thu Nov 29, 2012 9:07 pm
by petri
Argh, I just realized if you're really unlucky the scroll in Komag's original post could be collected just before it's passed to addItem
Re: rare "bad item" problem, script loading order?
Posted: Thu Nov 29, 2012 9:54 pm
by JKos
I hope that you mean like as "unlucky" as winning in the lottery. What are the chances for that approximately? How often the garbage is collected?
Re: rare "bad item" problem, script loading order?
Posted: Thu Nov 29, 2012 10:18 pm
by Komag
that must be what's happening to me every so often. I have so many things like this in my dungeon, the rare chances add up over time to a hit.
Any way to set it up to reduce/eliminate the chance of problem?
If it's not a problem after export, then I can just live with it
Re: rare "bad item" problem, script loading order?
Posted: Thu Nov 29, 2012 11:25 pm
by Batty
Yeah, I have the exact same scroll thing happening also, no problem for me if it's editor only but can this happen after export?
Instead, should we be doing:
Code: Select all
dungeon_alcove_1:addItem(spawn("scroll", nil, nil, nil, nil, "the_best_scroll"))
the_best_scroll:setScrollText("I am the best!")
edit: my assumption is that it was spawning the scroll, adding the text & then adding to the alcove and in that moment of adding text it got collected.