delphiuses-clause

Including client files for shared file


Okay, so I have 2 projects for a game. One is the server and one is the client. I keep the shared units into a shared folder that I use to include in my client/server project. There is a problem however: I have a shared file that needs a different file for client / server. Example: mySharedLib needs to print to the console, however the client/server console is different. What are my options? Thanks


Solution

  • In your shared file you could use define compiler directive

    For example

    {$IFDEF MYSERVER}
        Writeln('Server');  // this code executes
    {$ELSE}
        Writeln('Client');  // this code does not execute
    {$ENDIF}
    

    Then in your server project define a MYSERVER define and in your client define a MYCLIENT one, then when the shared code seperates use an {$IFDEF) statement.

    From the Delphi help on conditional definitions:

    The conditional directives $IFDEF, $IFNDEF, $IF, $ELSEIF, $ELSE, $ENDIF, and $IFEND allow you to compile or suppress code based on the status of a conditional symbol. $IF and $ELSEIF allow you to base conditional compilation on declared Delphi identifiers. $IFOPT compiles or suppresses code depending on whether a specified compiler switch is enabled.

    This will not however work if the shared code is in a DLL or any other sort of complied shared resource such as a package.

    From the Delphi help on conditional definitions:

    Conditional definitions are evaluated only when source code is recompiled. If you change a conditional symbol's status and then rebuild a project, source code in unchanged units may not be recompiled. Use Project|Build All Projects to ensure everything in your project reflects the current status of conditional symbols.