Implementing lua scripts for temperature adjustments

For topics related to the creation of simulation objects (SimObjects). This includes development of aircraft, ground, and maritime vehicles.
Post Reply
tauseefAhmed
Posts: 38
Joined: Wed Jul 31, 2019 4:56 pm

Implementing lua scripts for temperature adjustments

Post by tauseefAhmed »

A friendly hello to all!
For past few days I've been trying to figure out how IR (Black Hot & White Hot) sensor works in P3d.
Since the actual IR sensor on a system (i.e FLIR Brite Star) reads heat signatures of ground entities to distinguish them in IR mode, P3D must be doing something similar. While I was going through the learning center, I found about Lua scripting and some of my questions were answered.
The only thing I don't understand is how do we integrate lua with P3d such that it assigns temperature value to the ground entities (i.e vehicles, Buildings, Water bodies etc).
I'm creating AI objects using simConnect and number of simObjects would vary at times. Is there any possibility to assign temperature value to an object at runtime?
I know how lua uses variables to get and set values in p3d and I have also worked on ActionScript to modify guages and targeting Pod. I just need a little help to figure out how do I assign heat value to individual entities.

I would really appreciate if LM guides me through this.

Thank you for your attention.

Best Regards,
Tauseef
Clifton Crane
Lockheed Martin
Posts: 1207
Joined: Tue Sep 25, 2012 2:34 pm

Re: Implementing lua scripts for temperature adjustments

Post by Clifton Crane »

Hello,

One way to dynamically modify how IR renders an object is by scripting it into the 3D model.

The model's materials have a Temperature Scale setting that can be used to adjust the brightness of the material in an IR view. This Temperature Scale value can also be modified by a lua script attached to the material (see IR Scenery Object sample in the SDK and Learning Center).

Another option would be to visibility script parts on and off where each part could have different Temperature Scale values in their material (i.e. cold wheel, warm wheel, hot wheel).

Unfortunately there is no overall heat setting that can be applied to the entire SimObject.
Clifton Crane
Prepar3D® Software Engineer Sr.
tauseefAhmed
Posts: 38
Joined: Wed Jul 31, 2019 4:56 pm

Re: Implementing lua scripts for temperature adjustments

Post by tauseefAhmed »

Hello! Thank you for your attention.

Yeah I've gone through the IRSceneryObject example. It applies temperature scale on the entire scenery right?
I want to set different temperature scale for different entities in a scenery, for example a tank may have higher temperature than a jeep, and a jeep may have higher temperature scale than a motorcycle.

Setting the temperature using 3D modeling tool won't be the ideal solution for me as the temperature may be different during day/night as well as when it is a sunny day vs when there's a snowfall going on. I need to set the temperature when scenery is loaded and after checking the weather conditions. And yes, changing the temperature scale of entire Object is not possible, neither is it necessary so far.

Do I have to write a separate lua script for each of the entities (i.e in SimObjects\Airplanes\F-16 Base\Scripts\script.lua)?
Clifton Crane
Lockheed Martin
Posts: 1207
Joined: Tue Sep 25, 2012 2:34 pm

Re: Implementing lua scripts for temperature adjustments

Post by Clifton Crane »

I've gone through the IRSceneryObject example. It applies temperature scale on the entire scenery right?
Yes. That sample is set to use a single material for the entire model. However it is possible to script individual materials within the model.
Setting the temperature using 3D modeling tool won't be the ideal solution for me as the temperature may be different during day/night as well as when it is a sunny day vs when there's a snowfall going on. I need to set the temperature when scenery is loaded and after checking the weather conditions.
The lua script can access weather information as well as time of day, so it would be possible to script the material's temperature scalar based on different conditions. You may want to have a look at Object Variables and Environment Variables in the Learning Center for the list of available variables.
Do I have to write a separate lua script for each of the entities (i.e in SimObjects\Airplanes\F-16 Base\Scripts\script.lua)?
Material scripts can be shared between objects. There is a global Scripts directory in the Prepar3D application directory. Scripts added here can be accessed by any object. Additional Script directories can be added using the Add-On method, which is the preferred method. Scripts added under SimObject directories will only be accessible by that specific SimObject. Note that these directories also work in a fallback manner, so a SimObject may also look for scripts from with in the global directory.

So you can write generic scripts that can be used by multiple SimObjects as long as the files are in the correct location.

Hope this helps!
Clifton Crane
Prepar3D® Software Engineer Sr.
tauseefAhmed
Posts: 38
Joined: Wed Jul 31, 2019 4:56 pm

Re: Implementing lua scripts for temperature adjustments

Post by tauseefAhmed »

