
Senior Member
How to make combinable objects
This question is not specific to a game, I want to know what is the best way to make combinable objects.
Say I have 3 objects A,B,C, if i combine
A+B = AB
A+C = AC
B+C = BC
But if I had 4 objects, the number of possible combinations increase even more( limited to 2 objects per combinations)! Now what if I had 50 objects? The number of combinations possible just goes beyond what I can hardcode in the game, so is there any possible formula such that the combinations are automatically calculated? This migh be really simple, but i can't think of a very innovative way to handle this....please help!
If you like me, add me to your friends list .
PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!
My Arcade My Blog
Add me on twitter:

FK'n_dog
can you use this method (testing with an array of "strings" in AS2)
PHP Code:
tmp = [];
arr = ["A","B","C","D","E","F"];
len = arr.length;
for(var n=0;n!=len;n++){
for(var m=0;m!=len;m++){
if(arr[n]!=arr[m]){
tmp.push(arr[n]+arr[m]);
}
}
}
trace(tmp);
/*  output 
AB,AC,AD,AE,AF,
BA,BC,BD,BE,BF,
CA,CB,CD,CE,CF,
DA,DB,DC,DE,DF,
EA,EB,EC,ED,EF,
FA,FB,FC,FD,FE
*/

Senior Member
hmm, thanks, but what about the duplicates? AB is same as BA!
If you like me, add me to your friends list .
PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!
My Arcade My Blog
Add me on twitter:

Pumpkin Carving 2008
Code:
tmp = [];
arr = ["A","B","C","D","E","F"];
len = arr.length;
for(var n=0;n!=len;n++){
for(var m=n;m!=len;m++){
if(arr[n]!=arr[m]){
tmp.push(arr[n]+arr[m]);
}
}
}
trace(tmp);
/*
AB,AC,AD,AE,
AF,BC,BD,BE,
BF,CD,CE,CF,
DE,DF,EF
*/
Start the inner loop at the outer loops index.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i72600K QuadCore Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6

FK'n_dog
Originally Posted by ImprisonedPride
/*
AB,AC,AD,AE,
AF,BC,BD,BE,
BF,CD,CE,CF,
DE,DF,EF
*/
Start the inner loop at the outer loops index.
good catch
though in theory AB != BA
Last edited by a_modified_dog; 09092009 at 05:22 PM.

Senior Member
Thanks guys! The first part is solved!
Next is how should I define properties to combine, but that won't be hard after this!
If you like me, add me to your friends list .
PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!
My Arcade My Blog
Add me on twitter:

Pumpkin Carving 2008
Code:
var objA:Object = {n:"A", x:1, y:2, z:3};
var objB:Object = {n:"B", x:3, y:1, z:2};
var objC:Object = {n:"C", x:4, y:1, z:3};
var objD:Object = {n:"D", x:0, y:3, z:1};
var objE:Object = {n:"E", x:2, y:1, z:2};
Object.prototype.toString = function():String {
var out:String = "{";
for (var i in this) {
out +=i + ":" + this[i] +",";
}
out = out.substr(0, out.length1);
out += "};";
return out;
}
function run():Void {
tmp = [];
arr = [objA, objB, objC, objD, objE];
len = arr.length;
for(var n=0;n!=len;n++){
for(var m=n;m!=len;m++){
if(arr[n]!=arr[m]){
tmp.push(combineObjects(arr[n], arr[m]));
}
}
}
trace(tmp);
}
function combineObjects(a:Object, b:Object):Object {
var out:Object = {};
for (var i in a) {
out[i] = a[i]+b[i];
}
return out;
}
run();
/*
{z:5,y:3,x:4,n:AB};,
{z:6,y:3,x:5,n:AC};,
{z:4,y:5,x:1,n:AD};,
{z:5,y:3,x:3,n:AE};,
{z:5,y:2,x:7,n:BC};,
{z:3,y:4,x:3,n:BD};,
{z:4,y:2,x:5,n:BE};,
{z:4,y:4,x:4,n:CD};,
{z:5,y:2,x:6,n:CE};,
{z:3,y:4,x:2,n:DE};
*/
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i72600K QuadCore Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6

Senior Member
Thanks, but I didn't exactly mean that kind of properties! Anyway, I guess this solves this entire matter. Thanks again!
If you like me, add me to your friends list .
PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!
My Arcade My Blog
Add me on twitter:

Pumpkin Carving 2008
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i72600K QuadCore Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6

Senior Member
umm, by properties I meant a set of rules that define an object....when two objects combine these rules or behaviours come together to create a new object!
Like say, i define a "plank" as something which is solid and on which you can stand upon, and a "wheel" as something which can move in a direction! When we combine these two we get a skateboard(sort of).... meaning a movable plank on which the player can stand!
So this is kindof like what you did, but with a set of rules, instead of simple attributes!
If you like me, add me to your friends list .
PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!
My Arcade My Blog
Add me on twitter:

you can represent an object as a bitarray and use bitwise logic to combine attributes. so, let's say bit 1 is whether you can stand on an object, bit 2 is whether it moves left and bit 3 is whether it moves right.
001 //plank
110 //wheels
now a bitwise OR defines their combination of properties
111 //plank + wheels (001  110)
so let's say you're only going to have 32 different properties, then you can forget about the hassle of writing a bitarray class and just use uints. this same example, ActionScript looks like this:
Code:
const STANDABLE:uint = 1; //001
const MOVES_LEFT:uint = 1 << 1; //010
const MOVES_RIGHT:uint = 1 << 2; //100
var plank:uint;
var wheels:uint;
//to illustrate setting the bits
plank = STANDABLE; //001
wheels = MOVES_LEFT  MOVES_RIGHT; //110
var skateboard:uint = combine( plank, wheels ); //111
var canMoveLeft:Boolean = hasAttribute( skateboard, MOVES_LEFT ); //true!
public function combine( obj1:uint, obj2:uint ) : uint
{
return obj1  obj2;
}
public function hasAttribute( object:uint, attribute:uint ) : Boolean
{
return ( object & attribute ) == attribute;
}
Last edited by newblack; 09092009 at 06:49 PM.

Senior Member
Yeh, I am planning on using the same approach( i actually found this two days ago while looking up uses of bitwise operators).....but thanks for the example code!
It gets a little more difficult when you have a few opposing properties though, and then there's a thing about prioritization!
If you like me, add me to your friends list .
PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!
My Arcade My Blog
Add me on twitter:

it's not really any more complicated in those cases. for mutually exclusive attributes, you store a bit mask and since you're working with uints, priority can be handled natively by the value of the attribute.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
