iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🍵

Grouping Name, HP, and Attack Power with Structs: Character Data in C - Programming for All Roles Part 7

に公開

Recap of the Previous Lesson

https://zenn.dev/mattya3713/articles/67a9fb073515bd

In the last lesson, we learned about pointers.

  • Passing a value to a function passes a "copy".
  • If you want to change the actual value, you pass the "address" (pointer).
  • Use & to get the address and * to manipulate the data at that address.

It is okay if you didn't understand it perfectly.
Simply knowing that "such a mechanism exists" was enough.


This time, we will switch gears to a very convenient mechanism.

Up until now, we have been managing character data like this:

int   PlayerHP     = 100;
int   PlayerAttack = 30;
int   PlayerDefense = 10;
char  PlayerName   = 'A';

The number of variables keeps increasing, doesn't it?

What if there are two characters?

int   Player1HP      = 100;
int   Player1Attack  = 30;
int   Player2HP      = 80;
int   Player2Attack  = 20;

This is difficult to manage.

Is there a way to bundle "data for one person" together?

Yes, it's called a struct.

  • What is a struct?
  • How to use a struct
  • Combining it with arrays

We will look into these topics.

We will learn the mechanism of "grouping scattered data into a single block."


For this chapter as well, please actually type and test the code.

Rather than just reading,

Type it yourself
 ↓
Run it
 ↓
Fix errors / Ask someone ← This is where you level up
 ↓
Check the result and be convinced

This is the fastest way to master it.

Before Learning About Structs

First, let's run a piece of code.

#include <stdio.h>

struct Character
{
    int HP;
    int Attack;
};

int main()
{
    struct Character Player;
    Player.HP     = 100;
    Player.Attack = 30;

    printf("HP: %d\n",     Player.HP);
    printf("Attack: %d\n", Player.Attack);

    return 0;
}

Copy and paste the code and press the F5 key to run it.
If the HP and attack power are displayed, you have succeeded.

Once you have confirmed that it works, close the black window by clicking the X button in the top right, and let's actually learn about it.

What is a Struct?

In a word,

"A container that bundles scattered variables together."

This is a struct.

Breaking down the code from earlier, it looks like this:

struct Character
{
    int HP;
    int Attack;
};
struct

→ "Create a struct"

Character

→ The name of the struct (the name of the container)

int HP;
int Attack;

→ The contents of the container (member variables)

This struct Character is still just a "blueprint."
To use it, you create a variable.

struct Character Player;

This is the state where "you have created a variable named Player of the Character type."

Accessing the Contents

You can access the contents using a . (dot).

Player.HP     = 100; // Store 100 in HP
Player.Attack = 30;  // Store 30 in Attack

printf("HP: %d\n", Player.HP); // Extract it

"Player's HP", "Player's Attack"
You can read it just like regular English.

How to write initial values all at once

You can also assign values all at once when creating a variable.

struct Character Player = {100, 30};
//                         HP  Attack in order

It is concise, but be careful not to mix up the order.

When There Are Multiple Characters

Here, we combine it with arrays.

"There are 3 enemies. I want to manage the HP and Attack of each one."

struct Character Enemy[3];

Enemy[0].HP = 100;
Enemy[0].Attack = 20;

Enemy[1].HP = 80;
Enemy[1].Attack = 30;

Enemy[2].HP = 60;
Enemy[2].Attack = 40;

You can also write it collectively.

struct Character Enemy[3] = {
    {100, 20},
    { 80, 30},
    { 60, 40}
};

If you combine it with a for loop, you can process everyone at once.

for (int i = 0; i < 3; i++)
{
    printf("Enemy %d HP: %d Attack: %d\n",
        i + 1, Enemy[i].HP, Enemy[i].Attack);
}

Summary

What we did this time is:

Bundle scattered variables into a single block (Struct)

Access the contents using .

Combine with arrays to manage multiple characters


You who have made it this far have acquired these 8 skills:

  • Creating values (Variables)
  • Calculating (Arithmetic operations)
  • Judging (if)
  • Repeating (while / for)
  • Arranging data (Arrays)
  • Bundling processes (Functions)
  • Passing addresses (Pointers)
  • Bundling data (Structs)

Thank you very much for your hard work!

With this, the foundation of "how to think about programming" is complete.

From now on, you will be able to think by breaking things down bit by bit, saying, "Ah, this is just a combination of those 8 things."

And from the next chapter, we will gradually lift the restrictions on things we "left out for the sake of simplicity."

More convenient ways to write code.
Safer ways to build things.
Ways of thinking for building larger games.

You will gradually see why C++ is used in game development.

Of course, this is not just for people who want to become game programmers.

"How to organize"
"How to make it robust"
"How to scale it up"

From here on out, this is "practical knowledge."

Welcome!
To the realm of C++!

--

Next Time


All-Role Programmer Plan #7

Discussion