Thank you very much. Really appreciate your help.
I'll follow your instructions.
I hope you won't mind if I come back with another question (if any) regarding this issue.
tauseefAhmed
Posts: 38
Joined: Wed Jul 31, 2019 4:56 pm

Re: Implementing lua scripts for temperature adjustments

Post by tauseefAhmed »

Hello Clifton,

As you suggested, I added the lua script in the Scripts folder of each SimObject I need the temperatureScale for, but I didn't see any effect on Objects in IR camera.
So I added the scripts in the model using 3Ds Max. Now the objects seem to have different temperatureScale visible in IR mode (Bingo), depending on which of them have the script embedded in their model.
But I'm unable to figure out how to read this temperatureScale at runtime.
I tried to access the variable in Scaleform using Adobe Animate, but it does not access the Material variable.

Then I tried to get the object properties in SimConnect. But it only gets the LLA, Position, Health etc. and not the temperatureScale value.

I want to read the object temperatureScale when object is Locked in TargetingPod.
Clifton Crane
Lockheed Martin
Posts: 1207
Joined: Tue Sep 25, 2012 2:34 pm

Re: Implementing lua scripts for temperature adjustments

Post by Clifton Crane »

Hello,

Material variables (T:) are currently only available to the individual material scripts. They cannot be accessed by SimConnect.

There is no simulation variable (A:) that represents temperature scale, however you could register a custom property onto the SimObject using the PDK. In MyEngine SDK sample, the MyEngineThrottle custom property is registered onto the native Airplane simulation. Once the property is registered, you could have your plugin drive the temperature value and have your material script read from that value:

Code: Select all

varset("T:TemperatureScale", "number", varget("A:MyCustomTemperatureScaleProperty", "number"))

SimConnect should then be able to read and write the value to and from "MyCustomTemperatureScaleProperty".

If you didn't want to register the custom property, another work around would be to set the temperature value into an unused native simulation variable.

Hope this helps!

Edit: Corrected code sample so that simulation variable is being set into material variable. Material scripts cannot currently set simulation variables.
Clifton Crane
Prepar3D® Software Engineer Sr.
tauseefAhmed
Posts: 38
Joined: Wed Jul 31, 2019 4:56 pm

Re: Implementing lua scripts for temperature adjustments

Post by tauseefAhmed »

Thank you for the reply,
There's one more thing I'm having trouble with. If I add the lua script in the model using the 3Ds Max, I can see the texture in IR mode a little bit different from the model having no script embedded into its model.
Now when I add a lua script in Add-ons, no matter which directory the script lies in, only the object having a lua script embedded into its model takes effect of the script. (i.e it becomes brighter in WH and darker in BH mode), but the Add-on script seem to have no effect at all on the objects having no script in their model.
What could be the reason for that?

Also, I have tried to add Scripts inside the individual objects as follows.

[fltsim.01]
title=T-72
script=custom
texture=
sound=

My directory structureis as follows:

SimObjects\GroundVehicles\T-72
SimObjects\GroundVehicles\T-72\sim.cfg
SimObjects\GroundVehicles\T-72\script.custom\temperature.lua

But it doesn't seem to have any effect on the models either.

Edit: If I add the above path to the Add-on, it does not have any effect on this particular model. Rather it changes the temperature scale of the objects having a script embedded into their model using 3Ds Max.
tauseefAhmed
Posts: 38
Joined: Wed Jul 31, 2019 4:56 pm

Re: Implementing lua scripts for temperature adjustments

Post by tauseefAhmed »

I've got your point. Thank you for explaining nicely.
When I set the temperature value at some point, would it set the temperature scale of entire scenery? Or we can get the temperature value of individual simObjects?
For example, if I have a Tank and a Truck moving side by side, if there a way to set temperature for both of them using a their respective lua scripts, (or a single script if possible), read the temperatureScale of both the vehicles and then use them? (i.e compare the temperature of both vehicles and perform actions based on that comparison).
Clifton Crane
Lockheed Martin
Posts: 1207
Joined: Tue Sep 25, 2012 2:34 pm

Re: Implementing lua scripts for temperature adjustments

Post by Clifton Crane »

Hello,

The correct SDK sample to reference is MyEngine, I've updated the above post.

I misspoke in my previous post. Unfortunately, lua scripts cannot varset simulation variables (A:). One option would be to have your plugin drive the custom temperature variable. Then the material script could varget the temperature from the simulation variable (A:) and varset it into the material variable (T:TemperatureScale).

Lua scripts have to be added to the model's material in 3dsMax. They won't be referenced otherwise. Lua scripts are not baked into the MDL file, they are looked up by filename at runtime. This will happen in a fallback manner. If you are specifying a script directory in your SimObject's aircraft.cfg or sim.cfg, this would be the first place it will look, followed by global and add-on defined directories.

