# Making a Terraria like game, need help with an error

**URL:** <https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275>\
**Category:** Help\
**Tags:** javascript\
**Created:** [April 18, 2023, 11:32pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275 "2023-04-18T23:32:59Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samsano](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/samsano/32/8307_2.png) [@Samsano](https://forum.makecode.com/u/Samsano)\
**Post date:** [April 18, 2023, 11:32pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/1 "2023-04-18T23:32:59Z")

</div>

The code is stolen borrowed from @yoxd707. I put all the files into one file and now i get this error: can’t read properties of null (reading ‘fields’). I know it is because it tries to read something that doesen’t exist but my puny brain can’t handle JavaScript yet, let alone TypeScript.

Here is the code:

> **[Terraria like Game](https://arcade.makecode.com/S11006-42062-17640-45579)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

in: terrainGeneration.ts

---

<div class="post-metadata">

**Author:** ![labfox29](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/labfox29/32/9252_2.png) [@labfox29](https://forum.makecode.com/u/labfox29)\
**Post date:** [April 19, 2023, 9:44pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/2 "2023-04-19T21:44:23Z")

</div>

I dont see no code

---

<div class="post-metadata">

**Author:** ![yoxd707](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yoxd707/32/8506_2.png) [@yoxd707](https://forum.makecode.com/u/yoxd707)\
**Post date:** [April 19, 2023, 10:03pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/3 "2023-04-19T22:03:53Z")

</div>

And you had no better idea than to steal borrow an older version that had a worse performance, since it used sprites instead of tiles 🤣

Later I will check what you did and maybe I can help you with the error…

---

<div class="post-metadata">

**Author:** ![yoxd707](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yoxd707/32/8506_2.png) [@yoxd707](https://forum.makecode.com/u/yoxd707)\
**Post date:** [April 19, 2023, 10:03pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/4 "2023-04-19T22:03:55Z")

</div>

The file you made is fine but you missed calling the main function, you should do it before the game.onUpdateInterval and game.onUpdate events.

![imagen](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/4/48fd885c9cda85151e5b2193dcf2ab1d6085080f.png)

But the error actually comes from you didn’t create the grass, dirt and stone images in assets, they should be 8x8 px.

I tried to do it on a clone of your project but there seems to be a bug and it doesn’t work. So I recommend you to make a new one, first create the images in the asset and then add the file with the programming.

Good luck, keep me posted if any other issues arise 😄

---

<div class="post-metadata">

**Author:** ![pigpigeons\_r\_c00l](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/pigpigeons_r_c00l/32/12313_2.png) [@pigpigeons\_r\_c00l](https://forum.makecode.com/u/pigpigeons_r_c00l)\
**Post date:** [April 20, 2023, 9:27pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/5 "2023-04-20T21:27:08Z")

</div>

neither do i

---

<div class="post-metadata">

**Author:** ![Samsano](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/samsano/32/8307_2.png) [@Samsano](https://forum.makecode.com/u/Samsano)\
**Post date:** [April 20, 2023, 10:01pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/6 "2023-04-20T22:01:56Z")

</div>

Yea, i noticed that i used wrong when i edited your one to make a sprite with gravity and it phased through the ground…

Have been trying your newer codes.

And i am geniuenly impressed you can make procedural generation, in which i do not understand anytyhing 😅

---

<div class="post-metadata">

**Author:** ![yoxd707](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yoxd707/32/8506_2.png) [@yoxd707](https://forum.makecode.com/u/yoxd707)\
**Post date:** [April 21, 2023, 4:38am UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/7 "2023-04-21T04:38:00Z")

</div>

I’m going to try to make it more “accessible”, although I have so much work that it can take time 😵‍💫  
But it would be a good hobby to try to improve the project.  
Try using something newer, though it will be much more difficult to understand 😅

---

<div class="post-metadata">

**Author:** ![Samsano](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/samsano/32/8307_2.png) [@Samsano](https://forum.makecode.com/u/Samsano)\
**Post date:** [April 21, 2023, 4:43pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/8 "2023-04-21T16:43:04Z")

</div>

I said in the post you have to go to terrrainGeneration.ts.

---

<div class="post-metadata">

**Author:** ![Samsano](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/samsano/32/8307_2.png) [@Samsano](https://forum.makecode.com/u/Samsano)\
**Post date:** [April 21, 2023, 4:43pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/9 "2023-04-21T16:43:09Z")

</div>

Would it be possible if you made it an extension? Just a suggestion and then people like, wanting to use Noise for games, could use it in blocks

---

<div class="post-metadata">

**Author:** ![yoxd707](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yoxd707/32/8506_2.png) [@yoxd707](https://forum.makecode.com/u/yoxd707)\
**Post date:** [April 21, 2023, 5:37pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/10 "2023-04-21T17:37:48Z")

</div>

Yesterday I made an extension so that perlin noise can be used, the idea would be to make another one for the generation of 2d terrains 🙃

> **[GitHub - yoxd707/pxt-perlin-noise: Extension to generate perlin noise in...](https://github.com/yoxd707/pxt-perlin-noise)**
>
> Extension to generate perlin noise in arcade makecode - GitHub - yoxd707/pxt-perlin-noise: Extension to generate perlin noise in arcade makecode

---

<div class="post-metadata">

**Author:** ![YuHayate](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yuhayate/32/8275_2.png) [@YuHayate](https://forum.makecode.com/u/YuHayate)\
**Post date:** [April 21, 2023, 5:50pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/11 "2023-04-21T17:50:54Z")

</div>

How it work?

---

<div class="post-metadata">

**Author:** ![yoxd707](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yoxd707/32/8506_2.png) [@yoxd707](https://forum.makecode.com/u/yoxd707)\
**Post date:** [April 21, 2023, 7:10pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/12 "2023-04-21T19:10:02Z")

</div>

The extension adds 5 functions, one in the Math section that would be the [lerp](https://en.wikipedia.org/wiki/Linear_interpolation) function.  
And another 4 functions in the Perlin Noise section, two functions for noise, 1D and 2D. And two functions for the Perlin Noise ([FBM](https://en.wikipedia.org/wiki/Fractional_Brownian_motion)), 1D and 2D.  
More documentation about it…  
[Simplex Noise](https://en.wikipedia.org/wiki/Simplex_noise)  
[Perlin Noise](https://web.archive.org/web/20160530124230/http://freespace.virgin.net/hugo.elias/models/m_perlin.htm)

> **[Sebastian Lague](https://www.youtube.com/@SebastianLague)**
>
> Hello, on this channel I explore how to create stuff out of code. I hope you find some of it interesting!

[He has videos explaining the subject](https://www.youtube.com/watch?v=wbpMiKiSKm8)

---

<div class="post-metadata">

**Author:** ![Samsano](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/samsano/32/8307_2.png) [@Samsano](https://forum.makecode.com/u/Samsano)\
**Post date:** [April 24, 2023, 1:51pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/13 "2023-04-24T13:51:55Z")

</div>

I have two questions:

1. How would i place tiles on the noiseMap

2. What does this parameter do?  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/f/fe5da3fed0ddf81e6e17d1d65269f6081391ea4b.png)

---

<div class="post-metadata">

**Author:** ![YuHayate](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yuhayate/32/8275_2.png) [@YuHayate](https://forum.makecode.com/u/YuHayate)\
**Post date:** [April 24, 2023, 8:06pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/14 "2023-04-24T20:06:26Z")

</div>

Do you have a explanation on how to use them?

---

<div class="post-metadata">

**Author:** ![yoxd707](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yoxd707/32/8506_2.png) [@yoxd707](https://forum.makecode.com/u/yoxd707)\
**Post date:** [April 25, 2023, 4:29pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/15 "2023-04-25T16:29:58Z")

</div>

This extension **only** serves to generate “noise” (numbers from 0 to 1), how and what you use it for is up to your imagination.

The x and y coordinate parameters must contain decimals for correct operation, so I recommend multiplying those parameters by a scale.

I suppose that this parameter is that of the noise1d function, it is the necessary coordinate to calculate the noise at that point (the one that should be multiplied by a scale)

I am working on the extension that will be used to generate terrains in a simple way, but you can use the one that I left if you want to make your own implementation in something else.

I leave a quick example on how to use the noise1d and noise2d function:

> **[perlin-noise-example](https://arcade.makecode.com/S74129-93962-93318-21129)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

**Author:** ![YuHayate](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yuhayate/32/8275_2.png) [@YuHayate](https://forum.makecode.com/u/YuHayate)\
**Post date:** [April 25, 2023, 9:21pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/16 "2023-04-25T21:21:25Z")

</div>

Wow your extention is relly impressive

---

<div class="post-metadata">

**Author:** ![YuHayate](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yuhayate/32/8275_2.png) [@YuHayate](https://forum.makecode.com/u/YuHayate)\
**Post date:** [April 28, 2023, 4:50pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/17 "2023-04-28T16:50:30Z")

</div>

i just made my first extention end added 2dNoise to it

> **Code Here**
>
> ```auto
> namespace SpriteKind {
> export const Temporary = SpriteKind.create()
> }
> let tempNum = 0
> let TempImg = image.create(16, 16)
> enum DegréesToRotate {
> clockwise = 1,
> counterclockwise = 2,
> dubbleClockwise = 3
> }
> enum Size {
> Whith = 1,
> Hight = 2
> }
> function PixelValue (X: number, Y: number, Image2: Image) {
> return (Image2.getPixel(X + 1, Y) + Image2.getPixel(X - 1, Y) + (Image2.getPixel(X, Y + 1) + Image2.getPixel(X, Y - 1)) + (Image2.getPixel(X + 1, Y + 1) + Image2.getPixel(X + 1, Y - 1) + (Image2.getPixel(X - 1, Y + 1) + Image2.getPixel(X - 1, Y - 1)))) / 8 
> }
> /**
> * Image Editor
> */
> //% weight=49 color=#f1992b icon=""
> namespace Image_Editor {
> /**
> * TODO: describe your function here,
> * @param myImage describe parameter here,
> * @param myImage2 describe parameter here,
> */
> //% block="draw $myImage onto $myImage2"
> //% myImage.shadow=screen_image_picker
> //% myImage2.shadow=screen_image_picker
> export function Print(myImage: Image, myImage2: Image): Image {
> let TempImg = myImage2.clone()
> for (let X = 0; X <= TempImg.width; X++) {
> for (let Y = 0; Y <= TempImg.height; Y++) {
> if (myImage.getPixel(X, Y) != 0) {
> myImage2.setPixel(X, Y, myImage.getPixel(X, Y))
> }
> }
> }
> return myImage2
> }
> }
> namespace Image_Editor {
> /**
> * TODO: describe your function here, makes perline noise from en image
> * @param Image2 describe parameter here, the image to Create prerline noise
> * @param BlendingLevel describe parameter here, the Blending Level
> */
> //% block="noise image onto $Image2 Thicness $BlendingLevel"
> //% Image2.shadow=screen_image_picker
> //% BlendingLevel.number
> export function CreatePerline_image(Image2: Image, BlendingLevel: number) {
> for (let X = 0; X <= Image2.width; X++) {
> for (let Y = 0; Y <= Image2.height; Y++) { 
> if (X == 0 || Y == 0 || X == Image2.width - 1 || Y == Image2.height - 1) {
> Image2.setPixel(X, Y, randint(12, 16))
> } else {
> Image2.setPixel(X, Y, randint(3, 15))   
> }
> }
> }
> for (let Times = 0; Times <= 1; Times++) {
> for (let X2 = 0; X2 <= Image2.width; X2++) {
> Image2.setPixel(X2, Times * (Image2.height - 1), PixelValue(X2, Times * (Image2.height - 1), Image2) + 1)
> }
> for (let Y2 = 0; Y2 <= Image2.height; Y2++) {
> Image2.setPixel(Times * (Image2.width - 1), Y2, PixelValue(Times * (Image2.width - 1), Y2, Image2) + 1)    
> }
> }
> for (let Times = 0; Times < BlendingLevel; Times++) {
> for (let X2 = 0; X2 <= Image2.width - 3; X2++) {
> for (let Y2 = 0; Y2 <= Image2.height - 3; Y2++) {
> Image2.setPixel(X2 + 1, Y2 + 1, PixelValue(X2 + 1, Y2 + 1, Image2))
> }
> }
> }
>     
> return Image2
> }
> }
> namespace Image_Editor {
> /**
> * TODO: describe your function here, rotate any square image
> * @param myImage describe parameter here, the image you want to rotate
> * @param Rotate describe parameter here, rotate it clockwise or counterclockwise
> */
> //% block="Rotate $myImage 90° $Rotate"
> //% myImage.shadow=screen_image_picker
> //% Rotate.enum
> 
> export function Rotate(myImage: Image, Rotate: DegréesToRotate): Image {
> let TempImg = image.create(myImage.width, myImage.height)
> if (Rotate == 1) {
> for (let X1 = 0; X1 <= TempImg.width; X1++) {
> for (let Y1 = 0; Y1 <= TempImg.height; Y1++) {
> TempImg.setPixel(X1, Y1, myImage.getPixel(myImage.height - Y1, X1))
> }
> }   
> } else if (Rotate == 2){
> for (let X2 = 0; X2 <= TempImg.width; X2++) {
> for (let Y2 = 0; Y2 <= TempImg.height; Y2++) {
> TempImg.setPixel(X2, Y2, myImage.getPixel(Y2, myImage.width - X2))
> }
> }
> }
> return TempImg 
> }
> }
> namespace Image_Editor {
> /**
> * TODO: describe your function here, find the width or the height of en image
> * @param image describe parameter here, the image you want to find the width or the height
> * @param size describe parameter here, Choose if you want to know the width or the height
> */
> //% block="$size of $MyImage "
> //% MyImage.shadow=screen_image_picker
> //% size.Size
> // come back to this one 
> export function FindSizeOf(size: Size, MyImage: Image): number {
> if (size == 1) {
> return MyImage.width
> } else {
> return MyImage.height
> }
> }
> }
> namespace Image_Editor {
> /**
> * TODO: describe your function here, maks a grid with your image
> * @param image describe parameter here, The image you want to make a grid of
> * @param Xtimes describe parameter here, the amont of times to place your image on the X axes
> * @param Ytimes describe parameter here, the amont of times to place your image on the Y axes
> */
> //% block="Grid of $imagE size x $Xtimes y $Ytimes"
> //% imagE.shadow=screen_image_picker
> //% Xtimes.Number
> //% Ytimes.Number
> export function MakeGrid(imagE: Image, Xtimes: number, Ytimes: number): Image {
> let TempImg = image.create(imagE.width * Xtimes, imagE.height * Ytimes)
> for (let X = 0; X <= imagE.width * Xtimes; X++) {
> for (let Y = 0; Y <= imagE.height * Ytimes; Y++) {    
> TempImg.setPixel(X, Y, imagE.getPixel(X % imagE.width, Y % imagE.height))
> }  
> }
> return TempImg
> }
> }
> 
> ```

its not the best but its good

---

<div class="post-metadata">

**Author:** ![YuHayate](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/yuhayate/32/8275_2.png) [@YuHayate](https://forum.makecode.com/u/YuHayate)\
**Post date:** [April 28, 2023, 4:50pm UTC](https://forum.makecode.com/t/making-a-terraria-like-game-need-help-with-an-error/19275/18 "2023-04-28T16:50:35Z")

</div>

ya… i dont know how to use github
