Create a cwdtring array with an int

keldath

LivE LonG AnD PrOsPeR
Joined
Dec 20, 2005
Messages
7,367
Location
israel
Hi

Im trying to create an array in game.cpp, or globals,
That will its key is a string and value an int.

To be specific:
M_eventtype* = new cwstring [gc.getnumeventvtriggertypes()[

Cwstring* m_eventtype

In the init and
For (inr i : < gc.getnumeventtriggertypes(); i++)
,}
Cwstring thename = gettriggerinfo((eventtriggettipe)i).gettype();
If Code code....
M_eventtype[thename] = (eventtriggertypes)i)
{


But i get an error on cwstring and char* something. Tried a few combinations.

Anyone got some directions on that for me?

Thanks you in advance.
 
Im trying to create an array in game.cpp, or globals,
That will its key is a string and value an int.
An array has an int as key so what you are doing can't be done, at least not precisely how you write it. You will likely want a has table instead as that will allow storing ints with key access. Luckily we were informed on how to do that with fast code recently.
PHP:
Cwstring* M_eventtype = new cwstring [gc.getnumeventvtriggertypes()];
That is strings stored with int access, not the other way around. Also to do something where constructor and deconstructors are needed, use vectors, not pointers.

PHP:
Cwstring  thename = gettriggerinfo((eventtriggettipe)i).gettype();
If  Code code....
    M_eventtype[thename] = (eventtriggertypes)i)
Last line fails because M_eventtype expects an int in [] and a string right of =.
 
hey,

thanks!
i experimented quite a bit, tried with vectors also.
ran into issues with the int. now i know,
ill try the hash (i missed it there in the thread).

eventually, i just created an array of index and the event index and used a loop over another cached tag i added that stores how many items exists.

than you.

edit:

i need to set this also right?
SAFE_DELETE_ARRAY(
or .reset?
 
Last edited:
hey,

thanks!
i experimented quite a bit, tried with vectors also.
ran into issues with the int. now i know,
ill try the hash (i missed it there in the thread).

eventually, i just created an array of index and the event index and used a loop over another cached tag i added that stores how many items exists.

than you.

edit:

i need to set this also right?
SAFE_DELETE_ARRAY(
or .reset?
In my opinion, if you new a object in Class such as

CvUnit::init() {
Cwstring* M_eventtype = new cwstring [gc.getnumeventvtriggertypes()];
}

you should delete it in CvUnit::iuninit() funcion OR it will cause memory leak and lead to crash.

it is a rule of RAII in CPP

if you do not want to destroy it , you should set it as global var. However it will cost more RAM.

A better way is use std::shared_ptr or std::unique_ptr . But it is not posiible in C++ 2003.
 

mediv01 hi,​


A better way is use std::shared_ptr or std::unique_ptr . But it is not posiible in C++ 2003.
humm so it wont work then.

i added save delete on the uninit / reset.
thanks.

adding more global var? as in extent as Nighttingale suggested?
 
here are the code parts from the relative functions in game.cpp

Spoiler :

Code:
CvGame::CvGame() :
//doto special events
    m_aeSpecialEvents = new int[2]; // expected 2 events. gotta change it if im gonna add more exists in other places!
//doto special events   
    reset(NO_HANDICAP, true);
}

void CvGame::setInitialItems()
   
//doto special events - find the special events
    setSpecialEvents();
//doto special events

void CvGame::uninit()
{
//doto special events
    SAFE_DELETE_ARRAY(m_aeSpecialEvents);
//doto special events

void CvGame::reset(HandicapTypes eHandicap, bool bConstructorCall)
//doto special events -reset the list
    numSpecialEvents = 0;
    m_aeSpecialEvents = new int[2];
//    for (int iI = 0; iI < 2; iI++)
//    {
//        m_aiBonusThatArePrereqForUnits[iI] = 0;
//    }
//doto special events
    setSpecialEvents();
//doto special events


void CvGame::read(FDataStreamBase* pStream)
//doto special events
    pStream->Read(2, m_aeSpecialEvents);
//doto special events   

void CvGame::write(FDataStreamBase* pStream)
{
//doto special events
    pStream->Write(2, m_aeSpecialEvents);
//doto special events


void CvGame::onAllGameDataRead()
{
//doto special events - find the special events
    setSpecialEvents()
//doto special events - find the special events


the logic func:
Spoiler :

Code:
void CvGame::setSpecialEvents()
{
    //doto special events - find the special events
    int bPartisanFound = false;
    int bPalaceFound = false;
    int idx = 0; 
    //set the index of the found event, its value will be the index of the wanted event in the global events
    //list, which will then be used to trigger this event.
    for (int actualEventIds = 0; actualEventIds < GC.getNumEventTriggerInfos(); ++actualEventIds)
    {
        CvEventTriggerInfo& kTrigger = GC.getEventTriggerInfo((EventTriggerTypes)actualEventIds);
        CvString eventName = kTrigger.getType();
        if (eventName == CvString(L"EVENTTRIGGER_PARTISANS"))
        {
            m_aeSpecialEvents[idx] = actualEventIds;
            //set the event to active (its relevant mostly to partisans
            m_abInactiveTriggers.set((EventTriggerTypes)actualEventIds, true);
            numSpecialEvents += 1;
            bPartisanFound = true;
            idx++;
        }
        if (eventName == CvString(L"EVENTTRIGGER_PALACE_UPGRADE"))
        {
            m_aeSpecialEvents[idx] = actualEventIds;
            //set the event to active (its relevant mostly to partisans
            m_abInactiveTriggers.set((EventTriggerTypes)actualEventIds, true);
            numSpecialEvents += 1;
            bPalaceFound = true;
            idx++;
        }
        if (bPalaceFound && bPartisanFound)
            break;
    } 
}
 
PHP:
m_aeSpecialEvents = new int[2];
If you know at compile time that it is always 2, then you can write this in the header
PHP:
int m_SpecialEvents[2];
This makes it part of the class it is declared in rather than allocating memory outside the class. As a result it will be freed together with the class and you can't leak memory. It is also faster as it reduces memory I/O due to being able to read it directly rather than reading the address in the pointer and then looking up that address. However this only works if the size is known at compile time.
 
cool, than ,
having the size 2 is safe and fast :)
did i get it right?
Yes. New allocates memory elsewhere while declaring it without using new will put the memory where it is declared, just like int and whatever else variables will be part of the class itself rather than some remote memory. You can still make it unsafe through as it is your responsibility to make sure your arguments are within range. You can still get weird results from index -1 if it doesn't downright crash.
 
Top Bottom