Jump to content

Attemp to index global 'UNIT' (a nil value)


DarK_FeneR996

Recommended Posts

local UnitsCheck = {
	"Krashnodar Armor-1-1", "Krashnodar Armor-1-2", "Krashnodar Armor-1-3", "Krashnodar Armor-1-4", "Krashnodar Armor-1-5", "Krashnodar Armor-1-6", "Krashnodar Armor-1-7", "Krashnodar Armor-1-8", "Krashnodar Armor-1-9", 
	"Krashnodar Armor-1-10", "Krashnodar Armor-1-11", "Krashnodar Armor-1-12", "Krashnodar Armor-1-13", "Krashnodar Armor-1-14", "Krashnodar Armor-1-15", 
	
	"Krashnodar AAA-1-1", "Krashnodar AAA-1-2", "Krashnodar AAA-1-3", "Krashnodar AAA-1-4", "Krashnodar AAA-1-5", "Krashnodar AAA-1-6", "Krashnodar AAA-1-7", "Krashnodar AAA-1-8", "Krashnodar AAA-1-9", 

	"Krashnodar SAMrdr-1-1", "Krashnodar SAMrdr-1-2", "Krashnodar SAMrdr-1-3", "Krashnodar SAMrdr-1-4", "Krashnodar SAMrdr-1-5", "Krashnodar SAMrdr-1-6", "Krashnodar SAMrdr-1-7", "Krashnodar SAMrdr-1-8", 

	"Krashnodar Sam-3-1", "Krashnodar Sam-3-2", "Krashnodar Sam-3-3", "Krashnodar Sam-3-4", "Krashnodar Sam-3-5",
}

for unitid = 0, 37 do
	local UnitName = UNIT:FindByName(UnitsCheck[unitid]) --error here, line 13
	UnitName:HandleEvent(EVENTS.Dead)

	function UnitName:OnEventDead(EventData)
		repeat
			local ACchose = mist.random(0, 2)
			if ACchose == 0 then
				if mist.random(0, 100) <= 70
				then
					mist.cloneGroup('Krashnodar Plane-F14-1', true)
				else
					ACchose = -1
				end
			end
			if ACchose == 1 then
				mist.cloneGroup('Krashnodar Plane-F16-1', true)
			end
			if ACchose == 2 then
				mist.cloneGroup('Krashnodar Plane-F18-1', true)
			end
		until ACchose ~= -1
	end
	--send alert message
	local msg = {} 
    msg.text = 'Red AI Incoming' 
    msg.displayTime = 10  
    msg.msgFor = {coa = {'all'}} 
    mist.message.add(msg)
end

image.pngHello, I'm new in lua scripting (some knowledge in c#-c++), and I cannot understand why I get this message.

The units in the script are already spawned before this script runs... can anybody help me to solve this? 😕

THX 🙂

Link to comment
Share on other sites

The error means that UNIT is not defined. If you are using MOOSE or similar frameworks, make sure they load before your script runs.

Asked differently: where did you define UNIT:findByName()? Or did you intend to invoke Unit.getByName() instead?

Also, be advised that in Lua arrays start at 1, not zero, so your loop may run into more issues. A better loop would be for unitid =1, #UnitsCheck do

 


Edited by cfrag
Link to comment
Share on other sites

4 hours ago, cfrag said:

The error means that UNIT is not defined. If you are using MOOSE or similar frameworks, make sure they load before your script runs.

Asked differently: where did you define UNIT:findByName()? Or did you intend to invoke Unit.getByName() instead?

Also, be advised that in Lua arrays start at 1, not zero, so your loop may run into more issues. A better loop would be for unitid =1, #UnitsCheck do

 

 

1. I'm not using MOOSE

