rare "bad item" problem, script loading order?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

rare "bad item" problem, script loading order?

Post 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?
Finished Dungeons - complete mods to play
User avatar
Xanathar
Posts: 629
Joined: Sun Apr 15, 2012 10:19 am
Location: Torino, Italy
Contact:

Re: rare "bad item" problem, script loading order?

Post 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 :mrgreen: )
- 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 :)
Waking Violet (Steam, PS4, PSVita, Switch) : http://www.wakingviolet.com

The Sunset Gate [MOD]: viewtopic.php?f=14&t=5563

My preciousss: http://www.moonsharp.org
Ancylus
Posts: 50
Joined: Thu Oct 11, 2012 5:54 pm

Re: rare "bad item" problem, script loading order?

Post 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.
User avatar
Xanathar
Posts: 629
Joined: Sun Apr 15, 2012 10:19 am
Location: Torino, Italy
Contact:

Re: rare "bad item" problem, script loading order?

Post 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) ?
Waking Violet (Steam, PS4, PSVita, Switch) : http://www.wakingviolet.com

The Sunset Gate [MOD]: viewtopic.php?f=14&t=5563

My preciousss: http://www.moonsharp.org
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: rare "bad item" problem, script loading order?

Post 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
Finished Dungeons - complete mods to play
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: rare "bad item" problem, script loading order?

Post 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.
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: rare "bad item" problem, script loading order?

Post 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 :(
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: rare "bad item" problem, script loading order?

Post by JKos »

:shock: 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?
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: rare "bad item" problem, script loading order?

Post 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
Finished Dungeons - complete mods to play
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: rare "bad item" problem, script loading order?

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