• 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#.

Show more comments

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