How do I create a Unity 3D UI text element in an F# script? - unity3d

I'm attempting to create some ui elements in my F# script that is attached to a camera in Unity 3d. I know how to do this in C# but I'm not quite sure how to do it in F#.
Here's a simple C# example:
using UnityEngine;
using UnityEngine.UI;
public class CameraController : MonoBehaviour {
public Text uiText;
void Start ()
{
uiText.text = "Hello world!";
}
void Update ()
{
}
}
Now, here's my F# code. I've added comments that displays the compiler error I get when attempting to create a text element, This type has no accessible constructors.
namespace GameLogic
open UnityEngine
open UnityEngine.UI
type CameraController() =
inherit MonoBehaviour()
member this.uiText = Text // Compiler Error: This type has no accessible constructors
member this.Start() =
let uiText = Text // Compiler Error: This Type has no accessible constructors
()
member this.Update() =
()
It seems like F# implicitly calls a constructor method on Unity's Text object.
So, how can I create a ui text element in an F# script? Thanks for any and all help.
Kurt

In your C# code, the uiText member is getting initialized with null, which is the default for C#. In F#, there is no "default", you have to write the initial value explicitly:
member this.uiText: Text = null
This will compile, but won't give you the desired result. This syntax will create a read-only property that always returns null. In order to create a read/write property with initial value, you have to use member val:
member val uiText: Text = null with get, set
The 'get, set' part in there serves for clarification that you want the property to be readable and writable. Without it (by default), the property will be read-only.
For a complete description of how properties work in F#, see MSDN.

I ended up using a mutable let binding that specifies it is of type Text and its initial value is null. I then added the SerializeField attribute in order to associate UI Text elements in the Unity editor with the script.
Below is a simple example.
namespace GameLogic
open UnityEngine
open UnityEngine.UI
type CameraController() =
inherit MonoBehaviour()
[<SerializeField>]
let mutable uiText : Text = null
member this.Start() =
uiText.text <- "hello world!"
()
member this.Update() =
()

Related

class extend variable scope in unityscript

Let me be more specific here: This is used in Unity 2017 so the syntax they are using is this:
class CameraMotionBlurEditor extends Editor
{
var preview : SerializedProperty;
var previewScale : SerializedProperty;
...
function OnInspectorGUI () {
if (preview.boolValue) dosomething()
}
}
What I'm getting errors in is this preview.boolValue reference.. it claims it's ambiguous so therefore whatever this class is extending, must also have a declaration of that variable name. What I don't know is how to specify the local one.
The this keyword is used to refer to the current instance of the class. Retrieving the preview.boolValue from the current instance of the class hence becomes this.preview.boolValue:
function OnInspectorGUI () {
if (this.preview.boolValue) dosomething()
}
Note that UnityScript is slowly becoming deprecated, and the recommended route of action is to instead program Unity scripts in C#.

Access class property from instance?

