Page 1 of 1

function in a cloned object definition: question

Posted: Sat Feb 07, 2015 6:47 pm
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.

Re: function in a cloned object definition: question

Posted: Mon Feb 16, 2015 7:05 pm
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

Re: function in a cloned object definition: question

Posted: Mon Feb 16, 2015 9:35 pm
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.

Re: function in a cloned object definition: question

Posted: Tue Feb 17, 2015 6:07 pm
by Granamir
Thank you.
I would prefer a better solution but, even if a bit tricky, this works too.