- Home /
Problem with interface implementation or I simply dont get it
"I simply don't get it", because I lack of programming background and no matter how hard I searched for a workaround, I didn't manage to figure it out. So I probably need to do much more reading, but in case there is a straightforward answer, I'd be glad to know it and proceed further, instead of digging into these stuff that I barely understand in order to know where to focus exactly. The problem appears when I try to translate this C# class into Unityscript:
using UnityEngine;
public class TilePrefab : MonoBehaviour, IDFVirtualScrollingTile
{
....
public int VirtualScrollItemIndex { get; set; }
....
}
where the IDFVirtualScrollingTile interface is also written in C# as:
public interface IDFVirtualScrollingTile
{
int VirtualScrollItemIndex { get; set; }
....
}
So far, among other approaches, I have tried this:
#pragma strict
public class TestTilePrefab extends MonoBehaviour implements IDFVirtualScrollingTile
{
//I don't know how to translate the get, set line
}
but it has thrown different kinds of warnings and errors with the more popular to be:
when for example I don't include the before-mentioned line orBCE0141: Duplicate parameter name 'value' in 'TestTilePrefab.set_VirtualScrollItemIndex(int, int)
when I used some syntax of the javascript getter/setter method I found elsewhere. I even experimented to get the interface as Component, but I failed there too. Any kind of help is welcome.BCW0011: WARNING: Type 'TestTilePrefab' does not provide an implementation for 'IDFVirtualScrollingTile.VirtualScrollItemIndex', a stub has been created.
Answer by IshikiGames · Oct 10, 2015 at 09:59 AM
I would redefine your interface so that it is also javascript.
AFAIK, properties don't exist without specific implementation in javascript. Properties are just syntactic sugar for get and set functions (which are later generated by the compiler).
public int MyProperty { get; private set; }
is replaced with something like
private int m_BackingField;
public int get_MyProperty()
{
return m_BackingField;
}
private int set_MyProperty(int value)
{
m_BackingField = value;
return value;
}
Thank you people for your replies! I believe I have succeeded to make things work altogether. I only need some testing to check if everything's smooth. Once it's verified I'll come back to mark your suggestions as answers. @IshikiGames I couldn't manage to translate the C# interface into JS, but maybe that would affect other scripts written in C# that are depended on the interface, since they are all part of the DfGui plugin, which is reasonably written in C# (which leads me to comment Bunny83's reply).
Answer by Bunny83 · Oct 10, 2015 at 10:34 AM
The VirtualScrollItemIndex is a property. Specifically what you see inside your "TilePrefab" class is called an auto-property since it automatically creates a private "backing" field and the get and set methods for it.
As you can see over here UnityScript now also supports properties. The syntax is quite a bit different and (as far as i know) it doesn't support autoproperties. So you need to create the backing field and the get and set method yourself.
In C# it would look like this:
private int m_VirtualScrollItemIndex;
public int VirtualScrollItemIndex
{
get {
return m_VirtualScrollItemIndex;
}
set {
m_VirtualScrollItemIndex = value;
}
}
In UniyScript it would look like:
private var m_VirtualScrollItemIndex : int;
public function get VirtualScrollItemIndex() : int
{
return m_VirtualScrollItemIndex
}
public function set VirtualScrollItemIndex(value : int)
{
m_VirtualScrollItemIndex = value;
}
I don't use UnityScript, so i can't tell if that works along with using an interface. UnityScript has poor support for some programming concepts.
Hi Bunny83! I have regretted several times so far for having started this project in Javascript with which I was more familiar in advance. Anymore, I know in what language I'll work with the next one. (Being unable to even create regions in $$anonymous$$onodevelop has been, even if not an essential one, an inconvenient drawback nevertheless.) So far, it seems that the new syntax is able to join the jS with the C# interface. I'll just do some more testing. One last comment. I see that you have both used the characters "m_" in front of the property's name. Is this just for coding convenience purposes? I'm asking because I tried a similar syntax before, but it failed. It could be that I had done other mistakes however.
Well, in the past there was something that was called hungarian notation. Due to the lack of code completion in the IDEs back then it was quite common to use a prefix to include the type and kind of a variable. Hungarian notation isn't and shouldn't be used in modern languages like C# since we can get live feedback from the IDE which type a variable has.
Personally i use the m_
prefix for private or protected variables. It's not really hungarian notation since public fields are also "member" variables. However i like to mark private fields like that so it's clear it's a private / protected field. A field that really only belongs to the class itself. Public variables are often used as a kind of "interface" which you can directly use from outside. Usually I also prefix method parameters with "a" (for argument). I've seen people using just m
(mFieldName) however somehow i sticked with "m_".
Using "m_ " as prefix usually shouldn't cause problems. However since Unity is actually written in C++, where the "m_" prefix is still quite common, it can cause name collisions with already existing internal field which you don't actually see or can use, but the serialization system will have a problem. Just try setting the "asset seralization mode" (Unity -> Edit -> ProjectSettings -> Editor) to "Force Text". If you open your scene file with a text editor you can see the serialized scene. You will notice that almost all fields have the "m_" prefix.
Just as a tip: If you want to switch to C#, there's an easy (one way) solution.
Don't forget to create a backup of your whole project before you try this ^^.
build your game as it is now as standalone application.
If you haven't yet: download ILSpy and run it.
Open your "Assembly-UnityScript.dll" assembly which you will find inside "managed" folder inside the "XXX_Data" folder of your build.
$$anonymous$$ake sure you set the language to C#
Select your DLL and click File -> "Save code" to decompile the whole assembly as C# code.
Now you should be able to replace all the JS files with the CS files.
The resulting C# code should work out of the box. However you might want to "fix / simplify" all the overhead that might has been added by the UnityScript compiler.
$$anonymous$$eep in $$anonymous$$d that JS scripts inside Standard Assets and plugins are compiled into a seperate assembly called Assembly-UnityScript-firstpass. (It's the same for C# btw). So if you also havescripts in one of those folders you need to do the same thing for the firstpass assembly.
Hey, that's some solid information up there! Well explained answer, very helpful guidance and moreover a solution that I would not have imagined/thought, just out of the blue, myself and look for it. Communal support at its best! On behalf of anyone who will have the luck to enjoy your reply like myself and for the time you took to write it down, thank you a lot. :)
Your answer
Follow this Question
Related Questions
How to create an agenda or timetable system? 1 Answer
Script Component to implement interface 1 Answer
Ball delivery system ... back/down swing. 0 Answers
Retrieve Componet as Interface 1 Answer