Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Code layout

Author
Message
Major Payn
21
Years of Service
User Offline
Joined: 16th Dec 2003
Location: United States of America
Posted: 15th Apr 2004 05:57
Hi

One of the areas of coding that has confused me the most has to be the code layout, you know were everything goes. Many times I find myself mind boggled on were I should put that loop or that Gosub label, but the biggest perplexor is the main loop or main code whatever you want to call it. I just never know were to put it.

I am about to start on a major project, this may seem a little stupid being that I am very much still a beginner, but the way I see it is that I have 3 years left in High school and if I want to get in that fancy game school I been drooling at Im going to need something fantastic to show them. Ok back to the point...My game is to be a FPS, the way I see it I start out with the main game code and then branch off onto things such as levels, menus, and the what nots that make a pretty game, the problem is that I dont really understand the "main loop (main code as I call it)". Should it be in a subroutine? were should it be located? I just dont understand how to use it. At first I thought is should be in a subroutine because if it is running all the time that might interfere with other operations (you dont want to be able to move around and shoot during the menu). This is how I would think it should look like (dont lagh at me if this is way off)

Subroutine (main code)
Subroutine (menu)
subroutine (levels)

then load each one when it is needed (if you are loading a level it should load the main code to)


I hope you can see what I am tryin to do here, because I cant really explain it very good. Could someone please put me out of my misery and tell me the way a big game with many levels and other things should look like (code wise I mean)?

P4 3.2Ghz/Alienware area 51/radeon 9800 pro 256mb/sound blaster audigy 2/5.1 surround sound speakers.
andrew11
22
Years of Service
User Offline
Joined: 23rd Feb 2003
Location: United States
Posted: 15th Apr 2004 06:24
The main Loop is a Do... Loop or a While... Endwhile loop, usually. It goes after the setup code. Subs and functions go after main loop with Data statements. You can use multiple files to put code in.

Uhh... Lemme lay it out...
[Setup Code]
Sync On, Sync Rate, Backdrop, [Loading Media], etc...
[Main Loop]
{ (Do)

Do code, and call all functions here...

Sync
} (Loop)

[[Cleanup]] Delete media, vars here. May be in a seperate function

(Optional):
[Funcitons]

[Subs]

[Data]

This is just suggested, and you can organize your code anyway you feel is best.

P.S. If your just starting out, I reccomend getting DarkBASIC Pro instead of Classic, as its supported, and more powerful, but just as easy.

"All programmers are playwrites and all computers are lousy actors" -Anon
Major Payn
21
Years of Service
User Offline
Joined: 16th Dec 2003
Location: United States of America
Posted: 15th Apr 2004 06:32
Yeah but DBpro also costs alot more Plus I got DBC for a dollar on ebay, cant beat that! and thanks I have a clearer understanding of the code, but what I still dont understand is that, Is the main loop running all the time? and wouldnt that interfere with other operations? And how would you get the menu to appear first?

P4 3.2Ghz/Alienware area 51/radeon 9800 pro 256mb/sound blaster audigy 2/5.1 surround sound speakers.
andrew11
22
Years of Service
User Offline
Joined: 23rd Feb 2003
Location: United States
Posted: 15th Apr 2004 06:59
The main loop runs constantly, but "focus" is diverted to other parts of code when functions and subs are called.

The computer sees one instruction after another..

[Computer]
Setup code...

Do...

Go to the function...

Loop back to the do

function FOO()
do stuff
Endfunction Go back to function call line

And it just goes forever until you exit the loop (Exit)

Menu code would be a sub/function, and called say whenever the variable "start" = 0. I just made up the variable. Make a variable, when you start the game =1, then when you lose, make it 0 to display menu.

"All programmers are playwrites and all computers are lousy actors" -Anon
BearCDPOLD
21
Years of Service
User Offline
Joined: 16th Oct 2003
Location: AZ,USA
Posted: 17th Apr 2004 11:32
Good news! EA is sponsoring video game design courses at major universities in California.