I am not sure is this is correct behaviour or if its unintended. I have setup StealthFighter so that it returns a class type computed property variable called ammunition.
func globalTests() {
println("globalTests")
println("AMMUNITION: \(StealthFighter.ammunition)")
var myStealthFighter = StealthFighter()
println("MISSILES: \(myStealthFighter.missiles)")
println("AMMUNITION: \(myStealthFighter.ammunition)") // ERROR
}
class StealthFighter {
class var ammunition:Int {
return 500;
}
var missiles: Int = 5
}
When directly accessing the class StealthFighter this works fine and returns 500 as expected. But if I create and instance myStealthFighter and then try and access the class property on the instance I get the error: 'StealthFighter' does not have a member named 'ammunition' I can't find any mention of this, I am assuming from this that class properties are accessible only via the class? and not on any instances created from it? I just want to make sure I am understanding this correctly ...
EDIT:
So I have probably worded the type variable name wrong as it should probably be maxAmmunition to signify that StealthFighters can only take 500 rounds. I can see the point, if you want the maxAmmunition for the class then you ask the class.
As #Kreiri and #0x7fffffff points out it does seem that you can ask the instance what the class ammunition (or maxAmmunition) is by using dynamicType.
println("CLASS - AMMUNITION: \(StealthFighter.ammunition)")
var myStealthFighter = StealthFighter()
println("INSTA - AMMUNITION: \(myStealthFighter.dynamicType.ammunition)")
.
// OUTPUT
// CLASS - AMMUNITION: 500
// INSTA - AMMUNITION: 500
Your assumption is correct. Type variables are only meant to be accessed directly from the class. If you want to get at them from an instance, you can do so by accessing the dynamicType property on your instance, like so.
let theFighter = StealthFighter()
let missiles = theFighter.dynamicType.missiles
println(missiles)
However, I don't think that this is the correct approach for you to be taking here. Assuming that you want to have one class "StealthFighter", and possibly multiple instances of that class, each with the ability to have its own number of missiles independent of the others, you should probably make this an instance variable by simply ditching the class keyword.
dynamicType allows access instance’s runtime type as a value, so accessing class property from instance would look like this:
var myStealthFighter = StealthFighter()
myStealthFighter.dynamicType.ammunition
Works in playground, at least.
These properties are known as Type properties in swift. It should be called on its type ie class name, not on instance. Type properties holds same value across all the instances of the class just like static constant in C.
Querying and Setting Type Properties
Type properties are queried and set with dot syntax, just like instance properties. However, type properties are queried and set on the type, not on an instance of that type
Excerpt from : swift programming language
Swift 4:
var myStealthFighter = StealthFighter()
type(of: myStealthFighter).ammunition
Yes. This is a correct behaviour. These Type Properties can only be accessed over the Type and are not available on the instance itself.
In the Swift Book from Apple it is described in the section "Type Properties" (Page 205).
Swift Type Properties
“Unlike stored instance properties, you must always give stored type properties a default value. This is because the type itself does not have an initializer that can assign a value to a stored type property at initialization time"

Change G_PARAM_CONSTRUCT_ONLY property via inheritance

