Jump to content
tomis13lack

Add Do...While loops to the C++ tutorial API.

Recommended Posts

**Before I do this, i want to let you know that I am not well experienced in leadwerks C++. I am using what i know to combine it. I do not know any wait commands in leadwerks so i made my own. I am also not sure if that is how it wants the keys. Please fix as you see fit**

Just in case someone is to need it:

Do...While Loops

 

Like, the while loop, the do...while loop continues looping over its code until a condition is met. The only difference is a do...while loop runs the code once before checking the condition. The syntax is as follows:

do
{
    //code to be executed
} while (condition)

The example below can be pasted into your "main.cpp" script file to see how do...while loops work.  In this example, I am using a while loop like from the previous example. The do...while loop makes it so when you first start, it displays the "Tab menu" for 5 seconds. It will do nothing until tab is pressed again and it will display the tab menu for 5 seconds after the tab button is released and all of the time in between.

#include <unistd.h> // Library used for usleep
#define sleep(a) for(int i = 0;i<a;i++) {usleep(500000);usleep(500000);} // Creating a wait command for timing where "a" is the seconds

//Create a window
Window* window = Window::Create();
//Create a rendering context
Context* context = Context::Create(window);

// While loop
while (window:KeyHit(Key.Esc)==true) {
	   //Do...While loop
		do {
		   //Set the drawing color and clear the screen
        	context->SetColor(0,0,0);
        	context->Clear();

       	   //Set the drawing color and blend mode
        	context->SetColor(1,1,1);
        	context->SetBlendMode(Blend.Alpha);

       	   //Draw some text onscreen
        	context->DrawText("This is the Tab Menu!",2,2);

       	   //Update the screen
        	context->Sync();
  			sleep(5)
		} while (window:KeyHit(Key.Tab)==true)
}

 

  • Like 1

Share this post


Link to post

Decided to do this one as well simply because I thought "why not." I am sure that Josh is quite busy so i'll try to do some of the more annoying/tedious stuff for now:

Function Overloading


Functions can be "Overloaded." An overloaded function is a function that exists with different arguments. This can be used for some functions that you may want to run with different argument types. This may also be used to allow default values and to avoid compiling errors:

int addOne(int num) {
    return ++num;
}

int addOne() {
    print("There is no argument");
}

addOne() //Output is nil, but it prints "There is no argument"
addOne(5) //Outputs 6
  • Like 1

Share this post


Link to post

I don't personally think the do-while loop is that useful. Hence why it hasn't been used.

In my whole 6.5 years of professional software development, I can only recall using it once.

The same code for Leadwerks is there as the easiest possible way to show an example of the desired method call such as Entity::SetPosition

Complicating it with structures for learning purposes will just complicate these examples IMO.

Share this post


Link to post

Function overloading...yes.  Do, while...no.

While the information you posted here is valuable and appreciated, it is not my goal to explain the entire C++ language, just the parts you need to know.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...