2. I though it was a dcs function (I've found it in some examples on a website with UNIT:findByName(). I confirm that was MOOSE website) so yea, I'm switching to Unit.getByName().
Right now I'm getting "Attempt to index global 'EVENTS' (a nil value)" --Line 14
I can see the 'EVENTS' is from MOOSE aswel, I will try to avoid it

3. Aw okay, nice to know, THX 😄

Really thank you for everything 🙂


Edited by DarK_FeneR996
Link to comment
Share on other sites

Now I'm getting the error "attempt to call onevent (a nil value)". Any suggestion?
 

local UnitsCheck = {
	'Krashnodar Armor-1-1', 'Krashnodar Armor-1-2', 'Krashnodar Armor-1-3', 'Krashnodar Armor-1-4', 'Krashnodar Armor-1-5', 'Krashnodar Armor-1-6', 'Krashnodar Armor-1-7', 'Krashnodar Armor-1-8', 'Krashnodar Armor-1-9', 
	'Krashnodar Armor-1-10', 'Krashnodar Armor-1-11', 'Krashnodar Armor-1-12', 'Krashnodar Armor-1-13', 'Krashnodar Armor-1-14', 'Krashnodar Armor-1-15', 
	
	'Krashnodar AAA-1-1', 'Krashnodar AAA-1-2', 'Krashnodar AAA-1-3', 'Krashnodar AAA-1-4', 'Krashnodar AAA-1-5', 'Krashnodar AAA-1-6', 'Krashnodar AAA-1-7', 'Krashnodar AAA-1-8', 'Krashnodar AAA-1-9', 

	'Krashnodar SAMrdr-1-1', 'Krashnodar SAMrdr-1-2', 'Krashnodar SAMrdr-1-3', 'Krashnodar SAMrdr-1-4', 'Krashnodar SAMrdr-1-5', 'Krashnodar SAMrdr-1-6', 'Krashnodar SAMrdr-1-7', 'Krashnodar SAMrdr-1-8', 

	'Krashnodar Sam-3-1', 'Krashnodar Sam-3-2', 'Krashnodar Sam-3-3', 'Krashnodar Sam-3-4', 'Krashnodar Sam-3-5',
}

local EventHandl = {}

world.addEventHandler(EventHandl)

function EventHandl:OnEventHit(EventData)
	if EventData.id == world.event.S_EVENT_HIT and isAnyKrashGndUnit(EventData.target:getName()) then
		repeat
			local ACchose = mist.random(0, 2)
			if ACchose == 0 then
				if mist.random(0, 100) <= 70
				then
					mist.cloneGroup('Krashnodar Plane-F14-1', true)
				else
					ACchose = -1
				end
			end
			if ACchose == 1 then
				mist.cloneGroup('Krashnodar Plane-F16-1', true)
			end
			if ACchose == 2 then
				mist.cloneGroup('Krashnodar Plane-F18-1', true)
			end
		until ACchose ~= -1

		--send alert message
		local msg = {} 
		msg.text = 'Red AI Incoming' 
		msg.displayTime = 10  
		msg.msgFor = {coa = {'all'}} 
		mist.message.add(msg)
	end
end

local function isAnyKrashGndUnit(uname)
	for unitid=1, 37 do
		if uname == UnitsCheck[unitid] then return true end
	end
	return false
end

 

Link to comment
Share on other sites

4 hours ago, DarK_FeneR996 said:

I'm getting the error "attempt to call onevent (a nil value)". Any suggestion?

The most likely reason for this is that the table you registered with world.addEventHandler() does not have a method called "onEvent" (spelled exactly like that, the one I see is called "OnEventHit"). As a result, while processing an event, world tries to access EventHandl.onEvent(), gets nil, and complains.

"But wait, cfrag," you may object, "my metachlorians count is low; I wasn't born with this knowledge. WTF???"

To which I would respond - "Exactly."

This may help in the meantime 🙂 (and no, it doesn't explain that onEvent bit either. But it's a great resource nonetheless)

 

[To elaborate somewhat: DCS seems quite oldschool when it comes to documentation. And I'm talking Old Testament "if it was hard to write, it should be hard to use" oldschool here. There are great efforts underway by great people to make learning to mission-script less arduous, and we still have a long way to go. But it's well worth the hardship; so strap in, hold on, and carry a stiff upper lip (whatever that means)]


Edited by cfrag
Link to comment
Share on other sites

1 hour ago, cfrag said:

The most likely reason for this is that the table you registered with world.addEventHandler() does not have a method called "onEvent" (spelled exactly like that, the one I see is called "OnEventHit"). As a result, while processing an event, world tries to access EventHandl.onEvent(), gets nil, and complains.

"But wait, cfrag," you may object, "my metachlorians count is low; I wasn't born with this knowledge. WTF???"

To which I would respond - "Exactly."

This may help in the meantime 🙂 (and no, it doesn't explain that onEvent bit either. But it's a great resource nonetheless)

 

[To elaborate somewhat: DCS seems quite oldschool when it comes to documentation. And I'm talking Old Testament "if it was hard to write, it should be hard to use" oldschool here. There are great efforts underway by great people to make learning to mission-script less arduous, and we still have a long way to go. But it's well worth the hardship; so strap in, hold on, and carry a stiff upper lip (whatever that means)]

 

Aw yea initially it was 

EventHandl:OnEvent(EventData)

then I tried with 

EventHandl:OnEventHit(EventData)

Okay, onEvent seems does't exists. By searching I was sure it is a dcs function. So, how do I run a script and get initiator and target from a event?

this

world.addEventHandler(EventHandl)

should keep those data, right? how can I call onEvent? I'm getting mad🤧

 

Or maybe i'm misunderstanding you😅


Edited by DarK_FeneR996
Link to comment
Share on other sites

30 minutes ago, DarK_FeneR996 said:

how can I call onEvent? I'm getting mad

Here's the source to 'parashoo', a small script that removes a parachutist 3 minutes after they reach the ground. The script triggers on the 31 (landing after eject) event, the event handler is called onEvent(), and it is installed with world.addEventHandler. 

Maybe you can use that as a reference for your script

parashoo = {}
parashoo.killDelay = 3 * 60 -- 3 minutes delay

function parashoo.removeGuy(args)
	local theGuy = args.theGuy
	if theGuy and theGuy:isExist() then  
		Unit.destroy(theGuy)
	end
end 

-- remove parachuted pilots after landing
function parashoo:onEvent(event)
	if event.id == 31 then -- landing_after_eject
		if event.initiator then 			
			local args = {}
			args.theGuy = event.initiator 			
			timer.scheduleFunction(parashoo.removeGuy, args, timer.getTime() + parashoo.killDelay)
		end
	end
end

-- add event handler
world.addEventHandler(parashoo)

 


Edited by cfrag
Link to comment
Share on other sites

Try to put "world.addEventHandler(EventHandl)" to the bottom of your script, so that the function "onEvent(EventData)" is defined for EventHandl before you add it with that command.


Edited by Nick_17

1)  Asus ROG Strix Z690-A Gaming Wifi D4 / i9 13900K @ 5,5 Ghz / Corsair Vengeance RGB Pro 64 GB DDR4 RAM @ 3200 Mhz / Asus ROG Strix RTX 4090 / 2 x 4 TB SSD Samsung 870 Evo / 2 x 1 TB M.2 SSD Samsung 980 Pro / Meta Quest Pro

