Passing parameter to schedule_selecter - cocos2d-x

Hey guys i am using schedule_selecter in cocos2dx project, where i am having 7 different int values. I am generating random value among these 7 values. Now i want to pass this random value to another function after some interval. I am using schedule_selecter to make call after some interval but it does not allow me to pass value. how can i do this???
ex.
int random_val = arc4random() % 7;
...
function_selected_value(int random_val)
{
....
}
i want to pass random_val to function_selected_value using schedule_selecter
thnx in advance ..

schedule_selecter does not allow you to pass any data with functor. Instead you can assign value to any member variable and access there in callback function.
// scheduling a function with interval of 1 seconds.
float interval = 1.0f; // interval to call scheduler function.
CCNode::schedule(schedule_selector(MyClass::scheduleFunction), interval);
void MyClass::scheduleFunction(float dt) {
// use your random number related code here
}

Related

How can I display multiple return values of a recursive function efficiently?

I will be using JavaScript to demonstrate my example, but the question remains open to other programming languages as well.
Let's consider a simple recursive function:
function f(x){
if(x>1) return f(x-1)+5;
else return 3;
}
Let's say I want to display the value of the function for all x from 1 to 5, where x is an integer. The most obvious solution would be the following:
for(i=1; i<=5; i++){
console.log(f(i));
}
This method, however, seems incredibly inefficient, as we are calling the function 5+4+3+2+1 times. Intuitively, only 5 function calls would be necessary, as whenever we want the value of f(5), it is necessary to find the value of f(4) f(3) f(2) and f(1) first, so after finding f(5), we wouldn't need to call f(4) and start the entire process all over again to just to find the value of f(4) which we already found once.
Any ideas?

Go Tour #18: understanding pic.Show

I did not understand how the function call pic.Show(Pic) works and what it does.
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
pic := make([][]uint8, dy)
for y := range pic {
pic[y] = make([]uint8, dx)
for x := range pic[y] {
pic[y][x] = uint8(5 * (x + y))
}
}
return pic
}
func main() {
//pic.Show(Pic(40,30)) // doesn't work, but why?
pic.Show(Pic) // works, but why? Where are the values for dx and dy set?
}
There is, starting at line 5, a function, named Pic, and it receives two integer variables (dx, dy). So I think, a correct function call might be Pic(40,30) (with 40 and 30 being the values for dx and dy).
But why does line 17 throw an error? (pic.Show(Pic(40,30)))
Why does line line 18 work? (pic.Show(Pic))
And where do the values of dx and dy come from when line 18 is executed?
I tried to look up http://golang.org/x/tour/pic which redirects me to https://godoc.org/golang.org/x/tour/pic. There I can read, that the function Show is defined this way:
func Show(f func(int, int) [][]uint8)
Which I understand as:
Show is a function that needs one parameter. This parameter is a function that needs two parameters, both of type int, and it has to return a value of type [][]uint8 (a slice of slices of unsigned 8-bit integers). Show itself doesn't return anything.
So, here again, I read that the inner function (the parameter of Show) needs two parameters. But why do I get an error, when I try to provide those parameters? Why is it ok to call the function Pic without parameters? And where do the values for those parameters come from, when Pic is executed?
When you say Pic(40, 30) you call the function Pic and it returns a [][]uint8 (as seen in the function definition). This means that in your commented-out code you pass a [][]uint8 to Show.
When you say Show(Pic) you pass Pic as the parameter, which is a function. That is what Show expects. Pic is of type func(dx, dy int) [][]uint8.
Go allows you to pass functions around as parameters and that is what is happening here.
You are quite right about definition of Show - it is a function which accepts another specific format notion as a parameter.
Pic is such a function matching this criteria - so you pass it to Show successfully.
But when you call Pic(30,40) that means not a function but a result of calling the function with such parameters. So in this case you passs to Show not a function Pic but a slice returned by it [][]uint8. Of course Show can’t accept it.
Your function, Pic, takes two parameters, Show takes one. You're calling Show with a single parameter, which is a function; that function takes two parameters. When you pass a function to another function, the assumption is that the function you're calling (Show) will call the function you passed in (Pic) and provide the necessary parameters when it makes that call.

AS3: Perform action in intervals based on incrementing int

So as not to complicate my question, I won't involve the context, but basically let's say I have a variable:
var foo:int;
And 'foo' is constantly incrementing, how would I be able to perform a function every 300 increments (300,600,900, etc) of 'foo'?
Cheers
EDIT: Also worth mentioning, the number can occasionally skip numbers as it is a rounded version of a decimal number that is incrementing
What about making it a private variable and only acces it using these accessor methods:
function getFoo():int {
return foo;
}
function setFoo(newFoo:int):void {
if (newFoo % 300 > foo % 300) performAction();
foo = newFoo;
}
You could also add convenience methods like incrementFoo(increment:int) of course.
Sorry for possible syntax errors. I haven't used AS3 for quite a while.

What's the difference between call by reference and copy/restore