I try to inherit a gobject and, among other things, would like to change the value of a G_PARAM_CONSTRUCT_ONLY property so the next child class doesn't have to care.
Here's an example to depict this: GtkComboBox has a construct only property called "has-entry" with default value FALSE. In class A I want to change this value to TRUE, so that class B doesn't need to care.
GtkComboBoxClass <-- AClass <-- BClass
"has-entry" FALSE TRUE
The first naive approach was to use g_object_set() in A's instance_init function, but to no avail.
The next idea was to obtain the GParamSpec with g_object_class_find_property() and change the default value with g_param_value_set_default() in A's class_init function. But I suppose this to change the default for all GtkComboBoxClass derived objects.
The best idea I could come up with: If g_object_class_override_property() creates a new GParamSpec I could find this and set its default value in A's class_init function. But documentation doesn't loose a word about this.
So my question: Is this a working, and intended, way of accomplishing this, or is there a better solution?
Tried so far:
g_object_set() in instance init():
no warning on start
no effect
g_object_set() in GObjectClass->constructor():
no warning on start
no effect
warning on exit: invalid cast from GtkCellCiew to GtkEntry
g_object_set() in GObjectClass->constructed():
warning on start: can't be set after construction
Thanks
Stefan
if you want to set a property in a sub-class, and that property is construct-only, then you should use the constructed virtual function to call g_object_set() instead of the init virtual.
properties marked as construct-only will be applied during construction, using their default value, unless specified on the constructor itself — i.e. with g_object_new(). this means that setting a construct-only property inside init() will not suffice, as the value will be set after init() has been called. the constructed() virtual function, on the other hand, is called after the constructor properties have been applied, so it's possible to override the default value there.
Answering this for myself:
A look into gobject source reveals that the properties list given to constructor() contains all G_PARAM_CONSTRUCT and G_PARAM_CONSTRUCT_ONLY properties and their default or given values.
Modifying these values is undocumented (or at least I couldn't find it), but it works.
Construction time property values have to be modified in this list before chaining up to parents constructor, non construct properties have to be set afterwards. Example code looks like:
static GObject *constructor(GType gtype, guint n_properties, GObjectConstructParam *properties) {
GObject *object;
guint i;
gchar const *name;
GObjectConstructParam *property;
for (i = 0, property = properties; i < n_properties; ++i, ++property) {
name = g_param_spec_get_name(property->pspec);
if (!strcmp(name, "has-entry")) // is G_PARAM_CONSTRUCT_ONLY
g_value_set_boolean(property->value, TRUE);
}
object = G_OBJECT_CLASS(parent_class)->constructor(gtype, n_properties, properties);
g_object_set(object, "entry-text-column", TEXT_COLUMN, NULL);
return object;
}

Why do I get a lambda error that no-one else gets?

I'm trying to debug the following line:
MOrigValue.AllInstances.TestString = () => "New value";
There's a red squiggly line under:
() => "New value";
Mouseover shows the following error:
Delegate 'Microsoft.Moles.Framework.MolesDelegates.Func<OrigValueP.OrigValue, string>' does not take 0 arguments
Here is the complete class:
namespace OrigValueP
{
public class OrigValue
{
public string TestString() { return "Original value"; }
}
}
Here's the info from the object browser.
Click on the property MOrigValue.AllInstances.TestString:
public static Microsoft.Moles.Framework.MolesDelegates.Func<OrigValueP.OrigValue,string> TestString { set; }
Member of OrigValueP.Moles.MOrigValue.AllInstances
So, to a non-techie like me, that would explain the red squiggly line error above..
Click on the property MOrigValue..TestString:
public Microsoft.Moles.Framework.MolesDelegates.Func<string> TestString { set; }
Member of OrigValueP.Moles.MOrigValue
To me, this looks like the definition that I would have expected to see for MOrigValue.AllInstances.TestString. In other words a property that is actually a Moled "method" that has no parameters and returns a string.
As an experiment, based on the first object browser info above, I inserted the class as an input parameter, as follows:
MOrigValue.AllInstances.TestString = (OrigValue) => "New value";
This works :)
But my workaround looks like a "hack". I've seen every page on the internet (including StackOverflow) relating to moles and how to remove them painlessly. Many of them have lines with a lambda similar to the following:
MMyClass.AllInstances.DoSomething = () => "Hello world";
Assert.AreEqual("Hello world", new MyClass().DoSomething());
The fundamental issue is that Moles started from a method that takes no parameters and returns a string. The Moled equivalent takes its own class as a parameter and returns a string. Surely Moles knows that TestString() is a member of OrigValue.
Maybe my problem is a result of using VS Express, rather than the paid versions. I can live with that, but it would still be interesting to know why I need the hack. There might be cases where the hack produces incorrect test results without my knowledge.
BTW: I think this example proves the value of the object browser.
Your expectation is wrong. The "hack" you describe is the official documented way to use the AllInstances nested type. Its delegates really do always take a parameter containing an instance of the type under test.
It is unlikely that you could have seen this form of usage of AllInstances
MMyClass.AllInstances.DoSomething = () => "Hello world";
which, if you have, could be a mistake made by the author of the code.
What you expect to be the definition of a delegate belonging to the AllInstances type is really a different kind of use of Moles: it's used to detour an instance method of a single instance.
The "Mole Basics" section of the document "Microsoft Moles Reference Manual" contains more information on the topic. Here is an excerpt from there.
Instance Methods (for One Instance)
... The properties to set up those moles are instance methods of the mole type itself. Each instantiated mole type is also associated with a raw instance of a moled method type.
For example, given a class MyClass with an instance method MyMethod:
public class MyClass {
public int MyMethod() {
...
}
}
We can set up two mole types of MyMethod such that the first one always returns 5 and the second always returns 10:
var myClass1 = new MMyClass() { MyMethod = () => 5 };
var myClass2 = new MMyClass() { MyMethod = () => 10 };

GWT Exporter - How do i create an instance of object at runtime. Object type will be available as a string argument

I am using the following code in GWT client
Inside my jsni method I am using the following code, Assume that typeName is String argument
typeName = '$wnd.mysample.SampleButton'
var sample = new window[typeName]();
sample.addButton(name, parent);
SampleButton implements Exportable class, I used #ExportPackage("mysample") and #Export(all = true).
In My entry module I called ExporterUtil.exportAll();
Note: if I replace
var sample = new $wnd.mysample.SampleButton() with new window[typeName]() then it is working fine otherwise it is throwing undefined function called.
Kindly let me know how to create an instance for the type in JSNI code
eval('var sample = $wnd.mysample.SampleButton();'); solves the issue.