• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by BetaWar · Feb 29, 2012 at 05:01 PM · javascriptcastingwarnings

Implicit downcast from Object to boolean

I am running into the warning stating "Implicit downcard from 'Object' to 'boolean'." And it has been annoying me for quite some time now. I have been searching around and haven't come up with anything that works well except using the #pragma downcast compiler directive, which isn't ideal (far better to get rid of the warning than simply say it is okay to have them).

I did find another post (somewhere, can't actually find it again) on one of the communities that said you should be able to use the build-in System types for all primitives when casting between object type. However, I am finding that it doesn't work when you do that.

Here is a simplified example of what I am trying to accomplish (with an explanation below):

var parm:uint = 42;
var captured:System.Boolean = false;
captured = callback(parm) as System.Boolean;
if(captured){
    return true;
}
//...

Basically, I have a callback function passed into this function (top and bottom not displayed - but the gist of it is there) and this function calls the callback, which returns a boolean (well, I am currently trying to return System.Boolean from the callback function) and based on the return value of the callback, I want the current function to do different things (in this case return true off the bat).

Is there any way to cast a Object to a boolean without using #pragma downcast or getting a warning?

Thank you for your time.

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · Feb 29, 2012 at 06:14 PM 0
Share

Does UnityScript support nullable types? Why are you coding it this way?

avatar image BetaWar · Feb 29, 2012 at 06:25 PM 0
Share

No, UnityScript doesn't allow nullable types. However, they do allow Objects to be set to null, they also allow a default type (if you don't specify the variable type and don't set it on declaration -- however, this is pretty bad practice since it could store any value/ object then).

Quick example: var captured:Object = new Object();
captured = null;
if(eventCaptured === null){
   Debug.LogWarning("captured is null");
}

I am coding it like this bacause I want to allow an abstract interface where a number of object can call the same function and simply pass in a callback such that I can reduce code rewriting to a $$anonymous$$imum and still get the full desired functionality.

avatar image Jessy · Feb 29, 2012 at 06:53 PM 0
Share

Sounds like a pain. I'd love to see an example of this kind of thing in action.

avatar image BetaWar · Feb 29, 2012 at 07:35 PM 0
Share

Well, this isn't in action currently, but this is a possible usage for what I am trying to do:

function gtComp(a:int, b:int):boolean{ return a > b; } function ltComp(a:int, b:int):boolean{ return a < b; } var iters:uint = 0; function swComp(a:int, b:int):boolean{ if(iters++ % 2){ return gtComp(a, b); } return ltComp(a, b); }

function selSort(arr:Array, compare:Function):Array{ var $$anonymous$$Pos:uint; var length:uint = arr.Count; var tmp; for(var i:uint = 0; i < length; i++){ $$anonymous$$Pos = i; for(var j:uint = i + 1; j < length; j++){ if(compare(arr[j], arr[$$anonymous$$Pos])){ $$anonymous$$Pos = j; } } if($$anonymous$$Pos != i){ tmp = arr[$$anonymous$$Pos]; arr[$$anonymous$$Pos] = arr[i]; arr[i] = tmp; } } return arr; }

Basically, it lets you sort an array by using different comparison methods. You don't have to always sort ascending or descending. Plus you can quickly come up with your own comparison algorithm (or a comparison function for a different type) and use it without problem.

avatar image Jessy · Feb 29, 2012 at 08:17 PM 0
Share

I don't understand why you're not using List.Sort. From what I can tell here, you're trying to treat UnityScript as if it were JavaScript, when you really ought to be using C#.

avatar image BetaWar · Feb 29, 2012 at 10:05 PM 0
Share

The above code was just an example. It is not actually what I am wanting to do with the final program. Far from it. However, it does give an example of how this would be useful.

See, the problem with using C# is that (to my knowledge) you can't deploy to i* devices then. Since that is likely going to be my main market for the game I am working on it simply isn't aceptable. Furthermore, I have programmed in Javascript for over 7 years (and Actionscript for close to 5) so I understand it, it makes sense, and I don't have to learn a ton of additional things for a product that may never take off (and as such not be worth spending more time than necessary on).

So far, I must say that it looks like this community is more about defending design decisions than actually helping people who are attempting to do something... Disappointing really.

avatar image Jessy · Feb 29, 2012 at 10:27 PM 0
Share

Honestly, it seems like you're attempting to do something that is garbage, so I see no reason the community should support that. You cannot program in the JavaScript you know, or ActionScript, in Unity. I don't know where you heard that you can't use C# on "i*" devices (I'm assu$$anonymous$$g you mean iOS?), but that has never been the case, with Unity.

avatar image BetaWar · Feb 29, 2012 at 11:01 PM 0
Share

So, you are saying that there is absolutely no reason for a callback function? Or are you clai$$anonymous$$g that there is no reason a callback function should need to return anything?

I don't know about anyone else, but error codes could be a very useful thing to get back from a function, and if we can't cast between an "Object" and, say, an integer then there could be some problems (such as allocating excessive memory, or having to hack the program together to ignore certain warnings, etc.).

Apparently my information was wrong on the C# thing, but that doesn't change that UnityScript is basically ACtionscript 3 with some quirks (regardless of what you claim). Furthermore, if you truly want people to not compare UnityScript and Javascript I would strongly suggest you do away with the .js file extension. People may get the wrong idea there.

avatar image Jessy · Mar 01, 2012 at 12:39 AM 0
Share

I'm not knocking callback functions, but I am suggesting there are much easier ways to do what you're doing, that will result in less code. Particularly, it sounds like you're not familiar with generics, which are a big part of C#. UnityScript supports them to some degree; unfortunately, it only implements a subset of what C# does. I don't see a point to UnityScipt, because of that, and how similar its syntax is to C#. Please don't get the wrong impression; I do not work for Unity Technologies, and agree with you on the extension. I assume it's a marketing thing, to attract people like yourself – the web player was the main attraction for Unity licensees in the early days.

I suggest that you start some threads on the Unity forum, in order to get some hints to transition to working with $$anonymous$$ono. I think it would be beneficial to you, if you offered some code that you want to get working, and then have people show you some elegant ways can get there. As with anything else, if you fight the Unity/$$anonymous$$ono paradigms, you're going to make life hard on yourself. I understand the wanting to get things done quickly, but that can easily lead to hurdles like this, where you don't get the help you want, because of how alien your approach is. I'd be willing to offer what I can, if you post forum links here. I think you may find the community more welco$$anonymous$$g if you give off the impression that you care about the topics people can $$anonymous$$ch you about; that's just human nature.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by greay · Dec 01, 2012 at 10:52 PM

this has been bothering me for a couple weeks as well. finally found the answer here.

 // instead of this
 var callback:Function;
 
 // declare them like this:
 var callback:function(uint):boolean;

Yes, in the second one "function" must be lowercase.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

type not found 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Javascript array, use with a struct ? 3 Answers

Directly accessing enum types from other scripts (javascript) 0 Answers

Fonts not displaying properly when loaded through script 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges