A function that returns an array, but that also takes a function as parameter

Ericson Willians

I'm working on a personal game with SDL and I have two arrays:

float player[]={...};
float blueEnemy[]={...};

I have also a function:

float *moveOpposite(float *actor) {
    // Skipped content.
    return actor;
}

And then, I have the problematic one:

float *collideWith(float *x, float *y, float *(f)(float *a)) {
    if ((((x[0]+x[2])>y[0])&&(x[0]<(y[0]+y[2])))&&(((x[1]+x[3])>y[1])&&(x[1]<(y[1]+y[3])))) {
        *x = *f(x);
    }
    return x;
}

Ignore the collision mess. In my game, actors are arrays with xPos, yPos, width, height, speed, etc. The first four are obrigatory. Functions like "moveActor" or "moveOpposite" works arbitrarilly with all actors. collideWith is one of these functions. So, I'll explain it:

It takes the first actor and the second actor (Which are arrays with at least size 4), as parameters, and it also takes a function as a parameter. This argument-function also takes an actor as argument and returns it. Inside the collideWith function, after the test, I update the first argument with the result of the argument-function, and return the result. I would apply it like this:

*player=*collideWith(player,blueEnemy,*moveOpposite(player));

When I try to compile it, I get an horrid error:

cannot convert 'float' to 'float* (*)(float*)' for argument '3' to 'float* collideWith(float*, float*, float* (*)(float*))'|

This collision functions that takes another function as an argument is really important for my engine.

Daniel Frey

Your call needs to pass a function, hence instead of

*player=*collideWith(player,blueEnemy,*moveOpposite(player));

you might just need

*player=*collideWith(player,blueEnemy,&moveOpposite);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

A function that takes string parameter and returns integer pointer

Function that takes array of strings and returns the longest one

Defining a function that takes a std::array as a parameter

Write a valueStock function that takes an array of products as a parameter

How do I pass a function as a parameter to a function in C, if the passed function also takes functions as parameters?

A function that takes a predicate as a parameter

list object not callable calling a function whose parameter is also a function with an array

a function that takes a list of integers as a parameter and returns a list of running totals

It is possible to define a function in haskell that takes one parameter, ignores it, and returns itself?

Why is arrow function sent as a parameter when it simply returns the value it takes as is?

VBA function that takes a dynamic array and returns a dynamic array

SwiftUi create function with array as parameter and also array type of element

Can you create a function that takes another function and a parameter and returns a lazy stream of nested function calls?

Passing a function which returns array as a parameter

Function that takes an array of chars and returns a concatenated string. Haskell

React js error in a simple function that takes an array and returns a random value

Generic function which takes parameter an array of any object

function that takes iterables and returns a string

function that takes an array and arguments separated by commas and returns a function that returns any number

Create a function that takes a UIViewController as a parameter

Function that takes templated length parameter

Function that takes a function and list and returns Boolean

When passing a mutable array reference into a function that also takes a mutable array reference, why is &mut not stated?

Function that returns the index of the first parameter in the array passed as the second parameter

Kotlin: Unit testing a function that takes a function as parameter

Function which takes variadic function as parameter

MissingMethodException when testing a function that takes a function parameter

Custom R function that takes a function as parameter

A function that collects an array of numbers as a parameter and returns an array of percentages?