Anywho, for the code structure it helps to draw boxes and connect them with arrows, showing that they are kind of "inside" a particular part because that particular routine is called inside the other routine. This also allows you to branch into itty bitty tiny bits to make planning and execution a seemless process.

Here's my basic structure:

Main (Start of program)
>>>>>Utility functions
>>>>>>>>>>Includes improved LoadObject and LoadImage function
>>>>>SUB Declare variables
>>>>>SUB Declare arrays
>>>>>FUNCTION Setup graphics/system stuff
>>>>>FUNCTION Load Map
>>>>>>>>>>FUNCTION Display loading screen
>>>>>>>>>>FUNCTION Read .kzm level file
>>>>>>>>>>>>>>>FUNCTION Load All Object Types (.x stuff)
>>>>>>>>>>>>>>>FUNCTION Create predetermined object types(ie:lights)
>>>>>>>>>>>>>>>FUNCTION Create all triggers
>>>>>>>>>>>>>>>FUNCTION Load Sounds/Music Tracks
>>>>>Loop
>>>>>>>>>>FUNCTION Check player I/O
>>>>>>>>>>FUNCTION Check AI
>>>>>>>>>>FUNCTION Check Object State Machines bullets, barrels, etc
>>>>>>>>>>FUNCTION Check Triggers
>>>>>>>>>>FUNCTION Update display


Kinda lengthy, but the more you break it down the better. Imagine for everything that happens in your program there has to be a little piece of code controlling it. Plan that piece of code, don't leave it as a surprise for when you get there, surprises are bad for project efficiency.

Crazy Donut Productions, Current Project: KillZone
Web Site Button Does Not Work, Visit Here: http://www.geocities.com/crazydonutproductions/index.html
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 18th Apr 2004 19:36
code is done one instruction after the other, the exeptions are when you call a gosunb or function, hit a goto (goto is bad practice left in for legacy reasons) or use a conditional statement, these are the bits that give a program its power, for example the following code runs as it is written

print "hello"
print "world"

it will read the instruction print, put the letters hello into the print instruction, then copy the text hello to the screen, then it looks at the next line, reads the instruction print and copies the letters world to it, then it copies that text to the screen, then the program ends since it has no more instructions to follow, if you do

top:
print "hello"
print "world"
goto top

then it remembers the label "top" as a location it will need for later, then it does the instructions as before, then it hits a goto instruction and that tells the code to go and read the location "top", so it does, and the next instruction...etc, so it follows those instructions forever and you get an endless list of

hello
world
hello
world
hello
...
etc

all it does is read the instructions one by one and change the position it reads them from when you tell it to, now when you want to structure a program the simplest way is to do it like this

load models
load sounds
load images
set up variables
title screen
do <<<<<<<<<<<<<<<<<<<the outer loop
do <<<<<<<<<<<<<<<<<the main loop
update player
update monsters
update bullets <<<<<if any
update scores and hud info <<<<if any
if end of level then exit <<<set a condition (like a special location) that means the player has won and check for it, then exit if they have
if dead then exit
sync
loop
{clear away all unused models
load the next level
load the monsters positions}
or
let the player load a saved game
loop

the updates (update player etc) are function calls, you place these at the end of the code, the outer loop keeps the whole game running, the inner loop keeps the main game loop running, you exit the inner loop when you need to load another level or inform the player he has died and end the game (a special case of loading the level...this could be a savegame or level 1), general layout then is

load stuff needed for game
do
do
run the game
exit if dead or completed
loop
load next level if completed
game load screen for those that died
loop
functions
gosubs
data

that is pretty standard for most games, and the easiest to follow, cheers.

Mentor.

PC1: P4 hyperthreading 3ghz, 1gig mem, 2x160gig hd`s, ATI radeon 9800 pro gfx, 6 way surround sound, PC2: AMD 1.2ghz, 512mb ram, FX5200 ultra gfx, stereo 16 bit soundblaster, ups.

Login to post a reply

Server time is: 2025-05-23 19:36:38
Your offset time is: 2025-05-23 19:36:38