Author Topic: Button Held Release Call  (Read 5289 times)

beforethelight

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Button Held Release Call
« on: December 18, 2012, 05:45:52 PM »
Ok so I am using NGUI Buttons to emulate a controller on a Android device.  I have a script that does not send release calls every time a button is released.  The script is layed out below.  Thank you in advance.

  1. #pragma strict
  2.  
  3. var target : GameObject;
  4. var call : String;
  5. var release : String;
  6. var go : boolean;
  7. var releaseCall : boolean = true;
  8.  
  9. function OnPress (isPressed : boolean) {
  10.         if(enabled){
  11.                 go = isPressed;
  12.                 if(!isPressed && releaseCall){
  13.                         OnRelease();
  14.                 }
  15.         }
  16. }
  17.  
  18. function OnRelease(){
  19.         if(target == null){return;}
  20.         target.SendMessage(release, gameObject, SendMessageOptions.DontRequireReceiver);
  21. }
  22.  
  23. function FixedUpdate(){
  24.         if(go){
  25.                 Send();
  26.         }
  27. }
  28.  
  29. function Send(){
  30.         if(target == null){return;}
  31.         target.SendMessage(call, gameObject, SendMessageOptions.DontRequireReceiver);
  32. }
« Last Edit: December 18, 2012, 10:14:02 PM by beforethelight »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Button Held Release Call
« Reply #1 on: December 18, 2012, 10:58:06 PM »
I am not sure if there is a question in there, or what.

I don't recommend using SendMessage -- especially every FixedUpdate. It's a very slow operation.

beforethelight

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Button Held Release Call
« Reply #2 on: December 18, 2012, 11:53:09 PM »
The main question is why does it not call OnRelease every time I release the button?  As a second question now what would you recommend I use besides SendMessage?  This script is attached to the button and my ship has a thrust script that send message calls.  It uses forces on a rigidbody and thats why I have it in fixed update.  Thank you for the help.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Button Held Release Call
« Reply #3 on: December 19, 2012, 03:52:53 AM »
OnPress is sent out twice. Once with a 'true' parameter when it's pressed, and once with 'false' when it's released. If that's not what you are seeing, look closer at your code, such as your "releaseCall" flag, and whether "Collapse" is on in the Console window.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Button Held Release Call
« Reply #4 on: December 19, 2012, 03:53:44 AM »
Instead of using SendMessage, reference the object directly -- whether by retrieving it via GetComponent<> or having a public reference that you set on your script in inspector via drag & drop.

beforethelight

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Button Held Release Call
« Reply #5 on: December 19, 2012, 05:34:04 AM »
Thank You very much for your help!!  I am working on making this change but here is the issue I am having.  I want to write 1 script that is reuseable for each button you may encounter in the game, that is why I went with the SendMessage way at first.  But now that I know how bad that is I am trying to do the same thing with variables and cannot get it to work.  Is there a way to call a function that is a variable.  Like above I just typed in the function into the variable call and boom when held I got thrust from a sendmessage.  I can't figure out how to find the a variable type of script, and call variable functions from it.  Is this possible?  If not I will write scripts for each variation of button but really would like to avoid that.  Sorry if I am stupid I am trying to find the answers on my own also.  Thanks again.
  1. var variableScript : Component;
  2. var variableHeldFunction : Function;  <-- This does not show up in the inspector so I cannot define the value.  
  3. var variableScriptName : String;
  4. var variableCall : String;
  5.  
  6. function Start(){
  7.      variableScript = target.GetComponent(variableScriptName);<--  Here it tells me that I cannot convert Script type to component.
  8.      variableHeldFunction = variableCall; <-- Returns errors for string cannot convert string to function.
  9. }
  10.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Button Held Release Call
« Reply #6 on: December 19, 2012, 09:33:14 AM »
var variableScript : Component; <-- this is your problem. Instead of "Component", specify your actual script. For example if your script is HelloWorld, specify that.

There is no point in doing this after:

variableScript = target.GetComponent(variableScriptName);

That said, you code in javascript. I don't know javascript, so I can't help you further.

beforethelight

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Button Held Release Call
« Reply #7 on: December 19, 2012, 03:06:26 PM »
Thank You again for the great product and support.  I understand you are not a instructor for unity however you have been a great help to me even on things not NGUI.