How To Edit Bad Piggies Levels & Get A Larger Building Grid (Editing The Source Code)
Sep 24, 2014MobileComments (54)
This is a lengthy and technical post about editing the Bad Piggies application code in order to edit levels, enlarge the building grid, and make other tweaks to the gameplay. This covers the Windows and Android versions only, but the process will be similar for Mac and iOS if you can get access to the game application files.
Note: This guide currently only works with version 1.6.0 and earlier.
First, here is a screenshot showing what you can accomplish with these edits. This is the Fields Of Dreams level (on Android) with a larger building grid and nearly unlimited items:
Click to see the video
Following this guide could lead to breaking your game and losing your progress. Continue at your own risk. I will not provide pre-modded files; you'll have to edit your own files by following this guide.
Here is what you'll need to get started:
- A good text-editor with UTF-8 support like Notepad++
To edit the Windows version:
- The PC version of the game installed (that's it!)
To edit the Android version:
- The APK for the game, which you can get at an online APK mirror
- A basic file browser
- An archiver like 7-Zip to open the APK file
Bad Piggies is made with the Unity game engine and utilizes numerous assets files to control the game. These files usually have the extension .assets, but not always.
You can edit assets files directly using a good text editor. There are tons of different assets files and most contain what looks like a massive pile of gibberish characters. Hidden among this gibberish are little blocks of code that are human-readable, and which contain variables that affect the gameplay.
File Size
While editing assets files, they must always end up the same file size that they were originally. This is to pass a file size validity check that the game performs. Always make a backup of the assets file before editing and make a note of its Size in bytes. For example, if you change "count = 5" to "count = 150", you've added two characters, increasing the file size by two bytes, and so must remove two characters elsewhere to offset this. Addition and removal of characters must be done within the same parent GameObject.
Whitespace
The assets files are strictly-typed and whitespace matters. You must preserve tabs, newlines, and whitespace or you may run into errors. These files also use \r\n (carriage return & line feed) for every line break, which is the Windows standard. That's two characters per line break. It may seem counter-intuitive that a line break is two characters and not one. Read more about that here.
Level Editing
This guide will show you how to edit the Field Of Dreams level (seen in the screenshot above), which is called Sandbox_06 internally by the game. View the Level Number Cross Reference at the bottom of this post to find out the internal names for each of the other sandbox levels. You can use what you learn in this guide to edit any of the other levels in the game.
Base Zero Numbering
Everything uses base-zero numbering. So if you have an array containing 3 elements, the elements will be numbered 0, 1, and 2. Learn more about base-zero here.
The assets file you need to edit is in a different location on each platform.
PC/Windows
The assets file that you want to edit is called resources.assets and resides in the data folder of your Bad Piggies PC installation.
Open it in your text editor. Keep in mind that it may run slow; it has over 300,000 lines of code. This large assets file contains numerous levels. To get to the Field Of Dreams level, search for the first occurrence of "Level_Sandbox_06_data".
Android
Open up the APK with 7-Zip. You'll see this:
Go into the assets folder, then bin, then Data. In there are hundreds of files, some without extensions. The file that contains the Field Of Dreams level is called 746ee37fafe29a34ba3f2032ea011e0a (no extension). Extract just that file out and open it in your text editor.
Now that you have the correct assets file for your platform, it's time to make the revisions. Scroll down until you are looking at data that looks like this (starting with GameObject):
Step 1: Zooming Out
The first thing we need to do is adjust the default zoom level when building. If you don't do this, you won't be able to see the whole build grid once we increase it.
Go down about 33 lines and you'll find Array m_controlPoints. This controls the points that the camera pans and zooms to when you first start a level. It contains 8 elements (Element 0 through Element 7). The final element (7) is also the position of the camera during building. Its default zoom level is "Float zoom = 5.5". Change that to "Float zoom = 6.0". This will allow for more visible area for a larger build grid. It will look like this when you're done:
Note: Since we only changed "5.5" to "6.0", we didn't change the total number of characters or file size of the assets file.
Step 2: Larger Build Grid
To increase the build grid size, we need to modify the Array m_constructionGridRows. Go down about 47 lines from the zoom change you did above and you'll find it.
The Array m_constructionGridRows contains multiple Elements. Each Element corresponds to a row in the building grid. Its size (how many Elements it can contain) is specified by ArraySize size. By default it is set to 7. This means that there can be up to 7 rows in the building grid.
Now the curious thing about the Field Of Dreams level is that it only lists Element 6 (the last element) and none of the ones before it (0 through 5):
If an element is not listed it defaults to 10 boxes in the row. So that means Elements 0 through 5 (not listed) default to 10. That's 6 rows of 10 boxes each, which is correct (you can confirm this by playing the Field Of Dreams level). The final seventh element that is listed has its data set to 0, which means nothing in that row.
We want to add additional building rows as well as make each row contain more boxes, so we have to manually add in the missing Elements. This gets a little tricky but isn't too hard. Change the ArraySize to 8 (for 8 rows) and then copy and paste the Element 6 seven times until you end up with this:
Make sure you retain the same tab, space, and line breaks used in the file.
Now edit each of them to put them in sequence and change the data attribute to 8191 (not 0). Why 8191? I'll explain in a moment. Here is what you should end up with:
You now have 8 building rows, each with the value 8191, which means each row will have 13 build boxes. The reason 8191 means 13 is because the game uses the formula boxes = 2^n - 1 on the value (n) to determine the number of boxes in the row. If you just wanted 10 boxes per row (the default max), you would use 1023. Please look at this chart to get an idea why:
It might be confusing, but just know that 8191 means you'll get 13 boxes per build row. You can change it to something else, but just stick with it for now until you fully understand it.
Step 3: Unlimited Build Items
Now that you have the build grid done, you want to increase the number of items available to build with. This is done directly underneath in Array m_partTypeCounts. You'll see 43 Elements, each with an Integer count value. Increase all of these counts to something like 50 (or higher if you want). It will look like this:
Step 4: Fixing The File Size
So now that we've added a bunch of characters to the assets file, we need to remove an equal number of characters to bring the file size back to the original. This will largely be guess and check. Many of the variables in this GameObject are optional and can be deleted to reduce bytes. For example, these lines can be deleted entirely:
You can also delete these lines further down:
You can then adjust some of the float numbers to fine tune the file size. For example, you can add zeros to the end of a float (decimal) without changing what it means.
Just keep working with it until you've got the file size back to the original amount. Be careful not to delete any spaces or tabs (whitespace) in front of variables that you want to keep.
Once you're done, open up the game and try it out! If it gives you an error, you may have accidentally deleted something important, or the file size may be different. Double-check that the size is the same (not size on disk, but the actual size in bytes).
If you're on PC, you should be done now and able to open the game (you can actually edit the assets files while the game is running). If you're on Android you have a few more steps depending on your version of Android:
Android 4.4 and Earlier
Copy the edited 746ee37fafe29a34ba3f2032ea011e0a file back into the APK (using 7-Zip). And then copy the modified APK file over to the Android and overwrite your original Bad Piggies APK file. Keep a backup of the original before overwriting it and a backup of the modified APK on your PC, in case you need to reinstall.
Android 5.0 and Later (ART runtime)
Since the new runtime in Android precompiles applications, you have to install your modified base.apk (you can't just overwrite the old one). It's not too complicated. After you've copied the edited 746ee37fafe29a34ba3f2032ea011e0a file back into the APK (using 7-Zip), follow these steps:
1. Make a backup of your save files, which is the folder named files in Android/data/com.rovio.BadPiggiesHD/. Copy the files folder to a safe location (maybe keep a backup on your PC).
2. Uninstall Bad Piggies from your device, and delete the folder Android/data/com.rovio.BadPiggiesHD completely.
3. Now copy your modified base.apk file over to your device, using your File Manager, somewhere easy to access like your downloads folder.
4. Install Zip Signer from the Google Play Store and open it. Hit the "Choose In/Out..." button and navigate to the base.apk. Then hit "Sign The File". The app will take a few moments and then generate a new APK that is signed.
5. If you haven't yet, you'll need to allow installations from unknown sources. Enable the option at Settings > Security > Unknown Sources.
6. Navigate to your signed APK using your File Manager and tap it to install it. Once done, you will now have the installed Bad Piggies with your modifications. Do not use the Play store to update Bad Piggies or it may overwrite your edits.
Sandbox_01 = Find The Skulls
Sandbox_02 = Ground Hog Day 2
Sandbox_03 = When Pigs Fly 1
Sandbox_04 = Ground Hog Day 1
Sandbox_05 = When Pigs Fly 2
Sandbox_06 = Field Of Dreams
Sandbox_07 = Flight In The Night 1
Sandbox_08 = Flight In The Night 2
Sandbox_09 = Rise And Swine 1
Sandbox_10 = Rise And Swine 2
If you're on Android it can be difficult to find out which of the hundreds of files contains the sandbox level you want to edit. I recommend using the Find in Files functionality of Notepad++.
Note: This guide currently only works with version 1.6.0 and earlier.
First, here is a screenshot showing what you can accomplish with these edits. This is the Fields Of Dreams level (on Android) with a larger building grid and nearly unlimited items:
Click to see the video
Disclaimer
Following this guide could lead to breaking your game and losing your progress. Continue at your own risk. I will not provide pre-modded files; you'll have to edit your own files by following this guide.
Requirements
Here is what you'll need to get started:
- A good text-editor with UTF-8 support like Notepad++
To edit the Windows version:
- The PC version of the game installed (that's it!)
To edit the Android version:
- The APK for the game, which you can get at an online APK mirror
- A basic file browser
- An archiver like 7-Zip to open the APK file
Basic Information
Bad Piggies is made with the Unity game engine and utilizes numerous assets files to control the game. These files usually have the extension .assets, but not always.
You can edit assets files directly using a good text editor. There are tons of different assets files and most contain what looks like a massive pile of gibberish characters. Hidden among this gibberish are little blocks of code that are human-readable, and which contain variables that affect the gameplay.
File Size
While editing assets files, they must always end up the same file size that they were originally. This is to pass a file size validity check that the game performs. Always make a backup of the assets file before editing and make a note of its Size in bytes. For example, if you change "count = 5" to "count = 150", you've added two characters, increasing the file size by two bytes, and so must remove two characters elsewhere to offset this. Addition and removal of characters must be done within the same parent GameObject.
Whitespace
The assets files are strictly-typed and whitespace matters. You must preserve tabs, newlines, and whitespace or you may run into errors. These files also use \r\n (carriage return & line feed) for every line break, which is the Windows standard. That's two characters per line break. It may seem counter-intuitive that a line break is two characters and not one. Read more about that here.
Level Editing
This guide will show you how to edit the Field Of Dreams level (seen in the screenshot above), which is called Sandbox_06 internally by the game. View the Level Number Cross Reference at the bottom of this post to find out the internal names for each of the other sandbox levels. You can use what you learn in this guide to edit any of the other levels in the game.
Base Zero Numbering
Everything uses base-zero numbering. So if you have an array containing 3 elements, the elements will be numbered 0, 1, and 2. Learn more about base-zero here.
Assets File
The assets file you need to edit is in a different location on each platform.
PC/Windows
The assets file that you want to edit is called resources.assets and resides in the data folder of your Bad Piggies PC installation.
Open it in your text editor. Keep in mind that it may run slow; it has over 300,000 lines of code. This large assets file contains numerous levels. To get to the Field Of Dreams level, search for the first occurrence of "Level_Sandbox_06_data".
Android
Open up the APK with 7-Zip. You'll see this:
Go into the assets folder, then bin, then Data. In there are hundreds of files, some without extensions. The file that contains the Field Of Dreams level is called 746ee37fafe29a34ba3f2032ea011e0a (no extension). Extract just that file out and open it in your text editor.
Editing The Assets File
Now that you have the correct assets file for your platform, it's time to make the revisions. Scroll down until you are looking at data that looks like this (starting with GameObject):
Step 1: Zooming Out
The first thing we need to do is adjust the default zoom level when building. If you don't do this, you won't be able to see the whole build grid once we increase it.
Go down about 33 lines and you'll find Array m_controlPoints. This controls the points that the camera pans and zooms to when you first start a level. It contains 8 elements (Element 0 through Element 7). The final element (7) is also the position of the camera during building. Its default zoom level is "Float zoom = 5.5". Change that to "Float zoom = 6.0". This will allow for more visible area for a larger build grid. It will look like this when you're done:
Note: Since we only changed "5.5" to "6.0", we didn't change the total number of characters or file size of the assets file.
Step 2: Larger Build Grid
To increase the build grid size, we need to modify the Array m_constructionGridRows. Go down about 47 lines from the zoom change you did above and you'll find it.
The Array m_constructionGridRows contains multiple Elements. Each Element corresponds to a row in the building grid. Its size (how many Elements it can contain) is specified by ArraySize size. By default it is set to 7. This means that there can be up to 7 rows in the building grid.
Now the curious thing about the Field Of Dreams level is that it only lists Element 6 (the last element) and none of the ones before it (0 through 5):
If an element is not listed it defaults to 10 boxes in the row. So that means Elements 0 through 5 (not listed) default to 10. That's 6 rows of 10 boxes each, which is correct (you can confirm this by playing the Field Of Dreams level). The final seventh element that is listed has its data set to 0, which means nothing in that row.
We want to add additional building rows as well as make each row contain more boxes, so we have to manually add in the missing Elements. This gets a little tricky but isn't too hard. Change the ArraySize to 8 (for 8 rows) and then copy and paste the Element 6 seven times until you end up with this:
Make sure you retain the same tab, space, and line breaks used in the file.
Now edit each of them to put them in sequence and change the data attribute to 8191 (not 0). Why 8191? I'll explain in a moment. Here is what you should end up with:
You now have 8 building rows, each with the value 8191, which means each row will have 13 build boxes. The reason 8191 means 13 is because the game uses the formula boxes = 2^n - 1 on the value (n) to determine the number of boxes in the row. If you just wanted 10 boxes per row (the default max), you would use 1023. Please look at this chart to get an idea why:
0 = 2^0 - 1
1 = 2^1 - 1
3 = 2^2 - 1
7 = 2^3 - 1
15 = 2^4 - 1
31 = 2^5 - 1
63 = 2^6 - 1
127 = 2^7 - 1
255 = 2^8 - 1
511 = 2^9 - 1
1023 = 2^10 - 1
2047 = 2^11 - 1
4095 = 2^12 - 1
8191 = 2^13 - 1
16383 = 2^14 - 1
32767 = 2^15 - 1
65535 = 2^16 - 1
It might be confusing, but just know that 8191 means you'll get 13 boxes per build row. You can change it to something else, but just stick with it for now until you fully understand it.
Step 3: Unlimited Build Items
Now that you have the build grid done, you want to increase the number of items available to build with. This is done directly underneath in Array m_partTypeCounts. You'll see 43 Elements, each with an Integer count value. Increase all of these counts to something like 50 (or higher if you want). It will look like this:
Step 4: Fixing The File Size
So now that we've added a bunch of characters to the assets file, we need to remove an equal number of characters to bring the file size back to the original. This will largely be guess and check. Many of the variables in this GameObject are optional and can be deleted to reduce bytes. For example, these lines can be deleted entirely:
Float m_previewZoomOut = 8
Float m_previewMoveTime = 4
Float m_previewWaitTime = 1
Integer m_DessertsCount = 40
Boolean m_SuperBluePrintsAllowed = False
You can also delete these lines further down:
Integer m_tutorialBookPage = 65
ObjectReference m_tutorialBookPagePrefab = 4
You can then adjust some of the float numbers to fine tune the file size. For example, you can add zeros to the end of a float (decimal) without changing what it means.
Just keep working with it until you've got the file size back to the original amount. Be careful not to delete any spaces or tabs (whitespace) in front of variables that you want to keep.
Once you're done, open up the game and try it out! If it gives you an error, you may have accidentally deleted something important, or the file size may be different. Double-check that the size is the same (not size on disk, but the actual size in bytes).
Using Your Modified Version
If you're on PC, you should be done now and able to open the game (you can actually edit the assets files while the game is running). If you're on Android you have a few more steps depending on your version of Android:
Android 4.4 and Earlier
Copy the edited 746ee37fafe29a34ba3f2032ea011e0a file back into the APK (using 7-Zip). And then copy the modified APK file over to the Android and overwrite your original Bad Piggies APK file. Keep a backup of the original before overwriting it and a backup of the modified APK on your PC, in case you need to reinstall.
Android 5.0 and Later (ART runtime)
Since the new runtime in Android precompiles applications, you have to install your modified base.apk (you can't just overwrite the old one). It's not too complicated. After you've copied the edited 746ee37fafe29a34ba3f2032ea011e0a file back into the APK (using 7-Zip), follow these steps:
1. Make a backup of your save files, which is the folder named files in Android/data/com.rovio.BadPiggiesHD/. Copy the files folder to a safe location (maybe keep a backup on your PC).
2. Uninstall Bad Piggies from your device, and delete the folder Android/data/com.rovio.BadPiggiesHD completely.
3. Now copy your modified base.apk file over to your device, using your File Manager, somewhere easy to access like your downloads folder.
4. Install Zip Signer from the Google Play Store and open it. Hit the "Choose In/Out..." button and navigate to the base.apk. Then hit "Sign The File". The app will take a few moments and then generate a new APK that is signed.
5. If you haven't yet, you'll need to allow installations from unknown sources. Enable the option at Settings > Security > Unknown Sources.
6. Navigate to your signed APK using your File Manager and tap it to install it. Once done, you will now have the installed Bad Piggies with your modifications. Do not use the Play store to update Bad Piggies or it may overwrite your edits.
Sandbox Level Number Cross Reference
Sandbox_01 = Find The Skulls
Sandbox_02 = Ground Hog Day 2
Sandbox_03 = When Pigs Fly 1
Sandbox_04 = Ground Hog Day 1
Sandbox_05 = When Pigs Fly 2
Sandbox_06 = Field Of Dreams
Sandbox_07 = Flight In The Night 1
Sandbox_08 = Flight In The Night 2
Sandbox_09 = Rise And Swine 1
Sandbox_10 = Rise And Swine 2
If you're on Android it can be difficult to find out which of the hundreds of files contains the sandbox level you want to edit. I recommend using the Find in Files functionality of Notepad++.