How to store multiple boolean values in one int?

Talk about making games.

How to store multiple boolean values in one int?

Postby bspeers » Fri Jun 22, 2012 3:30 am

Hi folks,

bspeers here. I'm new to GE but not to scripting engines. I'm working on a sequel to Flags of Doom, a game nobody here could have possibly heard of. I like that.

I'm working on a save-system for a collection-based game with potentially 1000 or more collectables. Think a non-linear Jumpman (C64 era) with about 100 levels. Flags of Doom was 120 levels, so this isn't a big leap.

Now storing the actor's status, inventory, score, etc is all trivial as far as I understand GE's built in save system, but I also need to track whether each flag has been collected. It's not enough to simply count how many have been collected either. I want, upon loading the game, for you to be able to return to an uncompleted room and have your progress resume. I don't want to record every enemy defeated, every jump or anything like that, but do want to record whether any particular flag or other collectable has been taken and draw only those that haven't been snagged. The flags are placed strategically around each room, so knowing whether THAT PARTICULAR flag has been grabbed is essential.

Now I could create an array representing every flag, but that would mean having to declare possibly 1000 or more variables. I want to streamline my data.

My thought is that I could use a bitwise operator to isolate digits of an int and then have a calculation that adds a specified and checkable amount that is then compared via >> and <<. I'm not an expert on programming, but I believe this could be used to store as many as 27 different flags in a standard 9 digit int. For example, if you collect flag 1, add 1 to flagcount. If you get flag two, add 3. If you get flag 3 add 5. If you get flag 4, add 10. Flag, 5, 30, 6. 50 and so on. This way you could do only a couple checks and know whether particular flags were taken from individual levels. If the first digit is = to 4, we know flags 1 and 2 have been taken. If it's equal to 9, we know flags 1, 2, and 3. So on and so forth.

As you can probably tell, however, I don't really understand how bitwise operators work. In fact, I'm not sure they can be used to isolate non-binary integers in this way at all.

Does anyone A) have any advice on how to make this method work, or B) a simpler method for recording large amounts of data in one or two integers? Something that can be saved in GE games.

Thanks in advance!

tldr edition: how to store multiple boolean values in one int?
bspeers
 
Posts: 15
Joined: Thu Jun 21, 2012 2:18 am
Score: 0 Give a positive score

Re: How to store multiple boolean values in one int?

Postby jimmynewguy » Fri Jun 22, 2012 5:34 am

I still think an array would work well for this. Just have a int flag[1000]; array and when you collect a flag flag[cloneindex] = 1;
and when created if(flag[cloneindex] == 1) DestroyActor("Event Actor"); you can you fread and fwrite to save the flag array and load it
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: How to store multiple boolean values in one int?

Postby bspeers » Fri Jun 22, 2012 1:07 pm

Okay, actually, that does make sense. I'll try that. I'm more familiar with arrays anyway.

EDIT: Tried it. Don't know why it didn't occur to me originally, it worked well. Didn't realize clones could be detected in that way. Really streamlines the scripting, thanks very much!
bspeers
 
Posts: 15
Joined: Thu Jun 21, 2012 2:18 am
Score: 0 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest