Press "Enter" to skip to content

Lesson 19.3: Move data and image files out of Engine project

Currently, game data and image files are included in the Engine project. In this lesson, we’ll move them out.

They aren’t code, so it makes sense to get them out of a class library project. This will also help if we add more files. This technique always copies every file in the folders – giving us one less thing for us to remember (or forget).

We’re going to do this with a “post-build event”

Step 1: Create GameFiles folder

Create this new folder under your solution’s top-level directory

Step 2: Move files from Engine directory to GameFiles directory

Cut-and-paste GameData folder

Cut-and-paste Image folder

Step 3: Create post-build event to copy files

In the WPFUI project, right-click and select “Properties”.

On the properties screen, select the “Build Events” tab.

Copy this line into the “Post-build event command line box”. This command will run after each time we build the project/solution, copying all the files and folders in the GameFiles folder to the project’s output folder – the location where the executable version of the program is located.

xcopy “$(SolutionDir)GameFiles\*.*” “$(ProjectDir)$(OutDir)” /s /y

Step 4: Test the game

Rebuild the solution and make sure the games still runs correctly.

NEXT LESSON: Lesson 19.4: Replace BaseNotificationClass with Fody PropertyChanged Notification

PREVIOUS LESSON: Lesson 19.2: Identifying refactoring targets

13 Comments

  1. Phil Eakins
    Phil Eakins 2021-11-26

    So — MSDos is not dead!

    • SOSCSRPG
      SOSCSRPG 2021-11-26

      MS-DOS is always there, waiting for us. 🙂

  2. Julian
    Julian 2021-12-13

    Hi Scott, this change has broken my unit tests – not sure if it did for you.
    It appears to be because we have constants in our Factories using relative paths to our game data files.
    Since our executing directory for the unit tests is not the WPFUI bin location, the tests fail.
    I wasn’t sure of the best course of action to fix this. Simply changing directory in the unit tests could do it but seems hacky.

    • SOSCSRPG
      SOSCSRPG 2021-12-13

      Hi Julian,

      I think I deleted the unit test project when we added the dynamic player attributes. You could try adding an XCOPY for the unit test project, but I plan to completely redo the unit tests some time after finishing the refactoring. With the new solution structure, and removal of the circular project dependencies, it should be much easier to create unit tests.

      If you do the XCOPY, please post a comment here, for anyone else who may want to run the original unit tests.

      • Julian Smith
        Julian Smith 2021-12-14

        I can confirm using the same xcopy in the post build of the unit tests project made them work.
        Thanks

  3. Julian
    Julian 2021-12-13

    Of course including the post build xcopy in the unit test project would also probably work.

  4. Ted Bunny
    Ted Bunny 2022-02-04

    (Visual Studio 2019) Is there a way to get these files to still appear in the Solution Explorer? “Add existing Item” doesn’t mimic the folder structure, and maintaining that by manually recreating the folders and adding the objects every time one is added seems tedious.

    • SOSCSRPG
      SOSCSRPG 2022-02-04

      I don’t know a way to include those files in Solution Explorer.

      I’m not sure what requires manually recreating the folders and adding the objects” refers to. Are you talking about after each build? If so, adding the “xcopy” command to the post-build event (in step 3) should copy the game data and image files every time you build. Is that not happening, or are you referring to something else?

  5. Johan
    Johan 2022-04-30

    Hi!

    For those who get error code 4 while copying, it is due to spaces in the folder and/or filenames.

    To solve this you will need to close the destination and target with quotes, like this:

    xcopy “$(SolutionDir)GameFiles\*.*” “$(ProjectDir)$(OutDir)” /s /y

    • SOSCSRPG
      SOSCSRPG 2022-05-02

      Hi Johan,

      Thanks for sharing that. I updated the lesson to use that format.

  6. Betsy
    Betsy 2023-05-30

    Hello, I left a comment earlier today about not having the post-build command running correctly, and I think I found the issue just now, having to do with the different “types” of quotation marks

    When I copied/pasted your xcopy code, it copied with the character ” (more curly quotations) which VS was not parsing correctly. Replacing all of them with ” (straight quotations) made the post-build run correctly. You don’t have to respond to this, but maybe check the formatting on this page so others don’t have the same issue.

    Thanks for everything you do! I’ve been really enjoying going through these lessons.

    • SOSCSRPG
      SOSCSRPG 2023-05-31

      Thanks for letting me know. The source code formatting tool for this site broke a while back and I had to add a replace it. I thought I caught all the “smart quotes” and replaced them, but it looks like there are a few more. I’ll see if I can correct this lesson tomorrow.

Leave a Reply to SOSCSRPG Cancel reply

Your email address will not be published. Required fields are marked *