How do you add a button event to a button via script with parameters included Unity? - unity3d

I am trying to add an onClick event to a bunch of buttons on screen that I have Instantiated. But the function I am calling needs parameters. When I put the parameters the code doesn’t run but when I don’t it runs but the function doesn’t do what it is supposed to because it doesn’t have the right parameters. Any ideas?
public void PreviewButtonLook(Image previewPart, Sprite previewSprite)
{
previewPart.GetComponent<Image>().sprite = previewSprite;
}
button.GetComponent<Button>().onClick.AddListener(PreviewButtonLook(previewPart, options[i]);

onClick.AddListener() accepts a delegate of zero parameters.
You can create a zero-parameter anonymous function and pass it as a delegate.
This zero parameter anonymous function can call another function and pass it as many parameters as you want.
button.GetComponent<Button>().onClick.AddListener(delegate {
PreviewButtonLook(previewPart, options[i]);
});
But be aware of function closure.

Related

how do I use this OnCompleted function?

This library I found that handles music playing has the following variable public.
void Function() onCompleted;
I want to change the icon of a button when the track is finished, so that it returns to a play icon.
I tried using musicPlayer.OnCompleted(() { **stuff** }); but that gives me a syntax error Too many positional arguments: 0 expected, but 1 found.
How do I subscribe on that event, or how do I check if OnCompleted has been called?
I am still pretty new to dart but can't wrap my head around this one. I tried subscribing like in Angular or looking up if there's a different syntax for it, but I am at a loss.
Presumably you have to set onCompleted to something, specifically a function taking no parameters and returning void.
It would be normal to provide something like this in the constructor. Is there a named, optional parameter for this? Alternatively, there may be a setter.
Let's assume there's a setter. You could write:
musicPlayer.onCompleted = (){/* do stuff*/};

AnyLogic - create objects dynamically on simulation time

Is it possible to dynamically create objects or modify them on run-time ?for example,on button click,another button created or change number of lines of a road?
When I write this code for a button Action,in run-time
road123.setBackwardLanesCount(3);
I get error below:
root:
road123: Markup element is already initiated and cannot be modified.Please use constructor without arguments,perform setup and finally call initialize() .function
You'll get that error with any object you attempt to create at runtime using a parameterized constructor. If you create the object with a simple constructor (just "()") and then set all of the parameters individually, you won't run into that issue. Check the Anylogic API for specific information about the object you are using, because some require you to call .initiliaze() on that object after setting all of it's parameters if you created it using a simple constructor. Furthermore, if you want to add the object to the screen at runtime you'll need to add this code to the function that creates it:
#Override
public void onDraw( Panel panel, Graphics2D graphics) {
obj.drawModel(panel, graphics, true);
}
where obj is replaced with the name of the object you created dynamically.

WinJS.Class.define() - refer to member function in constructor

I have a WinJS class defined as follows and would like to use a member function in the constructor:
WinJS.Class.define(
function() {
setInterval(myMemberFunction, 100);
},
{ // Member variables
myMemberFunction: function() {
// Do something
}
});
Unfortunately it looks like I can't resolve member functions in that manner in the constructor. This code all lives in a module so I could move myMemberFunction up the hierarchy and access it from the constructor, however the drawback is that "this" would no longer refer to the instance of my WinJS class. What's the recommended method for accessing instance members in a WinJS from the constructor?
Generally speaking, you refer to any method or property using "this," as in this.myProperty. In the case of event handlers, you need to make sure that the "this" that you see inside the handler is the instance "this". That's the purpose of the bind method of a function object. So you do this:
setInterval(this.myMemberFunction.bind(this), 100);
This makes sure that you bind the right "this" instance to the callback. Because I've seen this question pop up frequently (use of .bind is all over the Windows SDK samples), I wrote about this in more detail on http://www.kraigbrockschmidt.com/2012/11/28/purpose-function-dot-bind/.
Just to note, this is pure JavaScript; nothing particular to WinJS or Windows Store apps.

PHPUnit mock a controller with reference parameter?

I have a class:
class Hello {
function doSomething(&$reference, $normalParameter) {
// do stuff...
}
}
Then I have a controller:
class myController {
function goNowAction() {
$hello = new Hello();
$var = new stdClass();
$var2 = new stdClass();
$bla = $hello->doSomething($var, $var2);
}
}
The "goNow" action I call using my tests like so:
$this->dispatch('/my/go-now');
I want to mock the "doSomething" method so it returns the word "GONOW!" as the result. How do I do that?
I've tried creating a mock
$mock = $this->getMock('Hello ', array('doSomething'));
And then adding the return:
$stub->expects($this->any())
->method('discoverRoute2')
->will($this->returnValue("GONOW!"));
But I'm stumped as to how to hook this up to the actual controller that I'm testing. What do I have to do to get it to actually call the mocked method?
You could create a mock for the reference, or if it is just a simple reference as your code shows, send a variable. Then the normal mock call may be called and tested.
$ReferenceVariable= 'empty';
$mock = $this->getMock('Hello ', array('doSomething'));
$stub->expects($this->any())
->method('discoverRoute2')
->will($this->returnValue("GONOW!"));
$this->assertEquals('GONOW!', $stub->doSomething($ReferenceVariable, 'TextParameter'));
Your example code does not explain your problem properly.
Your method allows two parameters, the first being passed as a reference. But you create two objects for the two parameters. Objects are ALWAYS passed as a reference, no matter what the declaration of the function says.
I would suggest not to declare a parameter to be passed as a reference unless there is a valid reason to do so. If you expect a parameter to be a certain object, add a typehint. If it must not be an object, try to avoid passing it as a reference variable (this will lead to confusing anyways, especially if you explicitly pass an object as a reference because everybody will try to figure out why you did it).
But your real question is this:
But I'm stumped as to how to hook this up to the actual controller that I'm testing. What do I have to do to get it to actually call the mocked method?
And the answer is: Don't create the object directly in the controller with new Hello. You have to pass the object that should get used into that controller. And this object is either the real thing, or the mock object in the test.
The way to achieve this is called "dependency injection" or "inversion of control". Explanaitions of what this means should be found with any search engine.
In short: Pass the object to be used into another object instead of creating it inside. You could use the constructor to accept the object as a parameter, or the method could allow for one additional parameter itself. You could also write a setter function that (optionally) gets called and replaces the usual default object with the new instance.

DelegateCommand<object> test with EventArg parameter mstest

I currently have an event trigger firing a custom trigger action.
The action passes back a EventArgs type of object to the view's view-model.
This is all well and good when I run the code it works perfectly. However, when I come to test this portion of code it all goes a bit rubbish.
As stated We are using an MVVM type pattern so I'm testing the 'Doing' end of the event trigger in my view-model and what I want to do is create a 'mocked' EventArgs object to pass into the execute method of my command under test. However it requires a RoutedEvent as it's ID property as stated above and I don't have access to it's constructor!
Cannot Access Internal Constructor for 'RoutedEvent' here.
Has anyone got any ideas? The code converage in test is more important than the current implimentation so if this is thought to be 'untestable', then I can make changes.
I have answered my own Question I think.
Casting the object passed back from the view at an earlier point means that the object I am passing to the methods under test is more easily created.
This is what I have now for the method under test.
public void DoItemsChanged(IList param)
Before I had
public void DoItemsChanged(object param)
Where the param is a SelectedItemCollection (previously a RoutedEventArgs, but now I use the IvokeCommandAction on the event trigger in the view, passign the SelectedItems). The param is now more easily passed into the method for the test and the code it much more descriptive as well. So it's all good for everyone.