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.

Geek Culture / Coding practices?

Author
Message
MikeB
17
Years of Service
User Offline
Joined: 5th Apr 2007
Location: My Computer, Shropshire, England
Posted: 21st Jun 2007 17:31
Hi, I was just wandering (for all languages), about a few of your coding practices .
If you could either post your own practice, or answer one of my questions, I would be immensley greatful, thanks!

1st: If you have a variable that tells an entities state of being, would you have
(a) alive=1, alive=0
or (b) dead=0, dead=1
, serious ones now.

If, in your game, you had multiple ways to die, how would you code it?

Would you have a seperate sub for each type of death?
Or one sub with various variables?

An example of the first option would be (pseudo code);

if bullet hits player, gosub bullet death
if rock hits player, gosub crush death
if player falls, gosub fall death.

Second:
if bullet hits player, bullet=1
if rock hits player, crush=1
if player falls, fall=1
gosub death

death:
if bullet=1
(bullet death stuff)
if crush=1
(crush death stuff)
if fall=1
(fall death stuff.


So, umm, yeah, I'm wandering about the above, and also any other little "coding practice" tips you might have would be greatly appreciated.

Thanks,

E.D.


Shame I can't change my name
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 21st Jun 2007 18:01
Can of worms springs to mind

Personally, I'd be using a state machine. You flag all of your entities with the state they are in at any given time, and they get processed accordingly each cycle.

Kentaree
22
Years of Service
User Offline
Joined: 5th Oct 2002
Location: Clonmel, Ireland
Posted: 21st Jun 2007 18:02 Edited at: 21st Jun 2007 18:02
I personally would have something more descriptive like a playerstate variable. I would then have defined constants such as

#constant ALIVE 0
#constant CRUSHED 1
etc...

and set playerstate to the appropriate state.

[Edit] Batvink beat me to it

Fallout
22
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 21st Jun 2007 18:06
I always use a variable called "alive" rather than dead, and the creation of the entity involves setting the value to 1, therefore any empty spaces in the entity array will default to 0 and be dead, and therefore not processed. Often the alive flag is kinda the final positive step to tell the game to process the entity.

As for deaths, it's totally dependent on the game, and all the methods you mentioned are quite valid, so long as no code is duplicated and it's reasonably tidy.


Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 21st Jun 2007 18:07
<Keep in mind I didnt read the whole post>

I would use functions.


Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 21st Jun 2007 18:31
I tend to have a mode variable for everything, mode 0 is dead, and anything above 0 is a state, could be dying or jumping or whatever, but really the important thing is how you control and track these modes. In my enemy handling loops for instance, I just check the mode to see if it's >0, otherwise I ignore it - it's the responsibility of the bit of code setting that mode to 0 to deal with hiding or deleting the object as well as anything else that happens on death.

For each different death, I'd have a mode, maybe even use a band of numbers, say mode 100 is fall down dead (health<0), 101 might be decapitation (hit on head and health<0). Once the death mode is done, like once the animation has been played, I get rid of the model and set it's mode to 0.


Good guy, Good guy, Wan...
Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 21st Jun 2007 20:04
I use Dead as a variable, attached to a type. Like char(i).dead=1. That way everything is alive unless I specify otherwise. Of course, using Alive would be just as easy.

For death, death occurs automatically if health<=0. Say something happens to a character, he falls or gets hit by a fireball or stabbed. His health <0. Nothing else happens right there. Next loop, during the NPC handling routines, if char(i).dead=0 and char(i).health<=0, he's dead. Several things can happen. He can be removed, exploding debris can be generated at his location, and a few other things. If he isn't removed, each loop it animates his death until it's finally complete. At that point, char(i).dead=1. If his body was removed instantly, char(i).dead=1 automatically.

This is how I turn skeletons into piles of bones. It works nicely and is a pretty automated system.


Come see the WIP!
MikeB
17
Years of Service
User Offline
Joined: 5th Apr 2007
Location: My Computer, Shropshire, England
Posted: 21st Jun 2007 20:18 Edited at: 21st Jun 2007 20:18
Cool thanks very much!

Any other little "coding practices" tips you might have would be greatly appreciated .

Mike B

Call me Mike please
dab
20
Years of Service
User Offline
Joined: 22nd Sep 2004
Location: Your Temp Folder!
Posted: 21st Jun 2007 20:22
Don't leave your code up while your cat is walking around on your desk... Cats are mean.

Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 21st Jun 2007 20:26 Edited at: 21st Jun 2007 20:27
While I actually wouldn't do it this way (it'd normally based around SpriteHit() in PB). I do generally just have sets of flags that represent the various object states. So your example might look a bit like this.



