Page 1 of 2

Dealing with Qualities

PostPosted: Wed Jun 28, 2006 8:46 am
by Fuzzy
Remember each time you use IF a bunny dies. So avoid IF folks.

Ok. This is a somewhat advanced topic. I've dealt with this in other aspects, but I am going to expand on it.

First, you have a stick.

Image

So lovely. So brown.

Here is the setup. You are making a adventure game, and the player character is lost on an island, and must find and build the tools needed to survive. The stick is an object that can be used to create several other types of useful objects.

Part of the puzzle is for the player to build items, rather than just find them. You plan it to be open ended, so the player may even create things you didnt plan for.

So. A stick. We will deal with sticks, and you can extrapoliate this to other raw materials.

We make a list of qualities of objects in our world, and use define for them. The two important parts are that they are independant of each other, and that we define them in a way that they dont overlap. We use define statements, and powers of two(including 1). You cannot use 3. You cannot use 6. You will not use 10. Each is double the last.

Here are some possible raw object traits in our world.

Code: Select all
#define lengthy 2
#define hard 4
#define burns 8
#define straight 16
#define heavy 32
#define forked 64
#define shiny 128
#define dry 256


you get the idea...

now, in the stick actor, we have a integer variable(all game objects would have this too). Lets call it Traits. in the sticks create actor event, we put

Code: Select all
Stick.Traits = long | burns | straight | forked | dry;


you might want to put hard in there too. I decided that hard would be better for really hard things, like rocks.

Code: Select all
Rock.Traits = hard | heavy | dry;


Easy hey? Here is where it gets fun.

The player gathers a rock and a stick. He breaks the fork off the stick, so now its....
Code: Select all
Stick.Traits = long | burns | straight | dry;


and you can use a comparison on two sticks to see if they are the same, or if a stick can be used for an item.

Code: Select all
if (stick.traits == otherobject.traits)
{
       do stuff
}


or...

if the player makes his axe, and drops it in the fire, you check to decide if it will burn(because he might make an all metal axe!)

Code: Select all
if (traits & burns)
{
     destroyactor();
}


This leaves everything open ended(other than the graphic), and saves you a bunch of IF statements checking each possible item type to see what effect it has. Just check the Traits variable!

Oh, and you can have only a certain amount of traits. The largest numbered define would be one half of max integer(which is a lot!).

[edited to correct errors, errors, more errors. Thanks Makslane!]

PostPosted: Wed Jun 28, 2006 9:18 am
by jazz_e_bob
This is a great post. Thankyou.

PostPosted: Wed Jun 28, 2006 12:11 pm
by makslane
Great post, thanks!
Only few corrections:

If you want test individual traits, like burn, you must use bit operations, not arithmetic operations.

So, dont use + to combine the traits.
Use the bit operator | (or)

And, to test if a trait was set, use the bit operator & (and)
Note, && is for boolean operations.
& is for bit operations.

So, the code to combine will be:

Code: Select all
Rock.Traits = hard | heavy | dry;


And code to test burn:

Code: Select all
if (traits & burns)
{
     destroy actor;
}

PostPosted: Wed Jun 28, 2006 1:35 pm
by Fuzzy
Oops! You are right! Thanks for the corrections Makslane. I will go back and fix things.

I always get the bit and boolean operators mixed up.

I do think, if you use only powers of two you can get away with + and -? but of course the bitwise operators are faster, and more syntaxically correct.
Code: Select all
  2  +  8   =  10      or
0010 + 1000 = 1010


is the same end result as
Code: Select all
2 | 8


But for anyone reading, do as Makslane says, use the | operator.

PostPosted: Wed Jun 28, 2006 2:07 pm
by makslane
+ is not |
Don't works for 2 and 10 for example

PostPosted: Wed Jun 28, 2006 8:33 pm
by jazz_e_bob
How did you break the fork off the stick?

Stick.Traits -= forked; ?

PostPosted: Wed Jun 28, 2006 9:41 pm
by Fuzzy
jazz_e_bob wrote:How did you break the fork off the stick?

Stick.Traits -= forked; ?


Nope. Try this Jazz_e_bob...

Stick.traits ^= forked;

Thats called xor, and works similar to |, but what it does is toggle back and forth. Try it out with a textNumber to see how that works. The trick that is that if there was no fork in the first place, it will add one.

I just want to point out an important BAD thing I did in that qualities list. You cannot use the word 'long' as it is an important keyword for C (and GE).

Re: Dealing with Qualities

PostPosted: Wed Jun 28, 2006 9:42 pm
by DilloDude
ThreeFingerPete wrote:Remember each time you use IF a bunny dies.

Kill the bunnies! :twisted: IF! IF! IF! IF! IF!... :twisted:

PostPosted: Wed Feb 14, 2007 9:51 am
by zoiba
this is a great tip! thanx Fuzzy

PostPosted: Sat Feb 17, 2007 4:18 pm
by Sgt. Sparky
:shock: Wow, it's awsome! :D

look at the locations right above mine, A chair (in) italy :lol:

PostPosted: Thu Feb 22, 2007 12:47 pm
by irblinx
Nicely explained

Re: Dealing with Qualities

PostPosted: Thu Feb 22, 2007 3:11 pm
by Sgt. Sparky
DilloDude wrote:
ThreeFingerPete wrote:Remember each time you use IF a bunny dies.

Kill the bunnies! :twisted: IF! IF! IF! IF! IF!... :twisted:

I woike a widdle wabbit! :cry:
use IF I do not shoot a person who said "IF! IF! IF! IF! IF!..."
:cry:

PostPosted: Fri Feb 23, 2007 1:48 am
by Game A Gogo
a bunny is born every time an IF statement is used correctly.

Problem solved!

PostPosted: Sat Feb 24, 2007 11:44 am
by Troodon
if (bunny.population >= 1)
{
a bunny dies;
}
else if (bunny.population == 0)
{
create *10* bunnies;
}

This solves all the problems! :D





Btw what's bunny? I can't find it from dictionarry and english isn't my first language.

PostPosted: Sat Feb 24, 2007 1:07 pm
by Fuzzy
A bunny is a baby rabbit.