Hi,
I'm making this video game in C.
The game contains a player, enemies and bullets. These objects,
which are arrays of structures, are initialized, updated and drawn
in a game loop. Now I am unsure on where to declare these things.
I could declare them in main() like this
#define NUM_ENEMIES 10
#define NUM_BULLETS 20
int main(void)
{
struct *player player1;
struct enemy enemies[NUM_ENEMIES];
struct bullet bullets[NUM_BULLETS];
init_player(pla yer1);
init_enemies(en emies, NUM_ENEMIES);
init_bullets(bu llets, NUM_BULLETS);
while (1) {
update_...
draw_...
}
}
Or, I could declare them globally and make main() a little "cleaner"
by calling the functions with void parameters and let the functions
handle the objects and the count by themselves.
I don't know which one to choose, the second way seems a little
more modular, since I move the internal (should it be internal?)
stuff out of main.
But OTOH, I've read that local variables are better
than global. But again, if I put the handling functions in an other
file, and have static linkage, they won't be global, right?
I would be grateful if someone could give me some points on how
to do.
Thanks!
Jun 3 '06
18 2278
On 2006-06-04, CBFalconer <cb********@yah oo.com> wrote: Andrew Poelstra wrote: ... snip ... Create a GAME struct (I will call it "world" because I think that's what you want):
struct world { /* All necessary world data here */ };
Ridiculous. Well designed to make the end result unmaintainable.
In my code, it's fairly obvious what globals are being changed (I never
have more than three or four) in which functions.
If there's ambiguity (or a situation where I'll be maintaining instead of
rewriting), I'll comment what variables are changed, and why.
--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
If we would just let the poachers into the zoo, we'd
have less squashed people and more fancy pianos!
Richard Heathfield wrote: jaso said: Ok, now I understand why to avoid the world structure. Thanks! So, generally, the best way is to declare all structures separately in a "high layer" like the game loop, and to pass all needed structures all the way down to the lowest level functions?
Yes, sort of. Ideally, each function should only be given the information it actually needs for doing its job. So try to arrange your structures in such a way as to minimise the amount of superfluous information you hand to your functions. And localise your data as much as possible. If you must define a structure in main(), okay - if you must, you must. But at least investigate the possibility of making it more local than that.
In my reworking of my text adventure game (a programming project 15
years old and still going!) I've tried to limit most entities' access
to game state data.
One way of doing this is to sort of borrow a concept from OO
programming. I move a part of the state data into a separate
translation unit, and make it static within there. There are a
relatively few public functions for which can access the data only in
certain predefined ways, and a bunch of static helper functions to
accomplish this.
So, for instance, no other part of the game can directly manipulate the
player inventory. They can use the public functions to query for an
item, add one, drop one, output the list, etc. This allows changing the
underlying data structures to a large degree without affecting the rest
of the game.
Brian
"Richard Heathfield" <in*****@invali d.invalid> wrote Ok, now I understand why to avoid the world structure. Thanks! So, generally, the best way is to declare all structures separately in a "high layer" like the game loop, and to pass all needed structures all the way down to the lowest level functions?
Yes, sort of. Ideally, each function should only be given the information it actually needs for doing its job. So try to arrange your structures in such a way as to minimise the amount of superfluous information you hand to your functions. And localise your data as much as possible. If you must define a structure in main(), okay - if you must, you must. But at least investigate the possibility of making it more local than that.
When you are wriitng world simulators this approach, though generally a good
idea, doesn't work.
The problem is that real world objects do communicate their state to each
other. If I walk into a pool of hot water, the water state communicates with
my nerves and the block of butter I am carrying in my knapsack.
So the only way to handle things, in practise, is to have a "world"
parameter that is apssed to every function. That is not to say that you
don't try to communicate through interfaces as much as possible, but
inevitably someone will throw a curveball that you didn't think of some time
during development.The demon turns red when it is about to attack, but it
doesn't look nice against a red wall. So if the wall is red, just turn off
the attack routine. Great idea, but now the attack locic needs access to the
wall pixel data.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
Malcolm wrote: When you are wriitng world simulators this approach, though generally a good idea, doesn't work. The problem is that real world objects do communicate their state to each other. If I walk into a pool of hot water, the water state communicates with my nerves and the block of butter I am carrying in my knapsack. So the only way to handle things, in practise, is to have a "world" parameter that is apssed to every function. That is not to say that you don't try to communicate through interfaces as much as possible, but inevitably someone will throw a curveball that you didn't think of some time during development.The demon turns red when it is about to attack, but it doesn't look nice against a red wall. So if the wall is red, just turn off the attack routine. Great idea, but now the attack locic needs access to the wall pixel data.
Indeed. I've thought alot since my first post about the design, and if
I am going to avoid globals I need to pass a lot of data as arguments
to my functions. And a world structure would free me from the uglyness
of 6+ argument functions. For example, if I have a update_enemies( )
function that moves the enemies and make them shoot bullets at the
player, I would have to declare something like
update_enemies( struct enemy *e, int num_enemies, struct bullet *b,
int num_bullets, struct player *p, int num_players);
And if I add things like powerups or weapons I need to add more
arguments if I want the enemies to interact with them. IMHO,
that would be pretty messy.
A call like update_enemies( world) is cleaner, and then from that
function I can call functions that don't need all world data like this
enemy_shoot(w->enemy, w->player);
Does that sound reasonable, or am I way off?
On 2006-06-05, Malcolm <re*******@btin ternet.com> wrote: "Richard Heathfield" <in*****@invali d.invalid> wrote Ok, now I understand why to avoid the world structure. Thanks! So, generally, the best way is to declare all structures separately in a "high layer" like the game loop, and to pass all needed structures all the way down to the lowest level functions?
Yes, sort of. Ideally, each function should only be given the information it actually needs for doing its job. So try to arrange your structures in such a way as to minimise the amount of superfluous information you hand to your functions. And localise your data as much as possible. If you must define a structure in main(), okay - if you must, you must. But at least investigate the possibility of making it more local than that.
When you are wriitng world simulators this approach, though generally a good idea, doesn't work. The problem is that real world objects do communicate their state to each other. If I walk into a pool of hot water, the water state communicates with my nerves and the block of butter I am carrying in my knapsack. So the only way to handle things, in practise, is to have a "world" parameter that is apssed to every function. That is not to say that you don't try to communicate through interfaces as much as possible, but inevitably someone will throw a curveball that you didn't think of some time during development.The demon turns red when it is about to attack, but it doesn't look nice against a red wall. So if the wall is red, just turn off the attack routine. Great idea, but now the attack locic needs access to the wall pixel data.
Well, you could have a "get_sight(WORL D_OBJECT self)" function,
which returns a struct or an array containing every object visible to the
caller. You could extract red wall data from there, and it would be basically
the same as the AI which allows the character to attack at all.
--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
If we would just let the poachers into the zoo, we'd
have less squashed people and more fancy pianos!
jaso <as@email.com > wrote: CBFalconer wrote: Andrew Poelstra wrote: ... snip ... Create a GAME struct (I will call it "world" because I think that's what you want):
struct world { /* All necessary world data here */ };
Ridiculous. Well designed to make the end result unmaintainable.
How come? Why will it be unmaintainable?
Some people are scared of global data and want to pretend that it is a
local to main(). To avoid this abhorrence, they put all their data in a
struct in main(), and then pass lots of pointers to every non-trivial
function. The result is that you still don't know which function
modifies which member of the struct, and you have to pass world pointers
everywhere. Big win :-/.
IYAM, if data is global by nature, make it global by declaration, too.
If 90% of your functions are going to refer to the "monsters in the
dungeon" array, why pretend that it's not a global object?
Richard
On 2006-06-08, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote: jaso <as@email.com > wrote:
CBFalconer wrote: > Andrew Poelstra wrote: > ... snip ... >> Create a GAME struct (I will call it "world" because I think >> that's what you want): >> >> struct world >> { >> /* All necessary world data here */ >> }; > > Ridiculous. Well designed to make the end result unmaintainable.
How come? Why will it be unmaintainable?
Some people are scared of global data and want to pretend that it is a local to main(). To avoid this abhorrence, they put all their data in a struct in main(), and then pass lots of pointers to every non-trivial function. The result is that you still don't know which function modifies which member of the struct, and you have to pass world pointers everywhere. Big win :-/. IYAM, if data is global by nature, make it global by declaration, too. If 90% of your functions are going to refer to the "monsters in the dungeon" array, why pretend that it's not a global object?
The only functions to which I would pass an entire struct would be
a few initialization or cleanup functions.
Everywhere else, I pass specific members of the struct. I prefer the
struct system because it means that I can go to a header file and see
all of my members in one place.
--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Andrew Poelstra <ap*******@loca lhost.localdoma in> wrote: On 2006-06-08, Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote: jaso <as@email.com > wrote:
CBFalconer wrote: > Andrew Poelstra wrote: > ... snip ... >> Create a GAME struct (I will call it "world" because I think >> that's what you want): >> >> struct world >> { >> /* All necessary world data here */ >> }; > > Ridiculous. Well designed to make the end result unmaintainable.
How come? Why will it be unmaintainable?
Some people are scared of global data and want to pretend that it is a local to main(). To avoid this abhorrence, they put all their data in a struct in main(), and then pass lots of pointers to every non-trivial function. The result is that you still don't know which function modifies which member of the struct, and you have to pass world pointers everywhere. Big win :-/. IYAM, if data is global by nature, make it global by declaration, too. If 90% of your functions are going to refer to the "monsters in the dungeon" array, why pretend that it's not a global object?
The only functions to which I would pass an entire struct would be a few initialization or cleanup functions.
Everywhere else, I pass specific members of the struct.
How does that help when 80% of all major functions need to read that one
member, and some 30% want to change it?
Richard
"Andrew Poelstra" <ap*******@loca lhost.localdoma in> wrote
[ world structures in video games ] The only functions to which I would pass an entire struct would be a few initialization or cleanup functions.
Everywhere else, I pass specific members of the struct. I prefer the struct system because it means that I can go to a header file and see all of my members in one place.
You need to pass the entire struct, but not necessarily to access it.
The problem is that in the real world there are no barriers to information
flow, and requirements chance.
If a games designer says "stop the demon from attacking if it is standing
next to a red wall" he doesn't want to be told that we can't achieve this
because the attack logic is in the gameplay module whilst the wall pixel
data is locked up in the texture cache. The information is in the computer,
so he sees his request as reasonable.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: BlackHawke |
last post by:
My Name is Nick Soutter, I am the owner of a small game company, Aepox Games
(We're in the middle of a name change from "Lamar Games"),
www.lamargames.net.
Our first commercial game, Andromeda Online (www.andromedaonline.net) is
going into beta soon. It runs on an evaluation edition of SQL Server 2000
(our intention is, when it launches, we earn the money to buy a copy before
the evaluation expires).
|
by: theodp |
last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml
Microsoft Patents Saving The Name Of A Game
Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM
from the yeah,-that's-non-obvious dept.
theodp writes "As if there weren't enough dodgy patents, here's an
excerpt from one granted to Microsoft Tuesday for a 'Method and
apparatus for displaying information regarding stored data in a gaming
system': 'When saving a game,...
|
by: the_philospher |
last post by:
I am a beginner programmer learning c++. i have read some useful
information about programming games (which is my goal) on gamedev.net,
particularly "how do i program games"
http://www.gamedev.net/reference/design/features/makegames/.
the article suggests first programming a tetris clone, then moving on
to "breakout", "pacman", and a super mario style game..
My question is, how long should it realistically take me to be able to...
|
by: enki |
last post by:
I am writing a game and I am having trouble with moving the character
on the map.
Here is what I have right now. It involves win32 programming but that
not my problem. I would like some suggestions how I can make my code
better. If you need more code I will post it. I am in early
devlopment of the game.
map.h:
|
by: Brandon J. Van Every |
last post by:
Anyone know of any "good" open source C# game projects out there? Something
that actually has a game engine and some content done, so I can just fiddle
with it and do interesting / goofy things. I hesitate to state genre
preferences. I've been all over SourceForge and it's slim pickings in C#
land, at least for projects that have actually gotten something done. I'll
be doing my last round of sifting and searching tonight, then I'm...
| |
by: Valerie |
last post by:
Ok. Help. the quarter is ending soon and I have no interest in creating
games. I think that's why i have a mental block on this particular lab
assignment.
I'm using the same book as Steven Smith is using when One Handed Man helped
him.
And, I don't even know where to continue.
The application is a tic tac toe game.
|
by: Chu |
last post by:
Hello All-
I'm writing a game using asp.net (c#). The game is a browser-based Role
Playing Game. I'm trying to determine the best way to design the game
with scalability in mind.
The Player class will hold info about the player, such as hit points,
weapons and armor that are equipped, and other stats specific to the
player. Each page on the site will require some or all of the info
stored in the player object.
|
by: v4vijayakumar |
last post by:
In computer based, two player, board games, how to make computer play?
Are there any formal ways to _teach_ computer, to choose best possible
move?
I know this is kind of off-topic here. Please redirect me, if there
are more appropriate newsgroup.
Many thanks.
|
by: swasa |
last post by:
We at AJ Aquare are engaged in design and development of PC-based,
online and mobile games. Our Game designers are passionate towards
creating games that people long to have. Our designs can drive
millions of players to your site.
sources: http://www.ajsquare.com/games/game_design.php?game=design
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |