Sign in to follow this  

How are new objects created in games?

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am stuck with this. I have seen in some games for example in RTS games when we create a soldier I am sure that all the objects will not be written in the program. So I want to create an object when the program is running. How do I accomplish this? Is there any way I can create an object when the program is running?

Share this post


Link to post
Share on other sites
This depends on your language, but in most of them the creation of a new object and its addition to the game will look like:

register_object_in_game (new type_of_object(arguments))

Share this post


Link to post
Share on other sites
I've never actually programmed an RTS, so I may be wrong. However, I'd approach it likes this:


CLASS : Feature
Stats all non-static features will share.

CLASS : Controllable (inherits feature)
Stats all player-controllable features will share.

CLASS : Unit (inherits controllable)
Stats all units will share.

CLASS : Sub-Class (inherits unit)
May be best, depending on the game. Example would be 'flying'.

CLASS : SpecificUnit(inherits sub-class)
The specific unit's max stats, special powers, etc.

Then I'd create an instance of said unit when necessary.

Share this post


Link to post
Share on other sites
ToohrVyk & rip-off are right on. Once you've defined the object in code, you should simply be able to create a new instance of that object and store it in whatever container is managing your objects.

On a side-note, that's one heck of an inheritance tree. You might consider looking at a component-based approach to simplfy that one. Remember, specialize data, not code wherever possible ;)

Share this post


Link to post
Share on other sites
This is a very simple way of doing it:

In code, you have a UNIT class. This has all the characteristic EVERY unit must have in the game. This is a basic example:

[source lang=C#]
Class Unit
{
int health;
int armour;
int damage;
float speed;
float range;
bool canMoveOnWater;
}



Then, in a data file (text or encoded) you have something like:



[Axeman]
health=100
armour=1
damage=4
speed=3
range=0
canMoveOnWater=False

[Archer]
health=60
armour=0
damage=5
speed=4
range=6
canMoveOnWater=True

[FishingBoat]
health=120
armour=0
damage=0
speed=6
range=0
canMoveOnWater=True




You load these in:

[source lang=C#]

UnitTypes.Add(new UnitType("Axeman", 100, 1, 3, 4, 0, false));




Then, in your program's code, when a unit is created, something like:

[source lang=C#]

units.Add(new Unit("Axeman"));




If you want more details on what UnitTypes and Unit do, how they differ, how to load the data etc., just PM me.

Share this post


Link to post
Share on other sites
Oops.. sorry I forgot to mention the language. I am learning c++. I just came across this and I had been searching for a solution. Anyway I thank all of you for your help..
and NickHighIQ does it work the same way in c++ also??

Share this post


Link to post
Share on other sites
It works the same in any language that is OO works in this way, the only difference is the actual code (the concept remains the same).

OK, I'm not the one to be asking about C++ code, so you'll have to do some research yourself. Forgive me if my comments seem like they're 'babying' you, I'm not sure how proficient you are, so I'm doing my best to help. This is the way I would do it in C# (this is NOT entirely C#, it's a little C#/pseudo-code cross-breed. Hopefully this won't bother you because you're learning C++):



//caution! Bad OO here
struct UnitData
{
public int health;
public int damage;
public string name;
}

//OK, so here we have the UnitTypes Class.
//It loads and stores each type of unit
//(in and array of our UnitData struct)
//so we can basically copy/paste into
//a new class (a Unit class) to use to update
//and draw. There is only one UnitTypes class,
//not 100% sure if it fits, but look up "Singletons".

public static class UnitTypes
{
//List of units, look up 'arrays'
UnitData[] unitTypes;

//This method loads the units from a file
//that exists at the path given
public static void LoadUnits(string path)
{
//(simplified for clarity)
//you don't need to see exactly
//how it's done because C++ is different
f = OpenFile(path)

while (!f.EOF) //while we're not at the End Of the File
{
UnitData newUnit = new UnitData(); //create a new, empty UnitData

newUnit.health = f.ReadInt();
newUnit.damage = f.ReadInt();
newUnit.name = f.ReadString();

unitTypes.Add(newUnit);
}

//in the loop we added all the units from our data file (the on
//from my last post)
}


//We call this method when we want
//to create a new unit to place in the game
public static void CreateNew(string name)
{
//here you would search through your list
//to find the one with the name passed to
//this function, and then return it:

return unitTypes.SearchByName(name); //absolute crud, I made that SearchByName thing up
}
}

//The actual unit class. We draw this in game.

public class Unit
{
//we've already stored all this unit's data
//somewhere else, so why double up?
//CAUTION: To achieve this memory gain in C++, you need pointers!!
//look up pointers!
UnitData myType;

//the health this unit has left.
int health

//Constructor
public Unit(string name)
{
//we take the name given to the constructor and forward it on
//so we get this unit's data back.
myType = UnitTypes.CreateNew(name);
}

public void Update();
// I leave these up to you.
public void Render();
}




If you still don't understand, and have a copy of the C# part of Visual Studio, I can send you a heavily commented, simple, working version if you would like.

Share this post


Link to post
Share on other sites

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this