2)  Asus ROG Strix Z490-E Gaming / i9 10900K @ 4,9 Ghz / G.Skill Trident Z RGB 32 GB DDR4 RAM @ 3200 Mhz / Asus ROG Strix RTX 3090 / 1 TB SSD Samsung 870 Evo / 500 GB M.2 SSD Samsung 980 Pro / HP Reverb G2

Link to comment
Share on other sites

 

1 hour ago, cfrag said:

Have you tried simply renaming "EventHandl:OnEventHit(EventData)" to "EventHandl:onEvent(EventData)" (lowercase 'o' in 'onEvent') - that could be all that is missing.

 

YAY! After this I changed it and it works (I got some errors in function "function isAnyKrashGndUnit(uname)" but I solved it by myself)

Really thank you guys for your time ^-^ 

 

Here is the fine script, maybe it can be useful as exhample for others ...

UnitsCheck = {
	'Krashnodar Armor-1-1', 'Krashnodar Armor-1-2', 'Krashnodar Armor-1-3', 'Krashnodar Armor-1-4', 'Krashnodar Armor-1-5', 'Krashnodar Armor-1-6', 'Krashnodar Armor-1-7', 'Krashnodar Armor-1-8', 'Krashnodar Armor-1-9', 
	'Krashnodar Armor-1-10', 'Krashnodar Armor-1-11', 'Krashnodar Armor-1-12', 'Krashnodar Armor-1-13', 'Krashnodar Armor-1-14', 'Krashnodar Armor-1-15', 
	
	'Krashnodar AAA-1-1', 'Krashnodar AAA-1-2', 'Krashnodar AAA-1-3', 'Krashnodar AAA-1-4', 'Krashnodar AAA-1-5', 'Krashnodar AAA-1-6', 'Krashnodar AAA-1-7', 'Krashnodar AAA-1-8', 'Krashnodar AAA-1-9', 

	'Krashnodar SAMrdr-1-1', 'Krashnodar SAMrdr-1-2', 'Krashnodar SAMrdr-1-3', 'Krashnodar SAMrdr-1-4', 'Krashnodar SAMrdr-1-5', 'Krashnodar SAMrdr-1-6', 'Krashnodar SAMrdr-1-7', 'Krashnodar SAMrdr-1-8', 

	'Krashnodar Sam-3-1', 'Krashnodar Sam-3-2', 'Krashnodar Sam-3-3', 'Krashnodar Sam-3-4', 'Krashnodar Sam-3-5',
}

local EventHandl = {}

world.addEventHandler(EventHandl)

function EventHandl:onEvent(EventData)
	if EventData.id == world.event.S_EVENT_HIT then trigger.action.outText('General Unit Hit', 2) end
	if EventData.id == world.event.S_EVENT_HIT and isAnyKrashGndUnit(EventData.target:getName()) then
		repeat
			local ACchose = mist.random(0, 2)
			if ACchose == 0 then
				if mist.random(0, 100) <= 70
				then
					mist.cloneGroup('Krashnodar Plane-F14-1', true)
				else
					ACchose = -1
				end
			end
			if ACchose == 1 then
				mist.cloneGroup('Krashnodar Plane-F16-1', true)
			end
			if ACchose == 2 then
				mist.cloneGroup('Krashnodar Plane-F18-1', true)
			end
		until ACchose ~= -1

		--send alert message
		trigger.action.outText('Red AI Incoming', 2)
	end
end

function isAnyKrashGndUnit(uname)
	for unitid=1, 37 do
		if uname == UnitsCheck[unitid] then return true end
	end
	return false
end

 


Edited by DarK_FeneR996
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...