Users browsing this thread: 1 Guest(s)
C++ Object parameter question

#3
Posts: 3,971
Threads: 279
Thanks Received: 238
Thanks Given: 58
Joined: Oct 2011
Reputation: 65
Status
Tissue-aware
If you are comfortable with bits and bytes you could have 8 attributes or less on a 2 digit number (hex value). This is how attributes are stored in FF3us. Maybe some will find it overkill with modern programming but if you want to save parameters it's a good solution.

So a byte have 8 bits and it's a 8 digit binary number. C++ or most programming languagues will accept bit 0 to bit 7 as 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40 and 0x80. Notice that it double everytime (it's binary duh!). Any combination of 1 to all 8 bits will give you a number between 0x00 and 0xFF in hexadecimal base (0xFF = 256). Now each bit will represent an attribute.

Code:
public class Monster
{
    bool attribute1
    bool attribute2
    bool attribute3
    bool attribute4
    bool attribute5
    bool attribute6
    bool attribute7
    bool attribute8

    public Monster(byte attributes)
    {
           if((attributes & 0x01) != 0){ attribute1 == true} // if bit 0 is set, attribute1 is true
           if((attributes & 0x02) != 0){ attribute2 == true} // if bit 1 is set, attribute2 is true
           if((attributes & 0x04) != 0){ attribute3 == true} // if bit 2 is set, attribute3 is true
           if((attributes & 0x08) != 0){ attribute4 == true} // if bit 3 is set, attribute4 is true
           if((attributes & 0x10) != 0){ attribute5 == true} // if bit 4 is set, attribute5 is true
           if((attributes & 0x20) != 0){ attribute6 == true} // if bit 5 is set, attribute6 is true
           if((attributes & 0x40) != 0){ attribute7 == true} // if bit 6 is set, attribute7 is true
           if((attributes & 0x80) != 0){ attribute8 == true} // if bit 7 is set, attribute8 is true
    }
}

Now you can just create a monster like that:

Monster m1(0x5F) or
Monster m1 = new Monster(0x5F)

meaning attributes 1, 2, 3, 4, 5 and 7 will be true for that monster.

It might be hard to figure out for someone who doesn't know hex but you can save parameters that way. I can't think of any other solution.



Edit: Lockirby2's solution is also good:

Code:
int[] attributes = new int[8]
attributes = {0, 1, 1, 0, 0, 1, 0, 1}

Now you could just do: Monster m1(attributes), and you will check if each attribute of the array is true(1) or false(0).

Man I'm so rusty in C++, I think I'm mixing C++ and C# syntax lol
  Find
 



Messages In This Thread
C++ Object parameter question - by Lloyd2k4 - 05-21-2013, 04:58 PM
RE: C++ Object parameter question - by Lockirby2 - 05-21-2013, 05:59 PM
RE: C++ Object parameter question - by madsiur - 05-21-2013, 06:27 PM
RE: C++ Object parameter question - by Lloyd2k4 - 05-23-2013, 02:30 PM
RE: C++ Object parameter question - by Daemoth - 05-23-2013, 04:32 PM

Forum Jump:

Users browsing this thread: 1 Guest(s)


Theme by Madsiur2017Custom Graphics by JamesWhite