function 'si_variable_write' errors in the log

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
User avatar
a13
Posts: 37
Joined: Tue Oct 25, 2016 7:59 am
Location: Austria

function 'si_variable_write' errors in the log

#1 Post by a13 »

Hi,

i have a problem with the Inter-instrument communication ( si_variable_* ) and i am sure that there is some of you who can sort out my missunderstanding of the concept itself.

I have two instruments ( Battery Panel and EICAS )

Battery Panel

Code: Select all

----------------------------------------------------------
--	Global variables Battery and Avionics   			--
----------------------------------------------------------
sivar_elec_sys_op		= si_variable_create ( "si_electrical_systems_operative", "INT", 0)
sivar_eicas_page_selected  	= si_variable_create ( "si_eicas_page_selected", "STRING", "BOOT")

Code: Select all

--------------------------------------------------------------
-- Electrical Power available?
--------------------------------------------------------------
function callback_elec_power ( battery, crosstie, avionics, voltage, elec_sys_op, eicas_page_selected )
	if battery[1] == 1 and crosstie == 1 and avionics == 1 and voltage[1] >= 20
		or battery[2] == 1 and crosstie == 1 and avionics == 1 and voltage[2] >= 20
	then
		si_variable_write ( sivar_elec_sys_op, 1 )
		si_variable_write ( sivar_eicas_page_selected, "NORM" )
	else
		si_variable_write ( sivar_elec_sys_op, 0 )
		si_variable_write ( sivar_eicas_page_selected, "BOOT" )
	end
	print("Page is : " .. eicas_page_selected)
end
variable_subscribe( 	"XPLANE",	"sim/cockpit/electrical/battery_array_on" , "INT[8]" , 
						"XPLANE",	"sim/cockpit2/electrical/cross_tie" , "INT" ,
						"XPLANE",	"sim/cockpit2/switches/avionics_power_on", "INT",
						"XPLANE",	"sim/cockpit2/electrical/battery_voltage_actual_volts", "FLOAT[8]",
						"SI", 		"si_electrical_systems_operative", "INT",
						"SI",		"si_eicas_page_selected", "STRING",
						callback_elec_power )
The console switches fine between BOOT and NORM

The second instrument does not create the si_vars again, but uses them of course and creates some itself:

One Switch and one callback as example

EICAS

Code: Select all

----------------------------------------------------------
--	Global variables EICAS                     			--
----------------------------------------------------------
-- Provided by Battery and Avionics
-- sivar_elec_sys_op			= si_variable_create ( "si_electrical_systems_operative", "INT", 0)
-- sivar_eicas_page_selected	= si_variable_create ( "si_eicas_page_selected", "STRING", "")

-- Self provided si_vars
sivar_checklist_selected	= si_variable_create ( "si_switches_eicas_checklist_selected", "STRING", "NONE")
sivar_checklist_active_page = si_variable_create ( "si_switches_eicas_checklist_active_page", "INT", 0)
sivar_checklist_pages_total = si_variable_create ( "si_switches_eicas_checklist_pages_total", "INT", 0)

Code: Select all

--------------------------------------------------------------
--	Switch 100_NORM				    						--
--------------------------------------------------------------
function click_100_norm ()
	si_variable_write (sivar_eicas_page_selected, "NORM") -- ERROR
	request_callback ( callback_100_norm )
end
switch_eicas_norm = switch_add ( "btn_norm.png", 		  "btn_norm_active.png",	 		 50, 1020, 80, 40, click_100_norm)

Code: Select all

--------------------------------------------------------------
--	Callback 100_NORM			    						--
--------------------------------------------------------------
function callback_100_norm ( elec_sys_op, eicas_page_selected, eicas_checklist_selected, eicas_checklist_active_page, eicas_checklist_pages_total )
	if	elec_sys_op == 1 and eicas_page_selected == "NORM" then
		switch_set_position ( switch_eicas_norm,	1 )
		visible ( group_100_norm,					1 )
	else
		switch_set_position ( switch_eicas_norm,	0 )
		visible ( group_100_norm,					0 )
        si_variable_write (sivar_eicas_page_selected, "NOT-NORM") -- ERROR
	end
end
variable_subscribe( "SI", "si_electrical_systems_operative", "INT",
					"SI", "si_eicas_page_selected", "STRING",
					"SI", "si_switches_eicas_checklist_selected", "STRING",
					"SI", "si_switches_eicas_checklist_active_page", "INT",
					"SI", "si_switches_eicas_checklist_pages_total", "INT",
					callback_100_norm )
How do i write a si_var* from a click_switch and from a callback without getting these errors in the log (Version 4 and BETA 16)?
Argument 'var_id(1)' in function 'si_variable_write' is nil, which is not allowed
Argument 'var_id(1)' in function 'si_variable_write' is not a valid reference, which is not allowed
and so on.

Thanks Stefan

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: function 'si_variable_write' errors in the log

#2 Post by Sling »

Hi Stefan,

It’s because you cannot do this and the other places where you are using an id that does not exist in that instrument.

Code: Select all

si_variable_write (sivar_eicas_page_selected, "NORM")
It only exists in the first instrument. Most uses of iic only have one instrument as the one doing the writes but you can do what you want if you create the exact same sI variable in each instrument that needs to write. It seems all you need to do is un-comment those creates that you already have at the top. This then creates an id in each instrument to use in the write function.

