• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

How in the world to code breeding and genetics?

Status
Not open for further replies.

Ciez17

Member
Hello! I find this sort of topic hard to find when it comes to coding, and was wondering if anyone knew the basics to coding this. For example, say I had a monster breeding game and wanted genetics to determine which creature gets which parts, and traits from their parents. I figure it involves some sort of randomizer. But how would that be coded?
 

hogwater

Member
Well, I guess each monster would have a table listing all of the possible traits or parts that it could potentially pass on. Then each time it breeds you would give each trait a certain chance to be passed on.
 
Last edited:

Ciez17

Member
I understand, but do you know any examples of how that could be coded in GameMaker? To be honest I'm a complete newb with coding in general, but I wanna make some goals.
 
T

Thunder Lion

Guest
Learn about variables and arrays. Check out the game maker user Manual and search it, I learn a lot from it.
 
A

AnomalousArcane

Guest
As thunder lion said, I would look into arrays. You could probably achieve something with a 2 dimensional array and a bit of planning. Is the genetic information coming in a stat form? or as a sprite, like an arm or a tail?
 
M

MishMash

Guest
Systems like this are more about design than the raw programming implementation. First, take a step back and start thinking about the problem on paper. Design out how YOU would want a genetics system to work. For example, as hogwater suggested, perhaps the design is based on each creature having a list of traits. If this is the case, think about what sort of traits exist, how those traits could combine (for example, is it random chance that determines which traits are inherited, or does it pick a dominent/best one?

Alternatively, you can thin of how this information is represented, creating something more abstract by simulating a DNA style of thing. For example having a random string of characters, and subsequently deriving traits from patterns or sections in that string. Then, designing a scheme for merging two parent schematics.

All of this planning should be done on paper first, before you even think about how to program it. The same premise can be applied to any new problem that you come across. You may find that you don't know your way around GameMaker that well yet, or even if you tackle a brand new programming language, you may not know how best to achieve something there, however this does not take away from your ability to structure and design a plan for how things might work on paper :) If you do this enough, you will quickly find that coming up with a design for a system gets easier, and then comparatively implementing it in code isn't as hard once you know what you are working towards.

This game design section of the forums is a good place to discuss these ideas more abstractly, but there's no need to think about the code yet, first, lets discuss ideas and designs for how something like this could work! Once you have come up with a plan, you can then start asking questions such as:
- What is the best way to give creatures a list of traits in gamemaker?
- How do I manipulate strings?

These will often be quite simple programming knowledge questions with simple solutions! Most of how you go about implementing systems is just about understanding what datastructures are available, you have a huge pool of resources to choose from. To give you a quick break down, as this may help generally, i'll run through a few I use every day:

- Arrays: Great for storing lots of items of similar data. An array is basically a list of values which can be accessed with an index offset: score[0] = 10, score[1] = 20, you can then access a specific score by putting an expression inside the square brackets: score[player] = 10; (Where player is either 0 or 1 depending on which player it is).

- Lists: Great for keeping a concise set of information where elements are added or removed more frequently. For example, implementing a deck of cards could use a list of cards still remaining in the deck. Once a card is removed, it is deleted from the list.

- Maps: A map is a very versatile datastructure which "maps" an identifer to a value. This is often a string/text identifier. A map is great for something like a trait system, as you could do something like: map[? "Speed"] = 1.0; You can also check if something has a given element in a map using the ds_map_exists function. This is ideal for systems where some creatures may have special traits or properties, but others don't. For example, one creature of the same breed may have the "Timid" property, but another may not. A map is a great way of structuring information like this.

You'll also need to get a reasonable level of confidence with regular variables, and general ideas to do with instances having different properties/variables associated with them, and how using variables in place of values throughout an objects code allows it to be reactive to changes in its genetic signature.

So takeaway points here are to separate the design of the problem/game-mechanic away from the programming implementation, and only once you have worked out the details of what needs to happen, should you start worrying about the implementation in code.

Good luck with it :)
 

Rob

Member
If you want something simple like if A and B breed they get C, A & C get D, B & C get E for example, create an excel sheet and work out which monster breeding with which monster creates X monster e.g.

