Howto:Exposing Subsystems to Nasal: Difference between revisions

Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 6: Line 6:


== Focus ==
== Focus ==
{{Main article|Resource Tracking for FlightGear}}
This article will specifically cover how to access the FlightGear event manager (SGEventMgr) and the listener API (SGPropertyChangeListener) to track registration and invocation of Nasal callbacks per frame.
This article will specifically cover how to access the FlightGear event manager (SGEventMgr) and the listener API (SGPropertyChangeListener) to track registration and invocation of Nasal callbacks per frame.


Line 147: Line 148:


=== FlightGear ===
=== FlightGear ===
{{Note|In its current form, this patch works perfectly well, but is specific to just the settimer() API, i.e. does not handle NasalTimerObj instances created via maketimer. However, the undelying approach is trivial enough, so can be easily extended.
Equally, it would make sense to not just log this info to the console but expose it in the property tree, or even provide dedicated Nasal APIs for getting this info at runtime, i.e. for further processing (think a dedicated GUI dialog)}}
<syntaxhighlight lang="diff">
diff --git a/src/Scripting/NasalSys.cxx b/src/Scripting/NasalSys.cxx
index dcd9965..27eb4c8 100644
--- a/src/Scripting/NasalSys.cxx
+++ b/src/Scripting/NasalSys.cxx
@@ -1307,6 +1307,13 @@ void FGNasalSys::setTimer(naContext c, int argc, naRef* args)
    t->gcKey = gcSave(handler);
    t->nasal = this;
+    SG_LOG(SG_NASAL, SG_ALERT,
+ "Nasal timer registration via settimer():\n"
+ "\tfile: " << naStr_data(naGetSourceFile(c,0)) <<
+ "\tline: " << naGetLine(c,0) << std::endl
+
+    );
+
    globals->get_event_mgr()->addEvent("NasalTimer",
                                        t, &NasalTimer::timerExpired,
                                        delta.num, simtime);
</syntaxhighlight>
To make the whole thing more useful, we will lower the logging priority and instead change the name of the added event to include the file name of the Nasal module and the line number where the settimer() call is invoked (which will be primarily useful in conjunction with the simgear patch above):
<syntaxhighlight lang="diff">
diff --git a/src/Scripting/NasalSys.cxx b/src/Scripting/NasalSys.cxx
index dcd9965..7f49b18 100644
--- a/src/Scripting/NasalSys.cxx
+++ b/src/Scripting/NasalSys.cxx
@@ -1307,7 +1307,25 @@ void FGNasalSys::setTimer(naContext c, int argc, naRef* args)
    t->gcKey = gcSave(handler);
    t->nasal = this;
-    globals->get_event_mgr()->addEvent("NasalTimer",
+    SG_LOG(SG_NASAL, SG_DEBUG,
+ "Nasal timer registration via settimer():\n"
+ "\tfile: " << naStr_data(naGetSourceFile(c,0)) <<
+ "\tline: " << naGetLine(c,0) << std::endl
+
+    );
+
+    if(delta.num <= 0.1)
+ SG_LOG(SG_NASAL, SG_ALERT, "Warning: settimer() should rarely be used per-frame !!");
+
+    std::string timerName("NasalTimer: ");
+
+    std::stringstream line;
+    line << naGetLine(c,0);
+
+    timerName += std::string(naStr_data(naGetSourceFile(c,0))) +" via line:"+line.str() ;
+   
+
+    globals->get_event_mgr()->addEvent(timerName,
                                        t, &NasalTimer::timerExpired,
                                        delta.num, simtime);
}
</syntaxhighlight>
Next, we can explore logging everything via properties, i.e. so that all this info shows up in the property tree using property objects
=== Base Package ===
=== Base Package ===

Navigation menu