Anyway to tell from the object if its in a container?

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

Re: Anyway to tell from the object if its in a container?

Post by JKos »

I'm not entirely sure what you are trying to achieve, so this may not be a suitable solution for you, but does it work if you attach a timer component to that particular item GameObject and call self.go:getFullId() from timers onActivate hook? I have not tested this.
- 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
Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Anyway to tell from the object if its in a container?

Post by Azel »

I have not been able to make getFullId() work on a monsters inventory yet, but I'm sure it's possible.

Here is the test I ran

Code: Select all


my_boss.monster:addItem(spawn("battle_axe").item);
for i in my_boss.monster:contents() do
	print(i);
end
Interestingly, "i" is only a number. In this test case, my_boss starts off with a single item. So the Print command would show me a numeric value of "1." Then, when I ran the addItem() command for the battle axe, the Print command would return "1, 2." So it saw the first original item, and then the second item (battle axe) that was added programmatically. That's about where I stopped, as there isn't much I could do yet to find out the ID (or Full ID) of either items via code.

Although, for me, this functionality gives me enough to work with since I can control which Item(s) a monster has at any point. So if the above code returns a certain number I can make certain decisions, without actually knowing the full ID of the item. Hence, since the print command returned "1" and "2" ... I know that the monster has the Battle Axe.
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: Anyway to tell from the object if its in a container?

Post by JKos »

Azel: try

Code: Select all

my_boss.monster:addItem(spawn("battle_axe").item);
for i,item in my_boss.monster:contents() do
   print(i,item);
end
And I tested attaching timer to an item, it doesn't work, the timer is disabled as soon as the item is picked up or placed in container.

Code: Select all

defineObject{
	name = "my_dagger",
	baseObject = "dagger",
	components = {
		{
			class = "Timer",
			onActivate = function(self)
				print(self.go:getFullId())
			end
		}
	}
}
- 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
MrChoke
Posts: 324
Joined: Sat Oct 25, 2014 7:20 pm

Re: Anyway to tell from the object if its in a container?

Post by MrChoke »

JKos wrote:I'm not entirely sure what you are trying to achieve, so this may not be a suitable solution for you, but does it work if you attach a timer component to that particular item GameObject and call self.go:getFullId() from timers onActivate hook? I have not tested this.
The 1st part of my problem was cleanly solved by Petri and that is being able to determine which surface an object is in. getFullId() gives you the ID of the game object of the surface its in.

But part 2 of my problem is when an object is in a container or held by a monster or party, how can I tell which entity it is in? For me, as soon as its held (or contained), the object is basically removed from the game, except by referencing it thru container calls. Its a catch-22 because how do I know which container to call? Also, I cannot call getFullId() on the object because there is no object. Petri says this works but I don't see how....

Would you be able to try it out to make sure I am not missing anything?
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Anyway to tell from the object if its in a container?

Post by minmay »

There IS an object. It's just not on a map, so it can't be located with findEntity() or its id.

Since you seem to be unwilling to believe this, here is a demonstration you can do. Pick up an item, then type this into the console and press enter:

Code: Select all

print(getMouseItem().go)
It will print the address of a table, which is the item's corresponding GameObject.
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
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: Anyway to tell from the object if its in a container?

Post by JKos »

Yeah, so go:getFullId() works even if the item is in container, but the problem is that there is no way to call it without iterating all containers because you can't have a direct reference to it (as far as I know).
So I think that you really have to use some other way. One possible way is to use modifyObjects-script:
viewtopic.php?f=22&t=8450

Code: Select all

modifyObjects{
   filter = {
      hasComponents = {'containeritem'}
   },
   components = {
      {
         name = 'containeritem',
         onInsertItem = function(self,item)
			print(item.go.id,'is now in',self.go.id)
         end
      }
   }

}
This adds that same onInsertItem-hook to all components named as containeritem.
- 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
MrChoke
Posts: 324
Joined: Sat Oct 25, 2014 7:20 pm

Re: Anyway to tell from the object if its in a container?

Post by MrChoke »

JKos wrote:Yeah, so go:getFullId() works even if the item is in container, but the problem is that there is no way to call it without iterating all containers because you can't have a direct reference to it (as far as I know).
So I think that you really have to use some other way. One possible way is to use modifyObjects-script:
viewtopic.php?f=22&t=8450

Code: Select all

modifyObjects{
   filter = {
      hasComponents = {'containeritem'}
   },
   components = {
      {
         name = 'containeritem',
         onInsertItem = function(self,item)
			print(item.go.id,'is now in',self.go.id)
         end
      }
   }

}
This adds that same onInsertItem-hook to all components named as containeritem.
Ok. I did not do any of the new framework stuff though like modifyObjects. I get the idea though. I am going to have a finite number of containers in the game, as well as a finite number of monsters who can hold objects and of course one party. So I will have otr setup the appropriate hooks for these. Thanks.
Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Anyway to tell from the object if its in a container?

Post by Azel »

JKos wrote:Azel: try

Code: Select all

my_boss.monster:addItem(spawn("battle_axe").item);
for i,item in my_boss.monster:contents() do
   print(i,item);
end
Good stuff! If I use that, I can ask for the item.go.name to see "battle_axe" as the result. The actual ID of the battle axe, however, is still elusive.

Code: Select all

my_boss.monster:addItem(spawn("battle_axe").item);
for i,item in my_boss.monster:contents() do
   print(i,item.go:getFullId());
end
Using the above code, if the Monster starts off with a Skull as an item, and then the battle axe is added programmatically, the Print command returns the following:

my_boss.skull_1
my_boss.45680

:o

No need to spend too much energy on this on my account, though. Since I am not using this in my Mod. Just a fun thing to try reading through this specific thread. Helpful for any future ideas too!
Post Reply