-
Notifications
You must be signed in to change notification settings - Fork 98
NCalc scripting Introduction
Ncalc is used into Simhub as a calculation engine. It's used to create some unique behavior, or let the user make some simple calculations.
It's not easy to let the game run while you are trying to make a proper formula. SimHub offers a "replay" feature :
The record / Replay is accessible in the top of the window :
- Press on "Record".
- Go into your favorite game and a make a few laps.
- Press on live.
- Your record is now done
You can now replay your record infinitely by pressing "replay"
The editor can be accessed for some easy formula testing from the properties list, it will be accessible too from DashStudio bindings, Screen editor or even Led Layout editor :
Using the property picker (insert property)
Search for the speed and double click on it
The property representing the speed will now get inserted into the formula.
A property is marked by the brakets ('[' and ']')
Congratulation you have made your first ncalc formula !
Now that we know how to insert a property let's do some basic maths. For instance we want to get RPMs at the "K" format (1 for 1000RPM). We need to divide the rpm by 1000.
Here is the result formula
[DataCorePlugin.GameData.NewData.Rpms] / 1000
The formula is self-explonary.
But captain ! Why I don't get
1
? i got1.646
instead !
Indeed if the rpm was 1 646
, the correct result is indeed 1.646
We have now two ways to fix this :
- Rounding
- Truncating
NCalc Offers a set of functions, lets use them to round it.
- Click on Insert function and double click on 'Round'
- Then modify the formula according to this
Round([DataCorePlugin.GameData.NewData.Rpms] / 1000, 0)
We ask the 'Round' function to use [DataCorePlugin.GameData.NewData.Rpms] / 1000
as a value and 0
as the rounding point, the function "parameters" are separated by a "," and all the a parameters are surrounded by (
and )
The result will now be 2 for 1 646 RPM
But captain ! Why I don't get
1
? i got2
instead !
Indeed round
will round to the closest integer.
Let's change that.
Truncate([DataCorePlugin.GameData.NewData.Rpms] / 1000)
We have replaced the round
function by truncate
which only keeps the integer part of the value.
The result will now be 1 for 1 646 RPM
We've just seen how to manipulate numbers, Ncalc offers ways to manipulate boolean values. Boolean are a kind of values representing true
or false
this kind of value is used in the "visible" properties of the DashStudio componenents for instance.
In the visible property open the binding editor and type
[DataCorePlugin.GameData.NewData.FilteredSpeedLocal] > 100
Raw result will now show True
or False
which means that the result of your formula is a boolean
We can combine one or more of the previous concepts to create some combined displays. Let's say that we only have a tm1637 with only 4 characters available. So we want it to display our current gear when we change it otherwise the speed.
For this we will use the changed
function who returns true
for a defined amount of time. We will also use the 'if' function allowing to switch between two values depending of a boolean
result
if(changed(500, [DataCorePlugin.GameData.NewData.Gear]),'[' + [DataCorePlugin.GameData.NewData.Gear] + ']', truncate([DataCorePlugin.GameData.NewData.SpeedLocal]))
Lets explain that miracle
- 'if'
- The value of [DataCorePlugin.GameData.NewData.Gear] has changed during the last 500 ms (changed function)
- Then return
- '[' + [DataCorePlugin.GameData.NewData.Gear] + ']' : which will return the gear like this '[4]'
- Else return
- truncate([DataCorePlugin.GameData.NewData.SpeedLocal]) : the truncated speed (with no decimal part)