How would you write a function for all the planes (back/mid/fore-ground)?

Hi there!

As we have been learning in this course, whenever you see a bunch of repeated code, you can either make a function or a ‘for’ loop to go through them a bit more efficiently.

I thought to follow up on the previous person asking this, by trying something out (and failing sadly). The thing I would like the function to do is to draw the input plane, just like how this is done in the course. However, I was already stuck thinking on what the type of return should be of the function, as I am already drawing a texture inside the function itself. Furthermore, I think I might be messing up some syntax, since vscode is not highlighting the function nor the new variable made within the function. Therefore, could I perhaps ask for a hint as to how to accomplish a more efficient way to load the different planes?

void updatePlane (plane, float deltaTime, int scrollSpeed)
{
float planePosX{};
planePosX -= scrollSpeed * dT;
if (scrollSpeed <= -plane.width * 2)
{
scrollSpeed = 0.0;
}
Vector2 plane1Pos{planePosX, 0.0};
DrawTextureEx(plane, plane1Pos, 0.0, 2.0, WHITE);
Vector2 plane2Pos{planePosX + background.width * 2, 0.0};
DrawTextureEx(plane, plane2Pos, 0.0, 2.0, WHITE);
}

Sincerely,
Tom

I wouldn’t try and draw the background planes inside the function, it’s good to keep update code and drawing code separate.

However, the reason this is likely not working is that you have not specified a type for the variable `plane`. Which would be of the type `Texture2D`.

The other problem with your function is that `planePosX` will always start at 0 with each call. This will result in the background you’re looking to draw to never move, but be offset to whatever the value of deltaTime happens to be.

Here’s my take on the idea, again I left the actual drawing of the backgrounds out of the function (you could create a second function that focuses on drawing, if you’d like).

``````float updateBackgroundPos(float deltaTime, float scrollSpeed, float posX, float backgroundWidth)
{
posX -= scrollSpeed * deltaTime;
if(posX <= -backgroundWidth*2)
{
posX = 0;
}
return posX;
}
``````

And then use it like this (I left the old implementation in for contrast)

``````       // Scroll background
bgX = updateBackgroundPos(dT, 20, bgX, background.width);
// bgX -= 20 * dT;
// if (bgX <= -background.width*2)
// {
//     bgX = 0.0;
// }

// Scroll the midground
mgX = updateBackgroundPos(dT, 40, mgX, background.width);
// mgX -= 40 * dT;
// if (mgX <= -midground.width*2)
// {
//     mgX = 0.0;
// }

// Scroll the foreground
fgX = updateBackgroundPos(dT, 80, fgX, background.width);
// fgX -= 80 * dT;
// if (fgX <= -foreground.width*2)
// {
//     fgX = 0.0;
// }
``````

Thank you so much for the quick reply! I see and think I understand the reasons the function did not work. Please correct me if I misunderstood.

1. The reset to 0 of PosX for each frame the function is called on would not work, would result in no value larger than 0 ever.
2. The type of output if drawing the backgrounds would need the background datatype of Texture 2D.
3. For code structure sake, it’s better to keep different tasks like updating and drawing code seperate from eachother.

I’ve implemented the solution you thought of into the code. The function works, but only if each of the variables for the backgrounds have been initiated. So, instead of :
bgX = updateBackgroundPos(dT, 20, bgX, background.width);

it needed:
float bgX{updateBackgroundPos(dT, 20, bgX, background.width)};

And now it works! Although the code is now only slightly shorter, of course, I’m glad I asked. Learning experience!

Not quite, in your function signature you had `(plane, float deltaTime, int scrollSpeed)`. This should have thrown up an error when compiling your code as `plane` did not have a type associated with it. If the intention was to pass the texture of the background into the function, then you would need `Texture2D plan`. The return type of your function being `void` would have been fine for what you were trying to do, the flaw was mainly with PosX.

As for why you needed to initialize bgX when you attempted my implementation, I suspect that’s because I have those variables already initialized somewhere else. This isn’t a bad thing, but I’m glad you were able to make the adjustment for your code.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.