Ask a simple question, get a simple answer

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!
SluisE
Posts: 8
Joined: Fri Nov 30, 2018 4:56 pm

Re: Ask a simple question, get a simple answer

Post by SluisE »

Isaac wrote: Sun Mar 15, 2020 4:49 am
SluisE wrote: Sat Mar 14, 2020 8:22 pm Use destroyDelayed instead of destroy. That way the item is destroyed when you end the function.
This is very good; best practice I think. (And thanks for the comment on it. 8-) )

But what really happens under the hood when the object is destroyed (by the usual method)? Is its position shifted? Can an entry be missed?

It seems to make sense to almost always (or to only) use destroyDelayed just as a matter of course, because it does bypass some nasty crashes in some situations. But does anyone know (or can reasonably speculate on) what could actually happen if an object gets destroyed in the iterator loop—and is never accessed again? Could it actually crash?

minmay will tell you (with good reason) that the monster.contents iterator is damaged, or broken; it DOES (or can? IIRC) cause crashes when it returns nil. I never had a crash with it after I added a 'pocket_lint' item before using it. :lol:
I don't know what happens internally, but you could try this test (I would do it myself but right now I'm too ill/tired/whatever to turn on the computer):

Put an alcove in your dungeon and put three different items in it.
Put a button next to it and connect it to a script.
In the script you do three things:
1) Iterate throught the contents and hudPrint the item-names. Shouldn't be too surprising.
2) Iterate through the contents and hudPrint the names, but destroy the first item (not destroyDelayed) after hudPrinting it. Are all three items now printed or only two, and if so, which two?
3) As in 2, but apart form destroying the item also spawn and add a new item. What happens now?

The answers might give you an idea (but no certainty) about the inner workings of the iteration.
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Basically, the contents of a SurfaceComponent are in an array, and new items get added to the end of the array, and removing an item will shift down the positions of the other items in the array. I think destroying an item in the middle of iterating over the contents with contents() will indeed result in skipping the next item in the array, which is not something you want.

destroyDelayed() is perfect for this situation; it destroys the object at the end of the frame instead of immediately.
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
Eleven Warrior
Posts: 745
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

Re: Ask a simple question, get a simple answer

Post by Eleven Warrior »

I'm using this code (Below) and have added Else and the line: hudPrint("I dont want that "..item:getUiName()) but the problem is it wont work with this line added. What is going wrong?

Code: Select all

--- Single Quest item for reward ---
function SpawnRewards()
  local s = altar_2.surface
    for _,e in s:contents() do
      if e.go.name == "dagger" then
        e.go:destroy()
	s:addItem(spawn("fire_gauntlets").item)
	s.go:spawn('teleportation_effect')
	     else
	hudPrint("I dont want that "..item:getUiName())
	  end
	end
end
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

It cannot work; item:getUiName() —by itself— refers to an object that doesn't exist in the function. You need to use the component of e for the item name; e.go.item:getUiName().

Code: Select all

function SpawnRewards()
  local s = altar_2.surface
    for _,e in s:contents() do
      if e.go.name == "dagger" then
        e.go:destroyDelayed()
		s:addItem(spawn("fire_gauntlets").item)
		s.go:spawn('teleportation_effect')
	  else
		hudPrint("I dont want that "..e.go.item:getUiName())
	  end
	end
end
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: Ask a simple question, get a simple answer

Post by bongobeat »

Is there some better quality log2 portrait? At least 256x256?
Hidden in the dat file, or never published?
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
User avatar
Eleven Warrior
Posts: 745
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

Re: Ask a simple question, get a simple answer

Post by Eleven Warrior »

Has anyone got a bunch or Potion objects? I need some new potion for my mod there are only a small amount with the assest pack ty :)
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

There are larger (but jpeg) versions of a few of the portraits here: http://www.grimrock.net/2013/08/16/maki ... portraits/
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
Eleven Warrior
Posts: 745
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

Re: Ask a simple question, get a simple answer

Post by Eleven Warrior »

I trying to get this potion to work when the champ drinks it the food bar should go up but, this potion here is not working Help...

defineObject{
name = "potion_restore",
baseObject = "base_item",
components = {
{
class = "Model",
model = "assets/models/items/flask.fbx",
},
{
class = "Item",
uiName = "Potion of Restoration",
gfxIndex = 148,
weight = 0.3,
stackable = true,
description = "Drinking the potion will cause the champion to be bestowed with a torrent of powerful energy and healing that cleanses ailments and provides nourishment.",
traits = { "potion" },
},
{
class = "UsableItem",
sound = "consume_potion",
emptyItem = "flask",
onUseItem = function(self, champion)
playSound("consume_potion")
champion:regainFood(350) --- This wrong it crashes
return true
end,
},
},
tags = { "zim_assets" },
}
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: Ask a simple question, get a simple answer

Post by bongobeat »

minmay wrote: Wed Mar 18, 2020 4:35 am There are larger (but jpeg) versions of a few of the portraits here: http://www.grimrock.net/2013/08/16/maki ... portraits/
Thanks but I already have those.

May I ask how you did the dm portraits in the dm ressource asset?
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: Ask a simple question, get a simple answer

Post by bongobeat »

Eleven Warrior wrote: Wed Mar 18, 2020 3:59 am Has anyone got a bunch or Potion objects? I need some new potion for my mod there are only a small amount with the assest pack ty :)
you can find different flasks in there:
https://www.nexusmods.com/grimrock/mods ... D422&pUp=1
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
Post Reply