function in a cloned object definition: question

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!
Post Reply
Granamir
Posts: 202
Joined: Wed Jan 07, 2015 7:19 pm

function in a cloned object definition: question

Post by Granamir »

I'm going to make a wizard object that give party underwater breathing ability while in hand and until wizard have energy left.
To make wizard consume energy while holding that object i set, in 'onEquipItem', to spawn a timer that call a function. But as i thought i can't just put the function in the "onEquipItem" component.
Is there a way to place the function directly inside object code, without the need to place a script entity in game with that function and call this function form the object.

If can help my script so far is:

Code: Select all

defineObject{
	name = "vigorsol_bowl",
	baseObject = "base_item",
	components = {
		{
			class = "Model",
			model = "assets/models/items/essence_air.fbx",
		},
		{
			class = "Item",
			uiName = "Vigorsol bowl",
			description = "A fresh air explosion. Uses energy over time.",
			gfxIndex = 347,
			impactSound = "impact_blunt",
			weight = 1,
			traits = { "two_handed" },
			
			onEquipItem=function(self,champion,slot)
				if slot==1 or slot==2 or slot==11 or slot==12 then
					local energiaRimasta=champion:getEnergy()
					
					if energiaRimasta > 0 then
						
						for i = 1, 4, 1 do
							party.party:getChampion(i):setConditionValue("water_breathing", 360)
						end
						
					end
					
					local xParty, yParty, dParty, lParty = party:getPosition()
					spawn("timer",lParty,1,1,1,0,"timerEnergiaVigorsol")
					--local timerVigorsol = findEntity("timerDanno_" .. nFuocoFatuo)
					timerEnergiaVigorsol.timer:setTimerInterval(1)
					timerEnergiaVigorsol.timer:setDisableSelf(false)
					timerEnergiaVigorsol.timer:setTriggerOnStart(true)
					timerEnergiaVigorsol.timer:setCurrentLevelOnly(false)
					timerEnergiaVigorsol.timer:addConnector("onActivate", self.go.id, "scendeEnergiaVigorsol")
					timerEnergiaVigorsol.timer:start()
					
--[[
					function scendeEnergiaVigorsol()
						energiaRimasta=champion:getEnergy()
						if energiaRimasta > 0 then
							champion:setEnergy(energiaRimasta-100)
						elseif energiaRimasta <=0 then
							timerEnergiaVigorsol:destroy()
							for i = 1, 4, 1 do
								party.party:getChampion(i):removeCondition("water_breathing")
							end
						end
					end
--]]
					
				end
			end,
			onUnequipItem=function(self,champion,slot)
				if slot==1 or slot==2 or slot==11 or slot==12 then
					for i = 1, 4, 1 do
						party.party:getChampion(i):removeCondition("water_breathing")
					end
				end
				timerEnergiaVigorsol:destroy()
			end
		 
		},
		{
			class = "Particle",
			particleSystem = "essence_air",
		},
		{
			class = "Light",
			offset = vec(0, 0.4, 0),
			range = 1.5,
			color = vec(0.5, 1, 1),
			brightness = 30,
			castShadow = false,
		},
		{
			class = "EquipmentItem",
			slot = "Weapon",
		},
	},
}
Commented function (function scendeEnergiaVigorsol()) is the function that cause editor crash.
Last edited by Granamir on Mon Feb 16, 2015 7:07 pm, edited 2 times in total.
Granamir
Posts: 202
Joined: Wed Jan 07, 2015 7:19 pm

Re: function in a cloned object definition: question

Post by Granamir »

Anyone can help me?
how do i make an item calling a cicle based on time?
Is possible to make it without using external script?
If not, how do i call (from object code) a function in a lua file?
Thanks for help
MrChoke
Posts: 324
Joined: Sat Oct 25, 2014 7:20 pm

Re: function in a cloned object definition: question

Post by MrChoke »

Granamir wrote:Anyone can help me?
how do i make an item calling a cicle based on time?
Is possible to make it without using external script?
If not, how do i call (from object code) a function in a lua file?
Thanks for help
You have to use a script_entity. It doesn't have to be an external LUA file though. You can update the LUA code of the script right in the editor if you want (choose embedded).

The way you call the function from your timer is mostly correct:
timerEnergiaVigorsol.timer:addConnector("onActivate", self.go.id, "scendeEnergiaVigorsol")

However, replace "self.go.id" with the ID of the script_entity holding the function.
Granamir
Posts: 202
Joined: Wed Jan 07, 2015 7:19 pm

Re: function in a cloned object definition: question

Post by Granamir »

Thank you.
I would prefer a better solution but, even if a bit tricky, this works too.
Post Reply