- Home /
Callback help...cuz it's hard
I'm trying to integrate with an ad service. They provide a callback class I can use to determine when a video has ended/completed/etc. However the class is not monobehavior, instead it is AndroidJavaProxy. This causes me problems because I can't access any of my scripts from here. I'm not sure how to receive a value back letting me know what the callback script has actually done.
The basics are like so:
In my class I set up the call back like this:
SomeAdService.setVideoCallbacks(newSomeAdServiceVideoCallbacks());
SomeAdService.Show(SomeAdService.Video)
//////////////////////////////////////////////////
//SomeAdServiceVideoCallbacks script below
///////////////////////////////////
using UnityEngine;
using System;
public class SomeAdServiceVideoCallbacks : AndroidJavaProxy
{
public SomeAdServiceVideoCallbacks() : base("com.SomeAdService.ads.VideoCallbacks") { }
void onVideoFinished()
{
//stuff happens here but now do I send a value back to my calling script
}
}
I have 0 experience with delegates, callbacks, lamdas, kung fu, etc and looking over the interwebs has just left me more confused. How can I get my class to read the results of the callback?
Appreciate any help
Thanks
Answer by Nerull22 · May 26, 2015 at 03:16 AM
So I'm gonna' try and answer your question as I understand it. I think where you're falling short is your understanding of delegates and events. I don't know what ad service your using or what exactly you're looking for, so I'll try and explain delegates and events to help you understand, and hopefully you can fix the problem yourself.
So a delegate is required in order for an event to trigger. A delegate is a template for an event. So an event is just like it sounds, it's when something else happens. A delegate tells it what should be returned and what parameters the calling functions need. This will make more sense in a minute.
A delegate in C# is written:
public delegate int ExampleDelegate(int x, int y);
Alright, so we classify it as a delegate. The next is the int, this is what any function should return. And then it ends with the two ints in the paranthesis, those are parameters that functions should take. So like an interface defines what methods an inheriting class should have, a delegate defines the structure of functions given should have. So now we'll move to events and show how delegates come in.
An event in C# will be written as:
public event ExampleDelegate Changed;
So we do as we did with the delegate and instead we define the variable as an event. Then the identifier is the delegate that we defined earlier. And lastly is the name of the event that we just created. So what's cool about this, is now we can add as many methods as we want to listen to this event. And then whenever the event is triggered, all of those methods will be called, without the necessity of us having to call them one at a time. An example will probably help a lot.
private void Awake()
{
Changed += MethodOne;
Changed += MethodThree;
}
private int MethodOne(int x, int y)
{
// Method one will be called
}
private int MethodTwo(int x, int y)
{
// This won't be called
}
private int MethodThree(int x, int y)
{
// Method Three will be called
}
So as you can see above, we add method one and method three to watch that event. So whenever the method shown below gets invoked method one and method three code will run, but method two won't.
Changed.Invoke();
So that ad service is waiting for a response back from the video or their servers or something, and then after it receives that information, it's triggering this event to occur. Or in your case, you're passing a method that you've defined in as a delegate to their function, so they know what function to call when that event occurs.
I highly recommend going over a lot of tutorials of delegates and events until you really understand them, as they're very powerful and can lead to many many great coding in the future. Lambdas are a little harder to understand, but super powerful when you combine them with Linq queries. I hope this helps.
Yes, this helps very much. Using this I can start to see how I can pull back some info - still got a ways to go but this definitely helps.
Answer by HTwist · May 26, 2015 at 03:34 AM
SomeAdService.setVideoCallbacks(new SomeAdServiceVideoCallbacks(() => println("ad finished")));
SomeAdService.Show(SomeAdService.Video)
//////////////////////////////////////////////////
//SomeAdServiceVideoCallbacks script below
///////////////////////////////////
using UnityEngine;
using System;
public class SomeAdServiceVideoCallbacks : AndroidJavaProxy
{
private Action onFinished;
public SomeAdServiceVideoCallbacks(Action callback) : base("com.SomeAdService.ads.VideoCallbacks")
{
this.onFinished = callback;
}
void onVideoFinished()
{
this.onFinished();
//stuff happens here but now do I send a value back to my calling script
}
}
I haven't tested it but basically the constructor takes an action. An action is a reference to a method (void) which which we will call when onVideoFinished is executed.
So at the moment you instantiate the class, either pass a method or directly write a lamdba which represent a method eith no parameters "()" and return no results (void) but only execute side effects which here is to print a text saying the video is done.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200301im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
OnAdRewarded called twice 0 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer