- Home /
Convert C# Into UnityScript - Events in Unityscript
Hello,
I'm trying to convert a piece of C# into JavaScript, but I came across some C# that I've never seen before, doesn't even look syntax valid:
StoreKitManager.productListReceivedEvent += allProducts =>
{
Debug.Log( "received total products: " + allProducts.Count );
_products = allProducts;
};
What the heck does the 'allProducts => {' mean? It doesn't even have a variable called 'allProducts', unless its referring to something within the StoreKitManager?
Did you try removing the invalid syntax? (=>) means equal or greater than, which is usually for an if() statement.
It's not invalid though - the C# script runs fine, it just looks invalid to me, but I don't know much about C#
The => notation is called a lambda expression. It is one way of creating anonymous methods. Read more about anonymous methods at http://msdn.microsoft.com/en-us/library/bb882516.aspx . What the code seems to do, on a first glance, is to print out how many subscribers there are to the event, then store a local instance of all subscribers.
Answer by oliver-jones · Jun 29, 2013 at 10:41 PM
Figured it out:
import System.Collections.Generic;
var _products = List.<StoreKitProduct>();
function Start(){
StoreKitManager.productListReceivedEvent += RecicedProductList;
StoreKitBinding.requestProductData( productIdentifiers );
}
function RecicedProductList( allProducts : List.<StoreKitProduct> ){
Debug.Log( "received total products: " + allProducts.Count );
_products = allProducts;
}
I simply missed the brackets off at the end of the _products List.
Answer by flamy · Jun 29, 2013 at 07:39 PM
function Start()
{
StoreKitManager.productListReceivedEvent += RecicedProductList;
}
function RecicedProductList( List<StoreKitProduct> allProducts/* add parameter according to the "productListReceivedEvent"'s delegate)
{
Debug.Log( "received total products: " + allProducts.Count );
_products = allProducts;
}
I havent tested this code but 99% it should work. Also i added parameter thinking that it is Prime31 plugin.. hope im right!!
I've done this:
function RecicedProductList( allProducts : List.<Store$$anonymous$$itProduct> )
{
Debug.Log( "received total products: " + allProducts.Count );
_products = allProducts;
}
I get this error: Cannot convert SystemCollectionsGenericList to System.Type?
Which is weird because both _products and allProducts are 'List.'
Was going to say remove the dot from list, but that's me thinking C#... Don't know list in javascript lol
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Animated camera problems 0 Answers
The best place to learn Javascript/C# 1 Answer
Translating js to c#, getting NULLReferenceExeption 2 Answers