Is code for a lot of video games mostly if statements?

174 views

I know some coding, it’s mostly for web development. To me it just seems things that happen in games occur as the result of some condition being met or not met. Am I missing the mark here or is it something similar?

In: Technology

Depends on what you’re including. It’s like asking if a car is mostly made of circles. Yes, there are a whole lot of circles in a car, but I wouldn’t say it’s “mostly” circles.

Are you including just the line that checks the condition, or all the code that runs as a consequence of it?

Well, if statements are really the fundamental building block of programming. In assembly language, if statements are comparable to branch statements, which tells the execution to hop from the branch to the line of code to which it points.

So, yes and no. All logic driven code is done with branch statements, but there is a considerable amount of stuff happening in between. From a code density perspective, no, most lines of a game’s code are not if statements. However, in the underlying libraries used by the game, a single method call might entail the use of 100 if statements.

I would not say that most statements if statements but many are.

If you are interested you can look at the code of some older computer games that have been published. So take a look at some older ID software games like DOOM 3 from 2004 where you can see what code for a commercial game looks like. A game from back then is fundamentally not that different from games today.

https://github.com/id-Software

Technically, everything in computing is the result of a condition being met or not (zeros and ones).

It uses a lot of objects and then if statements between them.
Something like this, though with actual coding language:

Create circle, name it “Player”

Create rectangle, name it “Floor”

Correspond “Player” position on screen to WASD, speed 3

If “Player” touches “Floor,” add friction of some -x

If “Player” not touching “Floor,” add gravity of some -y

Create rectangle, name it “Gun”

Create triangle, name it “Bullet”

Create circle, name it “Enemy”

Create number counter, name it “Kill Count”

If “Player” has “Gun” and X button pressed, spawn “Bullet”

When spawned, send “Bullet” x direction at x speed

If “Bullet” touches “Floor,” delete “Bullet”

If “Bullet” touches “Enemy,” change “Kill Count” +1

If “Bullet” touches “Player,” reset the game

that’s like saying that all the sites on internet are mostly made of ‘body’ tags
well yeah.. but there’s plenty of other things inside as well, you can’t make content with empty body