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!
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: Ask a simple question, get a simple answer

Post by Grimfan »

Okay, I've created this little script to open a couple of doors:

Code: Select all

step = 1

function buttonPressPort(button)
	if button.go.id == "button"..step then 
	step = step + 1	
	else
	step = 1
	end
	
	if step == 5 then
	dungeon_door_portcullis_13.door:open()
	dungeon_door_portcullis_14.door:open()
	end
	
	if button.go.id == "button1" then
	step = 2
	end
end
Now this works, but what if I want to have them press the same button twice in a row or go back to a previous button and press it again? Could I just add to this code or would I have to try another method (this is where my lua understanding begins to peter out).

Thanks for any help in advance. :)

Okay, another quick question. Is it safe to use player.level when spawning objects (and particles, items, etc) for local functions. I find having to go back and change the spawn level whenever I add a new level to my dungeon tedious.
User avatar
Isaac
Posts: 3179
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

Grimfan wrote:Is it safe to use player.level when spawning objects (and particles, items, etc) for local functions. I find having to go back and change the spawn level whenever I add a new level to my dungeon tedious.
The object/variable player.level doesn't exist in the default game. If you mean party.level, you can safely use it, but it changes to whatever level the party is currently on. So it's only useful if what you want to spawn must always appear on the same level as the party. If you use it to spawn objects on different floors it won't work if the party isn't on that floor when the objects are spawned; the objects will appear on whatever lever the party is exploring (and could be inside or outside the level's walls).
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: Ask a simple question, get a simple answer

Post by Grimfan »

Isaac wrote:
Grimfan wrote:Is it safe to use player.level when spawning objects (and particles, items, etc) for local functions. I find having to go back and change the spawn level whenever I add a new level to my dungeon tedious.
The object/variable player.level doesn't exist in the default game. If you mean party.level, you can safely use it, but it changes to whatever level the party is currently on. So it's only useful if what you want to spawn must always appear on the same level as the party. If you use it to spawn objects on different floors it won't work if the party isn't on that floor when the objects are spawned; the objects will appear on whatever lever the party is exploring (and could be inside or outside the level's walls).
Yeah, I meant party.level... player.level was a typo. Also, most of the spawns I'm talking about are initiated by the player when they step on a floortrigger or pull on a lever, so that means I am good to go (unless someone can point out another big pitfall). ;)
User avatar
Isaac
Posts: 3179
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

Grimfan wrote:Also, most of the spawns I'm talking about are initiated by the player when they step on a floortrigger or pull on a lever, so that means I am good to go (unless someone can point out another big pitfall). ;)
You can also use the trigger's level attribute. eg. floor_trigger_1.level
Another option is to use the script object's level attribute.

Also, you can use an object's built in spawn function to place a new item in the same place as the object. eg. treasure_script:spawn("chest")
This has the benefit of being able to move the object around (even between levels), and the instructions will always spawn at the object's current location.
Grimfan wrote:What if I want to have them press the same button twice in a row or go back to a previous button and press it again?
This script supports buttons that require a set number of presses each, in order to trigger. The names of the buttons are set in B_table. Currently it expects three wall_buttons.

Code: Select all

--button lock with combination
B_table = { --=====================[Add all of your button id's here. Put the number of presses in the braces {#} ]
			wall_button_1 = {3},
			wall_button_2 = {1},
			wall_button_3 = {2},
			
		  } --======================[]
for _,each in pairs(B_table) do each[2]=0 end 	--sets the recorded number of presses to zero.
previous_button = ""						--stores the last button pressed

function buttonCapture(button) 			
	if button.go.id ~= previous_button then		--runs code only if it's a different button.
		previous_button = button.go.id			--sets the current button as the previous one. 
		B_table[button.go.id][2] = 1			--resets button values
		B_table[button.go.id][3] = false
		if B_table[button.go.id][2]>B_table[button.go.id][1] then		 --catches too many presses; clears all previously correct button values. 
			for _,each in pairs(B_table) do
			 each[2]=0 each[3]=false
			end
		end	 
	end
	
	if B_table[button.go.id][2]>B_table[button.go.id][1] then			--catches too many presses; clears all previously correct button values.			
		for _,each in pairs(B_table) do each[2]=0 each[3]=false end 
	end
	 
	if B_table[button.go.id][1] == B_table[button.go.id][2] then		--detects correct number of presses.
		B_table[button.go.id][3] = true							--sets the button entry to true
	 else B_table[button.go.id][3] = false							--or false if incorrect
	end
	B_table[button.go.id][2] = B_table[button.go.id][2]+1			--increments the recorded number of button presses
	
	local unlock = true
	for _,b in pairs(B_table) do
		unlock = unlock and b[3]								--compares all buttons for true values; results in either unlock == true or false
	end
	
	if unlock then
		
		--=====================================[success, put unlock logic here]
		
		dungeon_door_iron_1.door:open()
		
		--====================================================[]
	end
end
*Note: This script only allows consecutive presses; it doesn't support alternating button presses.
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: Ask a simple question, get a simple answer

Post by Grimfan »

Thanks for the help Isaac, you're a marvel! :D
PedroMoniz
Posts: 16
Joined: Wed Jan 11, 2017 9:46 pm

Re: Ask a simple question, get a simple answer

Post by PedroMoniz »

Hello,

I am creating a UI component and I already have the example working.
I found the graphicsContext reference and I am having some problems.
When I print my context.width for example, it gives the value of 0 when the game window is stretched until the midle of the editor and the height gives 0 before the bottom touches with the top.
Since the drawRect at 0,0 gives the corner, just what are the values given by context.width and context.height?

Edit: Aparently the values just work really bad in the editor. In the game everything is good.

Edit2: A question,

Is it possible to change the text size in ui?
There is a drawText but only uses position. There is a font(font) but what exactly is a font for the game?
I have my ui scaling properly and looking nice, but not being able to change the size of the text is annoying.
Last edited by PedroMoniz on Wed Feb 01, 2017 8:46 pm, edited 3 times in total.
User avatar
THOM
Posts: 1267
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: Ask a simple question, get a simple answer

Post by THOM »

Is there a way to alter during the game a monsters brain behaviour?

I tried this but all I got was "attemped to call method 'goTo' (a nil value)" :

Code: Select all

ghost_1.brain:onThink(self:goTo("dungeon_floor_1"))
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
MrChoke
Posts: 324
Joined: Sat Oct 25, 2014 7:20 pm

Re: Ask a simple question, get a simple answer

Post by MrChoke »

THOM wrote:Is there a way to alter during the game a monsters brain behaviour?

I tried this but all I got was "attemped to call method 'goTo' (a nil value)" :

Code: Select all

ghost_1.brain:onThink(self:goTo("dungeon_floor_1"))
Yes, it is very moddable. I am doing extensively in my mod. First though, it's a hook. It seems you are calling it almost like a regular function call. You add the hook either directly in the "defineObject" asset if you are making a custom monster or you use "addConnector" to add the hook to a built-in monster. It's giving you that error because "self" in that context is not the Brain component. It's probably the script component.

Regarding the goTo function, it is a valid function that you can call on the brain. However, the string you use must be an item, not the name of a floor. Something lke "prison_key_1" or whatever.
User avatar
Isaac
Posts: 3179
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

PedroMoniz wrote:There is a font(font) but what exactly is a font for the game?
GraphicsContext.font(font)
Known valid values for font are: "tiny", "small", "medium", "large"

https://github.com/JKos/log2doc/wiki/Ob ... icscontext
User avatar
THOM
Posts: 1267
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: Ask a simple question, get a simple answer

Post by THOM »

MrChoke wrote:
THOM wrote:Is there a way to alter during the game a monsters brain behaviour?

I tried this but all I got was "attemped to call method 'goTo' (a nil value)" :

Code: Select all

ghost_1.brain:onThink(self:goTo("dungeon_floor_1"))
Yes, it is very moddable. I am doing extensively in my mod. First though, it's a hook. It seems you are calling it almost like a regular function call. You add the hook either directly in the "defineObject" asset if you are making a custom monster or you use "addConnector" to add the hook to a built-in monster. It's giving you that error because "self" in that context is not the Brain component. It's probably the script component.

Regarding the goTo function, it is a valid function that you can call on the brain. However, the string you use must be an item, not the name of a floor. Something lke "prison_key_1" or whatever.
I know I can do this inside the monster-definition. But what I want to do is to have a monster that behaves like a normal monster and at a certain moment it starts to go to a certain point of the map. So I think I have to add this behaviour during the game to the brain. Or something like that.

BTW the goTo command also understands names of objects, not only items. It works in my dungeon already like this.
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
Post Reply