User:Ccw: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Proposed solution to issue 4377 updated)
No edit summary
Line 3: Line 3:




=Some class constructors and destructors are too complex=
=Proposed Lua additions=
==Proposed solution to [issue http://bugs.mtasa.com/view.php?id=4377]==
Move code out of constructors and destructors into functions that are called post-construction and pre-destruction respectively.
==Example of solution when applied to CClientEntity and CClientColModel:==


Current base and concrete class declaration:
==Client and server function==
<syntaxhighlight lang="lua">
===setElementFrozen ( element, bool )===
class CClientEntity
To work with vehicles, peds and dynamic objects like barrels and what not.
{
public:
                                    CClientEntity          ( ElementID ID );
    virtual                        ~CClientEntity          ( void );
...


 
==Server event==
class CClientColModel: public CClientEntity
===onSettingChange ( name, oldValue, newValue )===
{
So the resource can take action if one of its settings get changed. Could even cancel the change I suppose.
public:
                                    CClientColModel        ( CClientManager* pManager, ElementID ID );
                                    ~CClientColModel        ( void );
...
</syntaxhighlight>
 
 
Proposed base class declaration and definition:
<syntaxhighlight lang="lua">
class CClientEntity
{
protected:
                                    CClientEntity          ( void ) {}
                                  ~CClientEntity          ( void ) {}
    void                            PostConstruct          ( ElementID ID );
    virtual void                    PreDestroy              ( void );
public:
    void                            Delete                  ( void );
...
 
 
void CClientEntity::PostConstruct ( ElementID ID )
{
    // Code from CClientEntity constructor moved here
}
 
void CClientEntity::PreDestroy ()
{
    // Code from CClientEntity destructor moved here
}
 
void CClientEntity::Delete ( void )
{
    PreDestroy ();
    delete this;
}
</syntaxhighlight>
 
 
 
 
Proposed concrete class declaration and definition:
<syntaxhighlight lang="lua">
class CClientColModel: public CClientEntity
{
protected:
                                    CClientColModel        ( void ) {}
                                    ~CClientColModel        ( void ) {}
    void                            PostConstruct          ( CClientManager* pManager, ElementID ID );
    virtual void                    PreDestroy              ( void );
public:
    static CClientColModel*        NewCClientColModel      ( CClientManager* pManager, ElementID ID );
...
 
 
void CClientColModel::PostConstruct ( CClientManager* pManager, ElementID ID )
{
    CClientEntity::PostConstruct ( ID );
    // Code from CClientColModel constructor moved here
}
 
void CClientColModel::PreDestroy ()
{
    // Code from CClientColModel destructor moved here
    __super::PreDestroy ();
}
 
CClientColModel* CClientColModel::NewCClientColModel ( CClientManager* pManager, ElementID ID )
{
    CClientColModel* pElement = new CClientColModel ();
    pElement->PostConstruct ( pManager, ID )
    return pElement;
}
</syntaxhighlight>
 
 
 
===Other changes required===
Any occurrence of
<syntaxhighlight lang="lua">
new CClientColModel ( pStreamer, ID );
delete pElement;
</syntaxhighlight>
should be replaced with
<syntaxhighlight lang="lua">
CClientColModel::NewCClientColModel ( pStreamer, ID );
pElement->Delete ();
</syntaxhighlight>

Revision as of 21:55, 19 June 2009

Coder.gif This user is an MTA developer


Proposed Lua additions

Client and server function

setElementFrozen ( element, bool )

To work with vehicles, peds and dynamic objects like barrels and what not.

Server event

onSettingChange ( name, oldValue, newValue )

So the resource can take action if one of its settings get changed. Could even cancel the change I suppose.