Although I'd

MikeB
17
Years of Service
User Offline
Joined: 5th Apr 2007
Location: My Computer, Shropshire, England
Posted: 21st Jun 2007 20:39
Quote: "Although I'd"


??

But thanks for your example


Mike B

Call me Mike please
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 21st Jun 2007 21:07
Although, I normally tie the collision and reaction code together.

defines



resolve


NeX the Fairly Fast Ferret
19
Years of Service
User Offline
Joined: 10th Apr 2005
Location: The Fifth Plane of Oblivion
Posted: 21st Jun 2007 21:18
I use 0 as nothing (just stand there), 255 is dead and 1-254 are different modes.


Since the other one was scaring you guys so much...
MikeB
17
Years of Service
User Offline
Joined: 5th Apr 2007
Location: My Computer, Shropshire, England
Posted: 21st Jun 2007 22:04
Quote: "I use 0 as nothing (just stand there), 255 is dead and 1-254 are different modes."


That's cool , not thought of that.

Mike B

Call me Mike please
Jeku
Moderator
21
Years of Service
User Offline
Joined: 4th Jul 2003
Location: Vancouver, British Columbia, Canada
Posted: 21st Jun 2007 22:24 Edited at: 21st Jun 2007 22:25
For boolean variables names I tend to use 'is'--- i.e. player1.bIsAlive = false or something like that. Of course, if you're using VS.NET you wouldn't need to worry about hungarian notation.

Kentaree
22
Years of Service
User Offline
Joined: 5th Oct 2002
Location: Clonmel, Ireland
Posted: 22nd Jun 2007 19:54
Eww, hungarian notation is evil. When we introduced a coding standard in my last job, that was the first disallowed thing on the list, then again, we were using a strongly typed language. However, in DBP you can usually tell the type of variable by the suffix, %, or $ or nothing, unless you specify a var's type explicitly. In the case of boolean isSomething would give it away

Fallout
22
Years of Service
User Offline
Joined: 1st Sep 2002
Location: Basingstoke, England
Posted: 22nd Jun 2007 21:48
Hungarian notation is great for strongly typed languages. It can be useful in DBP too if you want to make it more readable, but like Kentaree said, the # and $ do most of the same job, and the floats/ints aren't so carved in stone.


David R
21
Years of Service
User Offline
Joined: 9th Sep 2003
Location: 3.14
Posted: 22nd Jun 2007 23:32
I think it's more "coding style" than coding standards, seeing as though the largest majority of us are coding alone rather than in groups - which means that unless you're enforcing some draconian 'standard' upon yourself, it'll just be your coding 'style'.


09-f9-11-02-9d-74-e3-5b-d8-41-56-c5-63-56-88-c0
Code Dragon
18
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 23rd Jun 2007 02:53 Edited at: 23rd Jun 2007 02:57
I would use a function called player_die() with a parameter with the type of death. That way you don't have to have global variables for the different types of death. Just put a select block in player_die() and you can kill the player from any point in your code in any manner you choose. Aim for freedom in coding like that.

I made this idea up myself: make sure each function does one, indisvisable operation. You wouldn't put code like



into a game because then the function is doing two things, checking if a goomba is above him AND killing him. Now there's no way to make him die any other way (unless you use the or operator dozens of times, yuck)

It would be much easier just to put the die code in a kill_mario() function. Now you can make any code kill him with just a function call. It could be because of lava, bottomless pits, whattever. The important thing is you don't have to drive yourself crazy changing the kill mario code all the time.

You never really know a person until you look at their google autocomplete entries.
Matt Rock
19
Years of Service
User Offline
Joined: 5th Mar 2005
Location: Binghamton NY USA
Posted: 23rd Jun 2007 05:20
I tend to write programs in a linear fashion, but not the normal type of linear... more of a linear in terms of what the player is going to see first. I literally start with splash screens, then the frontend, then the engine, then I fill the engine with stuff. Usually each operation in the engine has a sub... say for instance it's an RTS game, I'd have "build base," "boss troops around," and, oh I don't know, "go kill something," each with a sub. Then controls, number-crunching, and repetitives go into functions. What I should be doing is writing the engine and the subs/ functions in needs first, then go and do the frontend... then I wouldn't have a million frontends that serve no purpose bogging down my HD .

Login to post a reply

Server time is: 2024-11-18 19:19:49
Your offset time is: 2024-11-18 19:19:49