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!
Slava
Posts: 21
Joined: Sun Aug 30, 2015 2:10 pm

Re: Ask a simple question, get a simple answer

Post by Slava »

Hello!
Yes it is possible.
Moreover, there are 2 ways for this: standard math.random() and https://www.grimrock.net/modding/script ... nneTwister.
You can also change the properties of objects in the onInit functions of components. For objects with minimalSaveState this will have an even more unpredictable effect)))
Well, after all, food is usable and has an onUse hook in which you can register a random effect. But this will not be displayed in the interface on the nutritional bar.
Hiker
Posts: 9
Joined: Sun Nov 19, 2023 1:43 pm

Re: Ask a simple question, get a simple answer

Post by Hiker »

Yes, thanks, onInit does allow you to create random objects, but for some reason if they are placed in containers on the map it does not work. However, if you put them in a script, then everything is ок - why? Are there any conditions under which onInit does not execute?
IttaBitta
Posts: 21
Joined: Thu Oct 22, 2020 12:14 am

Re: Ask a simple question, get a simple answer

Post by IttaBitta »

I forgot how to offset objects in the world, and I'm trying to put some torches on pillars.
How do????
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Use GameObject:setWorldPosition(). Make sure the object you're offsetting doesn't have minimalSaveState, though.
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.
IttaBitta
Posts: 21
Joined: Thu Oct 22, 2020 12:14 am

Re: Ask a simple question, get a simple answer

Post by IttaBitta »

minmay wrote: Mon Jul 22, 2024 4:40 pm Use GameObject:setWorldPosition(). Make sure the object you're offsetting doesn't have minimalSaveState, though.
Can I get an example of it's use? So as to understand the syntax?
Slava
Posts: 21
Joined: Sun Aug 30, 2015 2:10 pm

Re: Ask a simple question, get a simple answer

Post by Slava »

Hiker wrote: Fri Jul 19, 2024 7:23 pm Yes, thanks, onInit does allow you to create random objects, but for some reason if they are placed in containers on the map it does not work. However, if you put them in a script, then everything is ок - why? Are there any conditions under which onInit does not execute?
Hmm... I don't know. Apparently the hook only works when an object is created on the map. When an object is placed in a container, it is removed from the map. This means that the hook is not called during creation since the object is not on the map.
Slava
Posts: 21
Joined: Sun Aug 30, 2015 2:10 pm

Re: Ask a simple question, get a simple answer

Post by Slava »

IttaBitta wrote: Mon Jul 22, 2024 6:17 pm
minmay wrote: Mon Jul 22, 2024 4:40 pm Use GameObject:setWorldPosition(). Make sure the object you're offsetting doesn't have minimalSaveState, though.
Can I get an example of it's use? So as to understand the syntax?
GameObject:getWorldPosition()
Returns 3 values, X, Y and Z for the world position of the object. World position coordinates have Y-axis as up/down, X-axis is east/west, Z-axis is north/south. The cube that represent one game square is (3.0 x 3.0 x 3.0) world position units in size.
GameObject:setWorldPosition(vec) - It is written that this method accepts a vector. However, it accepts 3 numbers separated by commas, but not vec(x, y, z). I used it like this: g1_bookshelf_01_18:setWorldPosition(61.5,2.4,48)
Hiker
Posts: 9
Joined: Sun Nov 19, 2023 1:43 pm

Re: Ask a simple question, get a simple answer

Post by Hiker »

Slava wrote: Tue Jul 23, 2024 11:40 am Hmm... I don't know. Apparently the hook only works when an object is created on the map. When an object is placed in a container, it is removed from the map. This means that the hook is not called during creation since the object is not on the map.
The point is that we first create something, and then we put it somewhere. If we ourselves fill the container in the same way, the hook will be called.

Not called:

spawn("sack",15,16,0,0,"sack_2")
spawn("bread",15,16,0,0,"bread_1")
sack_2.containeritem:addItem(bread_1.item)

Called:

sack.containeritem:addItem(spawn("bread").item)

In principle, this may even be useful, but now I have to think (apparently use dynamic hooks) how to disable this call in the script when necessary.
Slava
Posts: 21
Joined: Sun Aug 30, 2015 2:10 pm

Re: Ask a simple question, get a simple answer

Post by Slava »

Hiker wrote: Wed Jul 24, 2024 8:50 am In principle, this may even be useful, but now I have to think (apparently use dynamic hooks) how to disable this call in the script when necessary.
For what? Define a new random container. using baseObject you can inherit from the original container. In the definition of the container component, add an onInit hook. Then you can use a regular container and a random container where necessary.

UPD newermind. I got the context wrong. Let me clarify, at the top is the code that generates the dungeon editor?
Below is what you wrote by hand?
In that case, yes, you can use addConector and removeConnector. But in my opinion, this is not entirely flexible.

Code: Select all

defineObject{
	name = "random_sack",
	baseObject = "sack",
	components = {
		{
			class = "ContainerItem",
			containerType = "sack",
			openSound = "container_sack_open",
			closeSound = "container_sack_close",
			onInit = function(self)
				
				--Your random script here
			
				if self:getItemCount() > 0 then
					self.go.model:setModel("assets/models/items/sack_full.fbx")
					self.go.item:setGfxIndex(81)
					self.go.item:updateBoundingBox()
				end
			end,
			onInsertItem = function(self, item)
				-- convert to full sack
				if self.go.item:getGfxIndex() == 82 then
					self.go.model:setModel("assets/models/items/sack_full.fbx")
					self.go.item:setGfxIndex(81)
					self.go.item:updateBoundingBox()
				end
			end,
			onRemoveItem = function(self, item)
				-- convert to empty sack when last item is removed
				if self:getItemCount() == 0 and self.go.item:getGfxIndex() == 81 then
					self.go.model:setModel("assets/models/items/sack_empty.fbx")
					self.go.item:setGfxIndex(82)
					self.go.item:updateBoundingBox()
				end
			end,
		},
	},
}
Hiker
Posts: 9
Joined: Sun Nov 19, 2023 1:43 pm

Re: Ask a simple question, get a simple answer

Post by Hiker »

The fact of the matter is that any container created using a script will be filled with items with random properties as specified in their onInit, but if you just put it in dungeon.lua as stated above, then it will not be random.

Okay, I can use this for my own purposes, but then how can I script an item that doesn’t have random properties? I think I can just disable onInit for this before spawning.
Post Reply