What's the difference in the outcome between call by reference and copy/restore?
Background: I'm currently studying distributed systems. Concerning the passing of reference parameters for remote procedure calls, the book states that: "the call by reference has been replaced by copy/restore. Although this is not always identical, it is good enough". I understand how call by reference and copy/restore work in principle, but I fail to see where a difference in the result may be?
Examples taken from here.
Main code:
#include <stdio.h>
int a;
int main() {
a = 3;
f( 4, &a );
printf("%d\n", a);
return 0;
}
Call by Value:
f(int x, int &y){
// x will be 3 as passed argument
x += a;
// now a is added to x so x will be 6
// but now nothing is done with x anymore
a += 2*y;
// a is still 3 so the result is 11
}
Value is passed in and has no effect on the value of the variable passed in.
Call by Reference:
f(int x, int &y){
// x will be 3 as passed argument
x += a;
// now a is added to x so x will be 6
// but because & is used x is the same as a
// meaning if you change x it will change a
a += 2*y;
// a is now 6 so the result is 14
}
Reference is passed in. Effectively the variable in the function is the same as the one outside.
Call with Copy/Restore:
int a;
void unsafe(int x) {
x= 2; //a is still 1
a= 0; //a is now 0
}//function ends so the value of x is now stored in a -> value of a is now 2
int main() {
a= 1;
unsafe(a); //when this ends the value of a will be 2
printf("%d\n", a); //prints 2
}
Value is passed in and has no effect on the value of the variable passed in UNTIL the end of the function, at which point the FINAL value of the function variable is stored in the passed in variable.
The basic difference between call by reference and copy/restore then is that changes made to the function variable will not show up in the passed in variable until after the end of the function while call by reference changes will be seen immediately.
Call by Copy/Restore is a special case of call-by-reference where the provided reference is unique to the caller. The final result on the referenced values will not be saved until the end of the function.
This type of calling is useful when a method in RPC called by reference. The actual data is sent to the server side and the final result will send to the client. This will reduce the traffic, since the server will not update the reference each time.
Call By Reference:
In call-by-reference, we pass a pointer to the called function. Any changes that happens to the data pointed by that pointer will be reflected immediately.
Suppose if there are numerous changes to be made to that data, while it wouldn’t incur much cost locally, it’ll be expensive in terms of network cost as for each change data will have to be copied back to the client.
C Code:
void addTwo(int *arr, int n){
for(int i=0;i<n;i++){
arr[i]+=2; //change is happening in the original data as well
}
}
int main(){
int arr[100]={1,2,3,...}; // assuming it to be initialised
addTwo(arr,100);
}
Call By Copy/Restore:
In call-by-copy/restore, the idea is that when the function is called with the reference to the data, only the final result of the changes made to the data is copied back to the original data(when the function is about to return) without making any changes to the original data during the function call, requiring only one transfer back to the client.
In the C code below, the data pointed by arr is copied in the function and stored back to arr after all the changes to the local data are finalised.
C Code:
void addTwo(int *arr, int n){
// copy data locally
larr = (int*)malloc(n*sizeof(int));
for(int i=0;i<n;i++){
larr[i]=arr[i];
}
for(int i=0;i<n;i++){
// change is happening to the local variable larr
larr[i]+=2;
}
//copy all the changes made to the local variable back to the original data
for(int i=0;i<n;i++){
arr[i]=larr[i];
}
}
int main(){
int arr[100]={1,2,3,...}; // assuming it to be initialised
addTwo(arr,100);
}
Note: Code shown above doesn’t represent actual RPC implementation, just an illustration of the concepts. In real RPC, complete data is passed in the message instead of pointers(addresses).

Function.length and variable (...rest) argument length [AS3]

Is there any way to determine if a (anonymous) function has defined the ...(rest) parameter in ActionScript 3? I know there's the function.length property, but it only counts the explicitly defined arguments.
If you mean checking to see if the function has defined parameters when it was called, then the only way to really do this (to my knowledge) is to prototype the function class and then inside your newly prototyped function class, capture the args array parameter and check to see if it's null. I'm not going to figure out and write all that code for you (lack of time) but here is a nice article that should thoroughly describe this process and have you well on your way.
http://tobyho.com/Modifying_Core_Types_in_ActionScript_3_Using_the_Prototype_Object
There is a way to determine if a function has defined a ...rest parameter, but you can only determine this within the function's body. Outside the function's body, the function has 0 parameters as shown by the first trace output. In reality, this ...rest parameter is an array that only has scope inside the body of the function. However, once you're inside the function body you can test for it, as shown by the second and third trace outputs.
public function Test()
{
trace(doSomething.length);
doSomething(7, 8, 9, 10);
}
public function doSomething(...numbers):void
{
if (numbers.length > 0) trace("Found the parameters!");
for (var i:int = 0; i < numbers.length; i++) trace(numbers[i]);
}
Output: 0
Output: Found the parameters!
Output: 7 8 9 10