If the lua script is attached to a SimObject's model, it will have access to that individual SimObject's simulation variables (A:).

Sorry for the confusion.
Clifton Crane
Prepar3D® Software Engineer Sr.
Clifton Crane
Lockheed Martin
Posts: 1207
Joined: Tue Sep 25, 2012 2:34 pm

Re: Implementing lua scripts for temperature adjustments

Post by Clifton Crane »

Hi Tauseef,

Would it be easier to determine your temperate scale value in both the material script as well as the SimConnect add-on? Instead of trying to pull the temperature scale from the individual object, your SimConnect application could recalculate based on the object selected by the targeting pod.
Clifton Crane
Prepar3D® Software Engineer Sr.
tauseefAhmed
Posts: 38
Joined: Wed Jul 31, 2019 4:56 pm

Re: Implementing lua scripts for temperature adjustments

Post by tauseefAhmed »

Hello Clifton,
Thank you for your time.
Clifton Crane wrote: Wed Oct 30, 2019 1:38 pm Hello,

The correct SDK sample to reference is MyEngine, I've updated the above post.
I'll take a look at it. I have found the previously mentioned Sample in SDK v4.3 though.

I've figured out how to add a script for individual Objects. For that, first we have to add a script in the object's material. Then we need to add the script with the same name in the script folder in SimObjects/MyObject directory.
Now I can set different temperature values to different objects and also change it based on time of day and weather conditions. There's no need to add the script in Add-on.
I misspoke in my previous post. Unfortunately, lua scripts cannot varset simulation variables (A:). One option would be to have your plugin drive the custom temperature variable. Then the material script could varget the temperature from the simulation variable (A:) and varset it into the material variable (T:TemperatureScale).
Yes you're right. I tried to set the Temperature value in Zulu Time variable as I wasn't using it for anything. Then I tried to get the variable in ScaleForm and display it on TargetingPod View, but it didn't show any value.
Clifton Crane wrote: Wed Oct 30, 2019 1:38 pm Sorry for the confusion.
That's not a problem. You've really helped me a lot so far for sure. I really appreciate that.
tauseefAhmed
Posts: 38
Joined: Wed Jul 31, 2019 4:56 pm

Re: Implementing lua scripts for temperature adjustments

Post by tauseefAhmed »

Hello Clifton,
Would it be easier to determine your temperate scale value in both the material script as well as the SimConnect add-on? Instead of trying to pull the temperature scale from the individual object, your SimConnect application could recalculate based on the object selected by the targeting pod.
As I mentioned in above post, I have figured it out how to add temperature value to individual objects.
Now I'm trying to read them and perform operation based on their temperature values.

To be precise, Let's say there are two Tanks side by side. I want to lock one of them in TargetingPod if it has higher temperature value than the other (i.e when its engine is running). After locked, if the object's temperature becomes similar to the other, lock breaks automatically (i.e when its engine is turned off and temperature of both Tanks is now same).

I hope I explained it right.

The engine on and off would be checked in lua script while setting the temperature value along with weather as well as time of day information. That won't be an issue.

So I need to have access of multiple objects simultaneously. Right?
Clifton Crane
Lockheed Martin
Posts: 1207
Joined: Tue Sep 25, 2012 2:34 pm

Re: Implementing lua scripts for temperature adjustments

Post by Clifton Crane »

Hi tauseef,

For your use case I would recommend having your plugin calculate the temperature scale instead of the lua script.

The PDK ISimObjectManagerV440 service has a GetObjectsInRadius function. This could be used to iterate over individual SimObjects. For each object, you could calculate the temperature scale based on engine state, weather, time of day, etc. Then the temperature scale would be set into that individual object's "TemperatureScale" custom property. After that your lua material script would only need to read the "A:TemperatureScale" property and set it into the materials "T:TemperatureScale" variable.

You would also be able to get the "TemperatureScale" custom property value in your C++ plugin. The ISimObjectManagerV440 has a GetObject function that takes in an object ID and returns an IBaseObject. The IBaseObject has GetProperty functions that can be used to retrieve the value of the "TemperatureScale" custom property. This could be used to determine the target for the targeting pod.

With this approach you would have a temperature scale value for individual objects that both your plugin and lua scripts can read from.

Hope this helps!
Clifton Crane
Prepar3D® Software Engineer Sr.
tauseefAhmed
Posts: 38
Joined: Wed Jul 31, 2019 4:56 pm

Re: Implementing lua scripts for temperature adjustments

Post by tauseefAhmed »

Hello Clifton.
Sorry for late update.
I have worked as per your suggestion and it solved my issue. Kudos to you.

Now I'm able to read custom temperatureScale in my plugin and set it using Lua script.


Thank you very much for your help
Post Reply