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!
User avatar
Isaac
Posts: 3188
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

Curunir wrote:...is there a way to place items in an alcove and NOT have them appear inside each other?
Yes. Items have a subtileOffset that can be changed; offsetting their default position.

eg.
GameObject:setSubtileOffset(x, y)
User avatar
Curunir
Posts: 332
Joined: Fri Mar 30, 2012 11:19 pm

Re: Ask a simple question, get a simple answer

Post by Curunir »

minmay wrote:Your definition of the openSesame function is inside the coldDoor function - and after the attempt to call it, so of course it's nil when you attempt to call it.
I was fully convinced I put openSesame outside of the upper function! Thanks for pointing it out!
Isaac wrote:Yes. Items have a subtileOffset that can be changed; offsetting their default position.

eg.
GameObject:setSubtileOffset(x, y)
So, I place whatever I want on an alcove from inside the editor, then use the alcove item id-s to offset them that way in any script entity, outside of any function?
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 »

Hi there, I got trouble with this code:
a book, when used, train the champion by 1 points in a skill. But if you already have available skillpoints, when you use this tome (it trains you by one skill and) it remove 1 skill points.
What am I missing?
SpoilerShow

Code: Select all

defineObject{
	name = "tomeskill_learner",
	baseObject = "base_item",
	components = {
		{
			class = "Model",
			model = "assets/models/items/tome.fbx",
			material = "book4",
		},
		{
			class = "Item",
			uiName = "The Incredible Mystery Of Life",
			description = "A thick tome made by an old crook who teach us to better know ourself and increase our learning experience.",
			gfxAtlas = "mod_assets/textures/oliveitem.tga",
			gfxIndex = 13,
			weight = 1.0,
			gameEffect = "Gain Learner Skill +1",
			traits = { "tome" },
		},
		{
			class = "UsableItem",
			sound = "level_up",
			onUseItem = function(self, champion)
				hudPrint(champion:getName().." gained Learner +1.")
				champion:trainSkill("perfection", 1)
				return true
			end,
		},
	},
}
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
minmay
Posts: 2788
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

champion:trainSkill("perfection",1,false)
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
Isaac
Posts: 3188
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

Curunir wrote:So, I place whatever I want on an alcove from inside the editor, then use the alcove item id-s to offset them that way in any script entity, outside of any function?
Yes.

Example:

Code: Select all

for _,item in dungeon_alcove_1.surface:contents() do
	item.go:setSubtileOffset(1.5,-.2)
	break
end
EDIT:
You can try my experimental script for this. It seems to work well enough, though I'd love (suggestions) to improve upon it.
Just place it on the map, it finds altars, alcoves, and chests, and it jostles their contents.

Code: Select all

function jostle(container, seed) --Intended to shift the contents of chests. 						
			if container and type(container) ~= 'table' then container = findEntity(container)
			end
			
			if seed and type(seed) == "number" then
			  math.randomseed(seed)
			end

			if not container.name:match('alcove') then
				for _,items in container.surface:contents() do
					items.go:setSubtileOffset((math.random()-.5)/2,(math.random()-.5)/2)
				end
			else   local offset = 1.3
					local dX, dY = getForward(container.facing)
					 if ((container.facing+1)%2) ~= 1 then
						 offset = offset * -1
					 end
					  if dX ~= 0 then dX = dX * offset end
					  if dY ~= 0 then dY = dY * offset end
					for _,items in container.surface:contents() do
						items.go:setSubtileOffset(dX+(math.random()-.5)/2,dY+(math.random()-.5)/2)
					end		
			end
end	

function string2bytes(text)
	local result = ""
	for x = 1, #text do
		result = result..string.byte(text, x)
	end
	return tonumber(result)
end

for x = 1, Dungeon.getMaxLevels() do
	for entity in self.go.map:allEntities(x) do
		if entity.surface and entity.surface:count() > 0 
		then
			jostle(entity.id)
		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 »

minmay wrote:champion:trainSkill("perfection",1,false)

Thanks ;)
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
User avatar
Curunir
Posts: 332
Joined: Fri Mar 30, 2012 11:19 pm

Re: Ask a simple question, get a simple answer

Post by Curunir »

One thing that keeps bugging me - why are there so many differently named instances of dungeon floor and ceiling entities in the editor? I plopped down a few side by side and could not see any immediately discernible difference.

p.s. I somehow did not see your reply to my previous question! Thanks, Isaac, I will give the script a go when I get back home! Which brings me back to an issue I've had with LUA ever since I started reading tutorials and scripts here - the use of the underscore.

"It is idiomatic in Lua to use the underscore character for throw away variables so you can simply assign it to another variable name."
"The Lua coding standard designates the underscore (_) as the standard global variable to use in place of an unused variable."

I just can't wrap my head around what that means. Is this sort of like a wildcard, anything goes replacer?
minmay
Posts: 2788
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

If you call a function that returns multiple values, but you don't use all of the return values, you may end up with an unused variable. Typically people name this variable _. The most common construction you'll see is

Code: Select all

for _,v in pairs(someTable) do
because sometimes you don't care about the keys in a table, just the values.

The underscore doesn't have an inherent meaning in Lua, it's just a normal variable name.
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
Curunir
Posts: 332
Joined: Fri Mar 30, 2012 11:19 pm

Re: Ask a simple question, get a simple answer

Post by Curunir »

Ah, that makes more sense, worded that way! Thank you!
User avatar
akroma222
Posts: 1029
Joined: Thu Oct 04, 2012 10:08 am

Re: Ask a simple question, get a simple answer

Post by akroma222 »

Hey All , I have a question re Container Items...
Specifically, what would be the best/cleanest way to detect whether a champion currently has an Open (or Closed) Container Item in their Inventory ???

Reason - I'm having to re implement some of the champ menu GUI, including Containers
(I've drawn over the top of some of the games hard coded things, and now have to re implement what Ive drawn over :lol: :roll: )

Ive not dealt with Containers extensively, but I suspect maybe...
.... a "UsableItemComponent" could be added and onUseItem hook could simply check/change/toggle a global variable ??
Ive made a few disastrous attempts at defining Containers using this^^ and similar methods to detect whether they are in an Inventory, Open/Closed etc

However, Im experiencing an acute case of deferred success :cry:
Any suggestions welcome :D
Post Reply