Hooray wrote in Mon Jan 27, 2020 10:24 pm:just briefly, your code looks fine - the style you're using is actually consistent with more advanced programming concepts (e.g. use of anonymous functions) - you could simplify the code a little by moving the node/path out of the loop body into a string variable, and then use that variable instead.
Yes, i've already thought of that, too.
Also, no need for string concatenation - i..e getprop() supports an optional index argument, too (see the wiki for details)
This?
- Code: Select all
getprop("/sim/view", i, "name")
Note that you will want to stop the timer, to prevent it from running too often.
That's a question i wanted to ask as well. Can I kill the timer from inside the timer function?
One simple option would be testing the whole thing as part of a Canvas GUI dialog and then register an event handler to remove the timer once the dialog is closed.
As in "manually closed"?
Not my preferred option. i want the dialog to kill itself once input values are nominal again.
For any logging, you can just create a simple wrapper "loginfo()" func and then use print/printlog/logprint or tooltips as needed - that way, you only have to touch a single single, the loop body would only call your wrapper
I'm thinking about a file io based debug log as the Nasal console tends to swallow my outputs too fast and CTRL+F'ing through fgfs.log is...tedious.
For the conditional check, you could introduce a helper function "inRange()" - again, only relevant if you are concerned about readability.
Not sure about that. My rule of thumb is that a function is only required when absolutely necessary or used more than once.
Apart from that, it seems that your Lua background comes in handy already
But only after finding out that I can get away with doing things Lua style in Nasal as well.
The listener had me a bit confused as I really thought that this was the only means of getting a property's value without flooding FG to death.
One thing I still need to figure out is whether doing setprop() every frame will also flood FG. If it does, I'd just force pause&freeze the sim until the input controller is moved and then unpause&unfreeze it.
By the way:
This is a simple Lua script for X-Plane. It gets the current day from the OS and then adjusts the dataref (=property) within X-Plane to match that day before going into idle mode. "Do_sometimes" is just the plugin's short version for a ten second timer.
In Lua, variables are assumed to be global unless specifically declared local. Dataref_table is the only "getprop"-like method that will permit local dataref (property) access (table = vector in Nasal). Global access may cause issues or even simulator crashes when written to by two different scripts. Nasal is much more safe and simple in that regard, handling access to variables from another script on what would amount to communication between two modules in Lua.
Also note the absence of any end marks for statements and curly brackets.
- Code: Select all
local day_os=(os.date("%j") - 1) -- XP starts day count at zero, therefor os count needs to be adjusted
local day_sim=dataref_table("sim/time/local_date_days")
local date_synched = 0
function Datesync()
if date_synched == 0 then
print("Initial OS Day is "..day_os..", sim day is "..day_sim[0])
if day_os ~= day_sim[0] then
day_sim[0] = day_os
print("OS day and sim day synched. ("..day_sim[0].."/"..day_os..")")
end
date_synched = 1
end
end
do_sometimes("Datesync()")
You could maybe indeed write a Nasal-based Lua parser for such simple scripts but there's still way too many special cases (like the aforementioned timer) that would require extensive conversion work.