Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Main Loop

Started by caldiar Apr 5, 2005 at 10:04 AM 4 replies 850+ views
Original Post
caldiar
caldiar
Hey everyone. I've been studying C++ for almost a year now. I want to go over the very basic to make sure I've learned this right... To call up a function from another file in the main loop, let's say update() { if {not updated, then update} else {do nothing} } (which will be stored in update.cpp) would we then do something like this in the main game loop (Yes I know there are more specific things in the syntax but for the sake of simplicity Im just going to give the layout) #include update.cpp main() { if { object exists update(); else { wait for 5 seconds then try again } } } So, do I have everything right so far? Please correct me if Im wrong as i don't want to continue learning something the wrong way. Thank you for your input everyone.
jbadams
jbadams
Well, you've simplified that a lot, but essentially it follows the correct layout. Waiting 5 seconds in the main loop is a long time though - generally my loops are allowed to run as fast as they can (requires updating game logic based on the amount of time passed; search term "Time Based Modelling"), but some people do have them running on fixed time steps.
- Jason Astle-Adams
caldiar
caldiar
Thank you very much for your time. You've been a great help =)
I think Im going to go tinker around and make a quick main loop to call up a function just to see a simple function in action.
Thanks again =)

~Brandon~
EAX
EAX
You are including a .cpp file.

The correct approach would be something like this.

in update.h
void update();


in update.cpp;
#include "update.h"void update(){   //code here}


in main.cpp(or whatever you call it)
#include "update.h"int main(int argc, char* argv[]){    //code    update();    //more code}


There are probably problems with this but you get the idea.
EAX
jbadams
jbadams
Quote:
Original post by EAX
You are including a .cpp file.


Good point, I'd totally glossed over that. Structure-wise, your loop is still fine though.
- Jason Astle-Adams
caldiar
caldiar
Thank you for the input everyone. I was trying to get the main loop to work earlier this morning but I couldn't figure out why I was getting errors. I will make a header files and then include it as you've said.

Thanks again for the help =)

Brandon

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.