DynValue concept is quite at the root of all MoonSharp, and while we have managed to have little to do with it so far, in fact it’s not so feasible to go very far
without touching the subject.
As said in the title, (almost) everything in MoonSharp is an instance of a
DynValue object. A
DynValue represents a value in the script, whatever type it has, and so
it can be a table, a function, a number, a string and everything else too.
So, to start, let’s start with the latest tutorial step and change it to use DynValue(s) instead.
Again, we start from the last step of the previous one. You did it, right ?
You can get the reference document here.
The first change, we get the
fact variable in a different way:
Ok, let’s spend some words on this line:
This gets the
fact function from the script globals.
Get method, opposite to the indexer property, does return a
DynValue - the indexer, instead, attempts to convert it to a
In this case we are interested on the
DynValue, so we use the
Ok, now if we are interested in avoiding yet another conversion, we can also pass directly a number instead of using the implicit conversions the
Call method gently offers.
Remember, here it’s not needed but somewhere else, it might be, not everything does magic conversions for you!
So, we replaced our number with
DynValue has a lot of factory methods, all starting with “New” (like
NewBoolean, etc.) which can be used to build our values from scratch.
It also has a handy
FromObject method, which creates a
DynValue from object and is just what
Call was using, under the hood, to ease our lives.
One of the most important properties in
DynValue is Type.
Type property is an enumeration telling us what kind of data is contained in a
We can query the
Type property everytime we want to know what is contained in a
It’s important to know that some properties of
DynValue have a meaningful value if and only if the value is of a given type. For example, the
Number property is
guaranteed to have a meaningful value only if the type is
DataType.Number, and the same for the
Remember to always check the Type of a DynValue before using its properties unless you are sure of what it contains. Assuming a
DynValuecontains a given type while it contains another, is a common source of mistakes and errors.
As you (should) know, Lua can return multiple values from a function (and in other situations).
To handle this, a special
DynValue type (
DataType.Tuple) is used. A tuple has a
Tuple property which is an array of
DynValue objects which are the members of the tuple.
This is simpler than it sounds:
We are done with DynValues. There’s a lot more to learn, but this should be enough to get you started on the topic.
Keep all this in mind, as this is the core everything revolves around.