Array Issue

Discuss suspected bugs with other users and Sim Innovations Staff

Moderators: russ, Ralph

Message
Author
User avatar
Sling
Posts: 5241
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Array Issue

#21 Post by Sling »

Good to see you have it working. I am testing what I posted earlier and this is what I find.

Example 1 - meaningful data

Code: Select all

--[[

EXAMPLE 1 - MEANINGFUL DATA

me = 11 just gives the error

ERROR: Error loading instrument script: logic.lua [string "logic.lua"]:38: attempt to index a number value (local 'wptx')

note: var me needs to be an array at least containing the elements you are trying to access

this code results in

INFO: outside 12 14
INFO: inside 12 14

]]

legs_data ={}

function import_flp(wptx)

legs_num =0
    if wptx == nil then
        print("wptx is nil")
        return
    end
    
    for i=1, 9 do
    
        legs_num = legs_num + 1
        legs_data[legs_num] = {}
        legs_data[legs_num][1] = 0
        legs_data[legs_num][2] = 0
        legs_data[legs_num][3] = 0
        legs_data[legs_num][4] = 0
        legs_data[legs_num][5] = 0
        legs_data[legs_num][6] = 0
        legs_data[legs_num][7] = wptx[i]
        legs_data[legs_num][8] = 0
    end
    
print("inside " .. legs_data[2][7] .. " " .. legs_data[4][7])

end

xpl_dataref_subscribe("laminar/B738/nd/wpt_x", "FLOAT[20]", import_flp)

me = {11,12,13,14}

import_flp(me)
print("outside " .. legs_data[2][7] .. " " .. legs_data[4][7])

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

Re: Array Issue

#22 Post by Sling »

Example 2 - use a timer

Code: Select all

--[[

EXAMPLE 2 - USE A TIMER

here a timer is used to call the outside print function.
you could just call the function by name if only needed once, but a timer allows multiple calls

this code results in

INFO: outside 12 14
INFO: inside 12 14

]]

legs_data ={}

function import_flp(wptx)

legs_num =0

    if wptx == nil then
        return
    end
    
    for i=1, 9 do
    
        legs_num = legs_num + 1
        legs_data[legs_num] = {}
        legs_data[legs_num][1] = 0
        legs_data[legs_num][2] = 0
        legs_data[legs_num][3] = 0
        legs_data[legs_num][4] = 0
        legs_data[legs_num][5] = 0
        legs_data[legs_num][6] = 0
        legs_data[legs_num][7] = wptx[i]
        legs_data[legs_num][8] = 0
    end
    
print("inside " .. legs_data[2][7] .. " " .. legs_data[4][7])
timer_start(0, 0, outside_print) -- only calls once but could modify to be what you need
--outside_print()   --if only needed once call function directly by name

end

function outside_print()
    print("outside " .. legs_data[2][7] .. " " .. legs_data[4][7])
end

xpl_dataref_subscribe("laminar/B738/nd/wpt_x", "FLOAT[20]", import_flp)
--these next 2 lines are just for testing without the sim, comment out when using sim
me = {11,12,13,14}
import_flp(me)


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

Re: Array Issue

#23 Post by Sling »

Example 3 - Check for nil

Code: Select all

--[[

EXAMPLE 3 - CHECK FOR NIL

here when checking for nil we populate the wptx array with zero's (well 9 of xplanes 20)
this could be modified to add the ability to return the last valid data in cases where the sim connection is lost

this code results in (note this is only tested with no sim connection, with a connection the values will change to whatever xplane is reporting after the initial first pass)

INFO: outside 0 0
INFO: inside 0 0

]]

legs_data ={}

function import_flp(wptx)

legs_num =0
    if wptx == nil then
        wptx = {0,0,0,0,0,0,0,0,0}
    end
    
    for i=1, 9 do
    
        legs_num = legs_num + 1
        legs_data[legs_num] = {}
        legs_data[legs_num][1] = 0
        legs_data[legs_num][2] = 0
        legs_data[legs_num][3] = 0
        legs_data[legs_num][4] = 0
        legs_data[legs_num][5] = 0
        legs_data[legs_num][6] = 0
        legs_data[legs_num][7] = wptx[i]
        legs_data[legs_num][8] = 0
    end
    
print("inside " .. legs_data[2][7] .. " " .. legs_data[4][7])

end

xpl_dataref_subscribe("laminar/B738/nd/wpt_x", "FLOAT[20]", import_flp)

import_flp()
print("outside " .. legs_data[2][7] .. " " .. legs_data[4][7])

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

Re: Array Issue

#24 Post by Sling »

After testing Example 4 - Ignore (don't use concatenation) I find that arrays behave as you would expect, differently from a standard variable so what I initially posted is only true for the array and not true when trying to print array elements.

You would of course not use print(array_var) or print("test " .. array_var) as they would still return the same results even with valid data as there is no index, but i'm just highlighting how trying to index an array of nil doesn't matter if you concatenate or not it still errors.

See my tests below.

Code: Select all

--[[

EXAMPLE 4 - PRINTING WITH CONCATENATION AND NIL VARS

un comment each print statement one at a time to see the effects of using print wuth a nil variable and array

]]

-- this results in INFO: null
--print(var)

-- this results in INFO: null
--print(array_var)

-- this results in ERROR: Error loading instrument script: logic.lua [string "logic.lua"]:16: attempt to index a nil value (global 'array_var')
--print(array_var[1])

-- this results in ERROR: Error loading instrument script: logic.lua [string "logic.lua"]:19: attempt to concatenate a nil value (global 'var')
--print("test " .. var)

-- this results in ERROR: Error loading instrument script: logic.lua [string "logic.lua"]:22: attempt to concatenate a nil value (global 'array_var')
--print("test " .. array_var)

-- this results in ERROR: Error loading instrument script: logic.lua [string "logic.lua"]:25: attempt to index a nil value (global 'array_var')
--print("test " .. array_var[1])

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

Re: Array Issue

#25 Post by Sling »

These examples are just to demonstrate what i've learned and I understand they do use elements of each other to achieve their result. I am just trying to record my findings for myself if I forget this at a later date and for the wider AM community.

I hope this helps someone down the track.

Sling

jedeen
Posts: 527
Joined: Fri Jan 13, 2017 5:41 pm

Re: Array Issue

#26 Post by jedeen »

Hello Sling,
I agree completely with you. I myself are "abusing" sometimes the fora to not forget how we "code freaks" (no offending) 8-) have achieved some results
just for not forgetting on what way we did to get those results.
I stated it earlier....maybe I have to use a timer in my code for getting the updated flight plans and maybe not.....it will be a trial and error cause.
The original 3 dataref's I have to subscribe to are arrays of [100].
At this time 2 of them only live in my own Laminar plane...so Xplane has to fill them and I have to read them and have to do something with the contents.
At this time I haven't the faintest idea what that will do with the FPS in AM or in Xplane.
In any case......thank you and Gilles for your help :)
Kind regards,
Derk

Post Reply