Code:
//Example Excel Sheet
   A  B  C  D  E
A  x  C  D  E  ?
B  C  x  ?  ?  ?
C  D  ?  x  ?  ?
D  E  ?  ?  x  ?
E  ?  ?  ?  ?  x
You'd code it with an array (that's what I did anyway) which you'd reference for breeding.

The excel sheet will help you immensely here so you don't forget what monster creates which.

Code:
//Example Breeding array

//A + A = A - (Trump + Trump = Trump)
a_breeding[0,0] = 0;

//A + B = C - (Trump + Blair = 50 Cent)
a_breeding[0,1] = 2;

//A + C = D - (Trump + 50 Cent = Paris Hilton??)
a_breeding[0,2] = 3;
Etc. If you're not comfortable with arrays I'd highly recommend familiarizing yourself with them asap because that kind of game LOVES arrays, with all the stats that are involved. It would be better to also get used to Data Structures which look more complicated than arrays but have some useful functions that will save you working things out yourself (like shuffling data and randomly picking one or finding the strongest/weakest stat).

This is one way to determine what the child gets from the parents. Each new monster already has traits/stats that you've predetermined and all the player has to do is cross breed the right monsters to find out.

You can make it more complicated so that random skills/traits are included but the above code is a good place to start imo.
 
Last edited:

Ciez17

Member
Thank you so much everyone for your advice so far! :D I am planning on utilizing both traits and sprites, but I kind of wanted to do sprites first since they could be visible if I could only have one or the other. Btw, which is the easiest for a beginner to start on?

Thanks again!
 
Z

zircher

Guest
Another way to look at it is to have an array of critter IDs and attributes. You then have a function that takes two attributes and breeds them producing a new set of variables. So Critter ID is one index of the array and the other index represents the attributes of the animal; strrength, dexterity, intelligence, sprite ID for head, eyes, face, body, body color, etc. When you go to breed them, it would look something like attritbute[newID][0] = ((attribute[ID1][0] + attribute[ID2][0]) /2)+plus a random value for numbers like strength, for visual attributes like eye (say that's the 5th attribute) you would instead pick either the eye sprite number of attribute[ID1][5] or attribute[id2][5] with a small chance of a random eye color/type and assign that to attribute[newID][5]. So, each critter's 'DNA' is just a string of numbers in an array with a custom function to mix them together. Clear as mud?
.
 

Ciez17

Member
That's precisely what I'm going for in my game. You know the classic four-squared mdel with the lowercase and uppercase letters to represent dominant and recessive genes? I wanted a system like that, where some traits may have of chance of happening or even showing down the line of a family tree. I'll do what MishMash suggested and have it on paper first.

I've looked up some tutorials knvolving arrays, but I guess I need a better explanation of how a game utilizes them. Are they essentially shortcuts that make reading a script easier?
 

Yal

šŸ§ *penguin noises*
GMC Elder
I've looked up some tutorials knvolving arrays, but I guess I need a better explanation of how a game utilizes them. Are they essentially shortcuts that make reading a script easier?
Arrays lets you store data in a structure that's easy to loop over for batch operations. They have lots of uses. (and for even more advanced stuff, there's Data Structures like queues and stacks, but you can do lots of stuff just knowing arrays)
 

Ciez17

Member
Ah, so is it safe to assume that it works like a copy machine then? I have an impression from watching tutorial vids that it helps when you want to use a certain action/command in multiple places multiple times.
 
Z

zircher

Guest
Arrays are just data storage like a spread sheet grid. It's totally static until you start manipulating the data (in loops as Yal mentioned or in comparison functions as I suggested.)
 
S

Storyteller

Guest
Arrays lets you store data in a structure that's easy to loop over for batch operations. They have lots of uses. (and for even more advanced stuff, there's Data Structures like queues and stacks, but you can do lots of stuff just knowing arrays)
most advanced data structures use 'array' or 'arratlist' as their underlying structure with additional functionality added on top of that. not all, but most.

consider trees as a choice for your data structure....

