gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > How does the vtable work

#177139 - Azenris - Sat Dec 17, 2011 8:55 pm

I'm not sure I'm even asking the right question, but here goes.

Basically I have a bunch of items, they are binaries added to the .nds file. I have different items deriving from a base class CItem.

CItemExpGain : CItem
CItemPotion : CItem
etcetc

This is the code that registers an item - basically puts it into a list.

Code:
void CItemManager::Register(const void *pItem)
{
   CItem *pItemPtr = pItem;
   int itemID = pItemPtr->GetID();
   int listSize = m_items.size();
   
   if (itemID >= listSize)
   {
      m_items.resize(itemID + 1);
      for (int i = listSize; i < (itemID + 1); ++i)
         m_items[i] = NULL;
   }

   ASSERT(m_items[itemID] == NULL, "itemID already registered.");

   m_items[itemID] = pItemPtr;
}


I register at init with
Code:
Register(Potion_item);
Register(BigPotion_item);
Register(UltraPotion_item);


Not actual code, but for example I did
Code:

m_items[0]->Use();


It doesn't know whether thats an exp item or potion. I think from what I got at http://www.parashift.com/c++-faq-lite/virtual-functions.html a vtable ptr is added to the class. But I'm creating my binaries outside the program.

Can I achieve what I want or should I scrap this whole process. OR am I confused completely at what I'm doing :D
_________________
My Homebrew Games

#177140 - Dwedit - Sat Dec 17, 2011 9:27 pm

A vtable is a bunch of function pointers, so as long as your class is specified correctly in the header file, and the exact same header is used, things should work fine. The code which creates the object will put in the correct function pointers before your constructor executes.

What do you mean when you say "I am creating my binaries outside the program"?
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#177141 - Azenris - Sun Dec 18, 2011 12:20 am

A constructor isn't called. I wasn't creating any instances. The item themselves are constant anyway, they don't contain any data that would change.

If I created an instance for each item isn't that just using memory, reallocating data into RAM I already have.

What I meant was I had a folder of items:
example of a potion: Potion.itm
Code:

# simple potion item

type CItemPotion
id 75
name_english "Potion"
desc_english "This simple potion will heal you ~{c:%d}%d~{c:0} hp when used."
gfx "gfx_ItemGfx_Potion"
maxStack 10
sellValue 15
weight 0
heals 20


When I run the make file I have

Code:

# ---------------------------------------------------------------
# items
# ---------------------------------------------------------------
%.item : %.itm
   @rt_itemConv $< -o$@ -lenglish
%.item.o : %.item
   $(bin2o)


I was hoping I could get the output so that I could just cast at that location CItem and all the virtual functions work as normal.

But from what I read my classes will be automatically modified since they are virtual and require a vtable ptr. But because I create them outside the program where the vtable for that object is nobody knows.

I'm probably trying to do something I shouldn't. Maybe I will find a different way. Perhaps just allocate them in RAM with new having the vtptr set up automatically, even though that feels like I'm wasting memory.

Tell me if I'm overcomplicating a simple task, I do that sometimes!

BTW I used to just have a C++ file contain all my items, but I wanted to have an easily modifable textfile for items and not be a C++ file.

I sound crazy don't I :(
_________________
My Homebrew Games

#177142 - Dwedit - Sun Dec 18, 2011 2:48 am

Maybe change the tool to make C++ files instead of .o files?
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#177146 - Azenris - Mon Dec 19, 2011 6:22 pm

Thanks for the suggestions. I may go the route of allocating with new, but including the item through nitrofiles(?). This could also be advantages later if I want to keep certain items out of memory until I need them.

I could even move the .itm converter within the game and include those in the nitrofiles. Might slow down the game startup, but will speed up compile times and make it so easy to balance items, such as adjusting what they heal etc.

Anyway, ty.
_________________
My Homebrew Games