- Home /
UnityScript, Delegates and SmoothMoves
Still learning code on my own here and I'm trying to understand delegates and how they would work in SmoothMoves.
edit Just to expand on this because this is the issue I'm having;
I'm trying to work with SmoothMoves while using UnityScript (JS). To do so, I need to work with delegates.
this is the code have, while it doesn't cough out any errors, it doesn't do what I 'think' it should.
#pragma strict
import SmoothMoves;
var hitPoints : int = 20;
var cost : int = 100;
var tilePartner : GameObject;
var tomatoSlot : GameObject;
var placementAudio : AudioClip;
var myAnimation : SmoothMoves.BoneAnimation;
private var projectileStrength : int = 0;
private var soundRate : float = 0.0;
private var soundDelay : float = 0.0;
var isAttacking : boolean = false;
var isSpawning : boolean = true;
var isIdle : boolean = false;
function Start ()
{
PlaySound ( placementAudio, 0 );
}
function Update ()
{
if ( isSpawning )
{
Spawn ();
}
if ( hitPoints <= 0 )
{
tilePartner.tag = "placementPlane_Open";
Destroy ( gameObject );
}
if (isIdle && !isSpawning && !isAttacking)
{
animation.Play("Idle");
}
if (isAttacking && !isSpawning)
{
myAnimation.RegisterUserTriggerDelegate(Throw);
}
}
function Spawn ()
{
animation.Play("Spawn");
yield WaitForSeconds (.8);
isSpawning = false;
isIdle = true;
}
function OnTriggerEnter ( other : Collider )
{
if (other.gameObject.tag == "alienAttack")
{
projectileStrength = other.GetComponent(alienAttack).strength;
hitPoints -= projectileStrength;
projectileStrength = 0;
}
}
function PlaySound (soundName : AudioClip, soundDelay : float) // calls to play sound based on sound file name attached to object
{
if ( !audio.isPlaying && Time.time > soundRate ) // sets sound rate
{
soundRate = Time.time + soundDelay; // option to add a delay to the sound before it starts playing
audio.clip = soundName;
audio.Play ();
yield WaitForSeconds ( audio.clip.length );
}
}
function Throw(userTriggerEvent : SmoothMoves.UserTriggerEvent)
{
animation.Play("Throw");
if (userTriggerEvent.frame == 13)
{
print ("throw");
}
}
My issue is this part right here in the function update;
if (isAttacking && !isSpawning)
{
myAnimation.RegisterUserTriggerDelegate(Throw);
}
The code will enter this section just fine, however...it's not calling the function "Throw" which I was assuming it should. It doesn't enter the function Throw at all. It just does nothing.
Being new to delegates, I'm unsure what I'm suppose to do to properly call the Throw function?
Answer by raimon.massanet · Nov 27, 2013 at 02:51 PM
I have never used SmoothMoves, so I can't tell you how it works. However, I can tell you that when you register a delegate, you don't trigger a function, you just setup a function which will be called when an event is triggered.
So, if you write:
myAnimation.RegisterUserTriggerDelegate(Throw);
you should not expect your function to be called there. Instead, you are setting up your function Throw
to be called when a SmoothMoves.UserTriggerEvent
happens. When will that event be triggered? That depends on the backend, so you should search for any documentation that describes when that particular event is triggered. Since your function is not being called, I would say that event is not being triggered at all.
In addition, you usually only need to register a delegate once. That means that every time that event happens, your function will be called. I see you are registering your delegate inside the Update
function, and since you are never de-registering it, I don't think it is necessary.
Holy shit, THAN$$anonymous$$ YOU!!!
I completely understand now, thank you so much for this. It just made my lightbulb go off and gave me clarity!
Thank you, thank you, thank you!
$$anonymous$$ade 2 line changes and she now works like a charm!
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
JS Delegates 1 Answer
c# delegates/events using from js? 1 Answer