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
THOM
Posts: 1274
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: Ask a simple question, get a simple answer

Post by THOM »

Okay, I have the following problem.

I have an item and when unequipping it I check if another instance of this item is in any attack hand of any champion in the party.

My code is this

Code: Select all

		onUnequipItem = function(self,champion,slot)
                	if slot==1 or slot==2  then
				local hascandle = false
				for i = 1,4 do
					local ch = party.party:getChampion(i)
					if ch:getItem(1) == candle or ch:getItem(2) == candle then
						hascandle = true
						break
					end
				end
				if hascandle == false then
					party.candle:disable()
				end
                	end
		end,

But it seems that the onUnequipItem function is executed in the same moment the item is still equipped. Means: When I search for a champion having the item in a hand the one who unequips it still has it and the result will alway be hasItem == true. But I cannot leave the champion unequipping the item out because he could have that item in the other hand.

Has anyone an idea how to solve this?
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
User avatar
Zo Kath Ra
Posts: 937
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: Ask a simple question, get a simple answer

Post by Zo Kath Ra »

THOM wrote: Fri Nov 27, 2020 11:49 am But it seems that the onUnequipItem function is executed in the same moment the item is still equipped. Means: When I search for a champion having the item in a hand the one who unequips it still has it and the result will alway be hasItem == true. But I cannot leave the champion unequipping the item out because he could have that item in the other hand.

Has anyone an idea how to solve this?
You could use a counter.

onEquipItem:
- if item equipped in slot 1 or 2, increment counter

onUnequipItem:
- if item unequipped from slot 1 or 2, decrement counter
- if counter == zero then disable light

edit:
Instead of the numbers 1 and 2, I would use

ItemSlot.Weapon
ItemSlot.OffHand

It's not necessary, but it makes the code a little clearer.
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 »

Zo Kath Ra wrote: Fri Nov 27, 2020 8:37 am
bongobeat wrote: Thu Nov 26, 2020 11:49 pm Also these objects have been tested, many times since i published many version, and I or the testers didn't notice something like that.
How many objects does your mod have in total?
Maybe there are so many that the game decides to just drop some of them...
around 80k objects, up to 100k until all is spawned. Minus 7-8k with the items, if they are used or sold, etc..
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
User avatar
THOM
Posts: 1274
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: Ask a simple question, get a simple answer

Post by THOM »

Zo Kath Ra wrote: Fri Nov 27, 2020 12:19 pm You could use a counter.

onEquipItem:
- if item equipped in slot 1 or 2, increment counter

onUnequipItem:
- if item unequipped from slot 1 or 2, decrement counter
- if counter == zero then disable light
Thanx. Seems to work perfect.
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
User avatar
7Soul
Posts: 209
Joined: Sun Oct 19, 2014 1:56 am
Location: Brazil

Re: Ask a simple question, get a simple answer

Post by 7Soul »

Does anyone know a good way to knockback monsters? I'm doing it like this:

Code: Select all

if monster and not monster:isImmuneTo("knockback") then
	monster.go.animation:stop()
	if monster.go.brain then
		monster.go.brain:performAction("idle")
		monster.go.brain:wait()
	end
	delayedCall("functions", 0.2, "knockback", monster.go.id, party.facing)
end

function knockback(id, facing)
	local monster = id and findEntity(id).monster or nil
	monster:knockback(facing)
end
But it's kinda awkward. Without the delay it fails half of the time, and sometimes I get "unknown monster action: "idle""
Join the LoG discord server: https://discord.gg/ArgAgNN :D

My Mods
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

The best way to forcibly interrupt a monster's action is to setPosition() it; see this similar question: http://www.grimrock.net/forum/viewtopic ... 92#p117892
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
THOM
Posts: 1274
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: Ask a simple question, get a simple answer

Post by THOM »

Okay, I must ask for some more help.

My candle item is finished so far. It works partly like a torch, but gives also light when the item ist put down.

At the moment it is an everburning candle, but I want to limit this (like it is with the torch). My first attempt was to give the gameobject a timer that destroys the item after ten minutes or so. But it seems that timers won't run when put into items. At least my construction didn't wanted to work.

Then I thought to create a timer on the map onInit of the candle. Not easy to give it a unique name, which is needed to progam it and connect it correct.

Or does anyone have an easier idea?
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
User avatar
Zo Kath Ra
Posts: 937
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: Ask a simple question, get a simple answer

Post by Zo Kath Ra »

THOM wrote: Wed Dec 02, 2020 12:46 am Okay, I must ask for some more help.

My candle item is finished so far. It works partly like a torch, but gives also light when the item ist put down.
You want the candle to burn when
- it's in a champion's weapon slot
and also
- when it's in the game world, i.e. when it can be found with findEntity(id)
?
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Timers elapse when the map they're on updates. When an object is being carried (or is in a container), it's not on any maps, so a timer will never elapse time. Also, if the object is not on the same map as the party, the timer will elapse a lot slower, which is probably not what you want in this case.

If I'm understanding the question correctly, the easiest way to get the behaviour you want would be to
1. have a function that finds a candle with a specific id and destroys it. or3_manager.lua already has the code for finding an item with a specific id, so you can just reuse that.
2. make a delayedCall() to that function when the candle is lit. Remember delayedCall() passes arguments after the first three through to whatever function it ends up calling, so you can do something like delayedCall("candle_script",600,"destroyCandle",self.go.id)
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
THOM
Posts: 1274
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: Ask a simple question, get a simple answer

Post by THOM »

Zo Kath Ra wrote: Wed Dec 02, 2020 12:55 am You want the candle to burn when
- it's in a champion's weapon slot
and also
- when it's in the game world, i.e. when it can be found with findEntity(id)
?
The candle burns in the champions attack hands and when put on the floor or elsewhere. You can find it with findEntity() then, yes.

Your thoughts are probably going into a similar direction like minmay's does. That solution is good and I will try it out. I just hoped I could solve this just from whithin the item itself. But it seems I have to need an external script entity. So it be.
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
Post Reply