I hope this makes sense. If not post back and I’ll explain some more. Let us know how you go.

Tony

User avatar
a13
Posts: 37
Joined: Tue Oct 25, 2016 7:59 am
Location: Austria

Re: function 'si_variable_write' errors in the log

#3 Post by a13 »

Hi Sling,
thanks for the hint!
And i thought, the iis is created in one instrument and published (or at least available) in every other instrument as the communication goes through the air plugin and can be consumed in every other instrument...
"These functions are used to communicate between instruments" (API-Wiki)
I will try asap and get back to you.
Stefan

User avatar
a13
Posts: 37
Joined: Tue Oct 25, 2016 7:59 am
Location: Austria

Re: function 'si_variable_write' errors in the log

#4 Post by a13 »

Hi Sling and all of the community,

it does not work - sorry. Nevertheless enabling the si_vars in the instrument was a good hint.
The si_var seems not to be written when clicking on a switch, here is what i have prepared:

Code: Select all

--------------------------------------------------------------
--	Debug options											--
--															--
--------------------------------------------------------------
_debug = 1

--------------------------------------------------------------
--	Debug Informations									    --
--	switch on with _debug									--
-------------------------------------------------------------- 
txt_debug 	= "size:24px; font:arimo_regular.ttf; color: col_red; halign: center; valign: left;"
txt_debug_1 = txt_add (	"debug_1", txt_debug,	10,  870 , 200 ,30 )
txt_debug_2 = txt_add (	"debug_2", txt_debug,	10,  900 , 200 ,30 )
txt_debug_3 = txt_add (	"debug_3", txt_debug,	10,  930 , 200 ,30 )
visible ( txt_debug_1 , _debug )
visible ( txt_debug_2 , _debug )
visible ( txt_debug_3 , _debug )

-- SI_VARS
sivar_elec_sys_op			= si_variable_create ( "si_electrical_systems_operative", "INT", 0)
sivar_eicas_page_selected	= si_variable_create ( "si_eicas_page_selected", "STRING", "NONE")

SWITCH
function click_100_norm ()
	var_eicas_page = "NORM"
	si_variable_write (sivar_eicas_page_selected, var_eicas_page)
	txt_set ( txt_debug_1, "Click " .. var_eicas_page )
	request_callback ( callback_100_norm )
end
switch_eicas_norm = switch_add ( "btn_norm.png", 		  "btn_norm_active.png",	 		 50, 1020, 80, 40, click_100_norm)

function callback_100_norm ( elec_sys_op, eicas_page_selected )
	if	elec_sys_op == 1 and eicas_page_selected == "NORM" then
		txt_set ( txt_debug_1, "NORM via SI" )
	end
	if elec_sys_op == 1 and var_eicas_page == "NORM" then
		txt_set ( txt_debug_3, "NORM via VAR" )
	end
end
variable_subscribe ( "SI", "si_electrical_systems_operative", "INT",
			       "SI", "si_eicas_page_selected", "STRING",
				callback_100_norm )

I never get the txt_debug_1 to show NORM via SI, but always txt_debug_3 NORM via VAR

When i change this to only write the local var_eicas_page when clicking the switch and write the si_var from the callback it works, but this cannot be a smart solution.

I am a little bit clueless right now...
Stefan

Tetrachromat
Posts: 236
Joined: Sun Feb 14, 2021 6:55 pm

Re: function 'si_variable_write' errors in the log

#5 Post by Tetrachromat »

Hi Stefan

I did a short review of your code.

First, I think the responsibilities are not correctly distributed between the 2 instruments. Thats why the code get's so convoluted.
You have 2 instruments 'battery' and 'eicas'. Instrument 'battery' should only be responsible to publish its own state, but it does publish the page state of the 'eicas' instrument. This should be the responsibility of 'eicas'.

Second, there is no real internal state maintained in the instruments. I am missing some local variables or data structures that represent the state. If you do not maintain an instrument state, you can not publish anything. What your 'battery' instrument publishes is not it's own state. It is an aggregation of some xplane states that 'battery' has subscribed to. It is just republishing what is already known ('eicas' could have subscribed to this by itself).

Third, the use of request_callback is not necessary in such a running scenario. It might be of use in initializing a instrument on startup. There is something wrong in the design if the publish-subscribe cyle does not work without it.

I recommend to rethink the design. Assign responsibilities to the instruments, then define the state an instrument needs to maintain to fullfill it's responsibilities. After this is set, the code is the easy part.

Paul

User avatar
Ralph
Posts: 7915
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: function 'si_variable_write' errors in the log

#6 Post by Ralph »

Maybe I'm missing a piece of script, but in your SI variable callback function, you were checking for elec_sys_op to be 1. But this one is initialized as 0, and it is not set to 1, so it will never reach the txt_set.

I have modified your script a bit to set elec_sys_op to 1 as well, and that works fine here. Give the attached instrument a try.
Test - SI variable write example.siff
(23.5 KiB) Downloaded 91 times

User avatar
a13
Posts: 37
Joined: Tue Oct 25, 2016 7:59 am
Location: Austria

Re: function 'si_variable_write' errors in the log

#7 Post by a13 »

Thanks to Sling, Tetrachromat and Ralph,
for your time you spend on my problem.
I will be out of the Stubai-Valley for the next two weeks, will give you a feedback after return.
a13
#stayhealth

Post Reply