Jump to content
The simFlight Network Forums

LUA error: attempt to call global 'function_name' (a nil value)


Recommended Posts

Hi there,

I'm trying to call the function 'Armrest_Toggle' with a button assigned through FSUIPC as follows:

if ipcPARAM == 0 then

	Armrest_Toggle()

end

local function Armrest_Toggle()
    ipc.writeLvar(nToggleVal(ipc.readLvar("L:Twot_Armrest"), 0, 1))
end

local function nToggleVal(nCurrVal, nVal1, nVal2)
	if nCurrVal == nVal1 then
		return nVal2
	else
		return nVal1
	end
end

I'm tracing the changes in LVars and when I press the assigned button, I get this error:

273111 *** LUA Error: ...Games\Microsoft Flight Simulator X\Modules\dhc-6.lua:26: attempt to call global 'Armrest_Toggle' (a nil value)

And here is the content of FSUIPC.ini, as relates to the particular aircraft I've loaded into the sim:

[JoyNames]
AutoAssignLetters=No
0=X52 Pro Flight Controller
0.GUID={A1946F60-C51A-11E6-8005-444553540000}

[Profile.DHC-6]
1=Aerosoft DHC-6

[Buttons.DHC-6]
0=P0,1,CL1:R,0 ; armrest toggle 	-{Lua dhc-6}-

[General]
ShortAircraftNameOk=Substring

[LuaFiles]
1=dhc-6
2=ipcReady
3=linda

 

I wonder what can cause this error? Thanks in advance!!!

FSUIPC4.log

Link to comment
Share on other sites

Hi,

in C/C++ you need a forward declaration to have the definition below the actual call. So you write bottom up if you don't do this, means you might need to have the local function Armrest_Toggle()... above the calling part.

The error says you are looking for a global function, that means your actual function is not visible when you call it.

Thomas

  • Upvote 1
Link to comment
Share on other sites

Thank you, Thomas!!! Strange, I thought calling functions was just a matter of proper environment definition (local or global), but direction of declaration is smth new for me...

I'll try your suggestion and return to this thread.

And Happy New Year!!!

Link to comment
Share on other sites

I tried this to no avail yet...

function nToggleVal(nCurrVal, nVal1, nVal2)
	if nCurrVal == nVal1 then
		return nVal2
	else
		return nVal1
	end
end

function Armrest_Toggle()
    ipc.writeLvar(nToggleVal(ipc.readLvar("L:Twot_Armrest"), 0, 1))
end

if ipcPARAM == 0 then

	Armrest_Toggle()
	
end

FSUIPC.ini is unchanged - FSUIPC recognizes the button OK. The problem is associated with function call I think. But I don't know why the above syntax doesn't work... :(

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.