0

I have 3 AI files that have the same "decideAction(game)" function

In the main file running the game:

In C++ this was easy as I just used namespaces

namespace AI_A {
    #include "AI_A.cpp"
}
namespace AI_B {
    #include "AI_B.cpp"
}
namespace AI_C {
    #include "AI_C.cpp"
}

and in the game I did

if(turn == 0) { AI_A:decideAction(game); } etc....

Is there a way for me to do this for C? Since C doesn't have namespaces or classes?

Each of the AI files have lots of functions in themselves and a decideAction function, so I can't just #include all the AI files and rename decideAction, cos 2 different AI files may have the same function names/global variables.

Thanks!

Friedpanseller
  • 654
  • 3
  • 16
  • 31
  • 2
    Including implementation (`.cpp`) files is a very bad idea. You should never include definitions (it is a bit more complicated in C++). And namespaces should be in the header files. And C very well has name-spaces, just no user-defined ones. They are different languages. Siad that: reconsider the project structure, as-is, it looks quite messed up. – too honest for this site May 02 '17 at 01:39
  • One option is to use function pointer tables. – kaylum May 02 '17 at 01:46
  • @kaylum - I thought the signatures of all functions have to be different to use function pointer tables. In the stated question, all function signatures are the same but in different files. Would this still be possible? Thanks. – Nguai al May 02 '17 at 02:00
  • @Nguaial "I thought the signatures of all functions have to be different to use function pointer tables". Not sure where you got that thought from. – kaylum May 02 '17 at 02:19
  • @kaylum - Lets say file A.c has a function called void X(int y){}. And lets say file B.c has a function called void X(int y){}. So the function X has same signature in 2 different files. I do now know how to differentiate these 2 X functions in 2 different files using function pointer. I cannot think of a way. Is there a way? – Nguai al May 02 '17 at 02:25
  • @Nguaial Have a different function pointer for each implementation. It's a different variable so can be differentiated via the variable name. The point of a function pointer table is that the *same* function with the *same* signature can have different implementations. So I think the use case is actually exactly opposite to what you are thinking. – kaylum May 02 '17 at 02:27
  • @kaylum - Lets say using the above example, typedef int (*Z)(int). So Z is a type of functional pointer. So lets say I have Z fp1, fp2; Lets say I want to have fp1 point to X function A.c file and fp2 point to X function in B.c file. Is there a way? Or am I not understanding the problem correctly. – Nguai al May 02 '17 at 02:34
  • 1
    @Nguaial Ah, that's a different problem. Of course there are ways to do it but which way is best/appropriate depends on the OP's full requirements. One way is for each implementation to have an API that initialises or returns a function table. The main code would then call that API for each of the implementations to get its function table. This is the basic plugin model that is commonly used. – kaylum May 02 '17 at 02:38
  • @ kaylum - Thanks. Really appreciate it. – Nguai al May 02 '17 at 02:44

1 Answers1

1

You can't include files like that in C. In C "#include" means a simple file insertion and is done on preprocessor level (like everything that starts with #), so it obviously can not depend on anything runtime(like value of turn)

However, it's not a big deal because you can still use functions from all 3 files simultaneously in many ways. The way that most system work is that they add preprocessor mechanic to names to operate them. Read this: Why doesn't ANSI C have namespaces?

The quick fix I see on preprocessor level is to simply use preprocessor to trick names, which works like a simple find and replace:

#include "AI_A.cpp"
#define decideAction AI_AdecideAction
...
#include "AI_B.cpp"
#define decideAction AI_BdecideAction

Also, don't forget the headers(if any) and watch out for all conflicts, and it will work.

Community
  • 1
  • 1
iantonuk
  • 1,178
  • 8
  • 28