that said, what you need is a heuristic or other weighting algorithm to determine what genes might be inherited from one generation to the next.
this is really where your nuance and mutation will likely come from.

you might look into binary 'flags' as genes and logic structures such as AND, OR and XOR for combining two sets of binary 'genetic flag lists' into a new one, then possibly modifying it through various filters or bitshifts of sections. You could use a floating point number to determine the binary flags based on a certain threshold or floor/ceiling functions.

a lot of options here with different ways of doing things to explore. keep us posted, Im interested in what you come up with
 
M

MadZenno

Guest
I know it's not GM related but you could see how it's done in Mincraft mods do it and follow those who already posted here.
 

Ciez17

Member
I think I FINALLY got a hold of the function of arrays! Sorry that I haven't posted in a while, college and other things had to come first. But now I'm back!
 
Last edited:
T

Timze

Guest
If you're idea is more along the lines of jade cocoon
stats1 + stats2 /2 = new monster stats
monster 1 head get x, y, z, length & width + monster 2 head get x, y, z, head length & width /2 = new head length width xyz.
Repeat for all body parts. (pseudo code)
concept should work for the mesh. When it comes to skinning this would be a separate file which is layed over the mesh which could be randomized. with % chance new monster inherits parent 1 or 2 skin. (head, eyes, body, arms, legs, etc)

If it's more along the lines of monster rancher 2, then you don't have to mess with the lengths and widths of body parts so much just the skinning. Monster 1 (frame data) monster 2 (skin data) = monster 3 (monster 1 frame data and monster 2 skin data) May require some alterations or even a reskinning of each possibility for the monster.
 
Last edited by a moderator:

Ciez17

Member
Another way to look at it is to have an array of critter IDs and attributes. You then have a function that takes two attributes and breeds them producing a new set of variables. So Critter ID is one index of the array and the other index represents the attributes of the animal; strrength, dexterity, intelligence, sprite ID for head, eyes, face, body, body color, etc. When you go to breed them, it would look something like attritbute[newID][0] = ((attribute[ID1][0] + attribute[ID2][0]) /2)+plus a random value for numbers like strength, for visual attributes like eye (say that's the 5th attribute) you would instead pick either the eye sprite number of attribute[ID1][5] or attribute[id2][5] with a small chance of a random eye color/type and assign that to attribute[newID][5]. So, each critter's 'DNA' is just a string of numbers in an array with a custom function to mix them together. Clear as mud?
.
most advanced data structures use 'array' or 'arratlist' as their underlying structure with additional functionality added on top of that. not all, but most.

consider trees as a choice for your data structure....

that said, what you need is a heuristic or other weighting algorithm to determine what genes might be inherited from one generation to the next.
this is really where your nuance and mutation will likely come from.

you might look into binary 'flags' as genes and logic structures such as AND, OR and XOR for combining two sets of binary 'genetic flag lists' into a new one, then possibly modifying it through various filters or bitshifts of sections. You could use a floating point number to determine the binary flags based on a certain threshold or floor/ceiling functions.

a lot of options here with different ways of doing things to explore. keep us posted, Im interested in what you come up with
If you're idea is more along the lines of jade cocoon
stats1 + stats2 /2 = new monster stats
monster 1 head get x, y, z, length & width + monster 2 head get x, y, z, head length & width /2 = new head length width xyz.
Repeat for all body parts. (pseudo code)
concept should work for the mesh. When it comes to skinning this would be a separate file which is layed over the mesh which could be randomized. with % chance new monster inherits parent 1 or 2 skin. (head, eyes, body, arms, legs, etc)

If it's more along the lines of monster rancher 2, then you don't have to mess with the lengths and widths of body parts so much just the skinning. Monster 1 (frame data) monster 2 (skin data) = monster 3 (monster 1 frame data and monster 2 skin data) May require some alterations or even a reskinning of each possibility for the monster.
I particularly like these suggestions. It's just a matter of beginning. Like I said earlier, I'll eventually post a code of what info I'm gathering. Still appreciate any further help to make this even more understandable!
 
Status
Not open for further replies.
Top