1

Topic: [TUTORIAL] Coding: Handling entities

Hey, fellow Tees! How are you doing?
Today we have another tutorial: handling entities/TILES!

Note that this tutorial has been made and tested in Teeworlds 0.6.4!
If you haven't compiled your Teeworlds yet, see the Teeworlds help.

Let's start coding!

Go to your Teeworlds source, then go to src/game/server/entities.
Open character.h and character.cpp.

Use Ctrl + F and search for

class CPlayer *GetPlayer() { return m_pPlayer; }

Add

void HandleTiles(); 

after the code you searched.

Save the current file and now go to character.cpp.
Use Ctrl + F and search for

void CCharacter::ResetInput()

After the code you searched, add the following code:

void CCharacter::HandleTiles()
{

}

This won't do nothing at the moment. You must go first into collision.cpp file.
Use Ctrl + F and search for

m_pLayers = 0;

After the code you searched, add the following code:

m_pExtraTiles = 0;

This will result in a crash/bug, you must now go into collision.h file.
Use Ctrl + F and search for

class CTile *m_pTiles;

After the code you searched, add the following code:

class CTile *m_pExtraTiles;

Save both files: collision.h and collision.cpp.
After the code of the COLFLAGs, add the following code:

int IsTile(int x, int y, int Type);
int IsTile(vec2 Pos, int Type) { return IsTile(Pos.x, Pos.y, Type); }

Save the current file.
Go into mapitems.h and add your TILEs.

After TILE_NOHOOK, code, add the following code:

TILE_MYTILE=4,

Save all, we almost finish this.
Go into character.cpp.

Replace this code:

void CCharacter::HandleTiles()
{

}

With this one:


void CCharacter::HandleTiles()
{
if(GameServer()->Collision()->IsTile(m_Pos, TILE_MYTILE))
{
 if(Server()->Tick() % (1 * Server()->TickSpeed()) == 0)
   {
    GameServer()->SendBroadcast("              ~ Shop ~\nWrite \"/shop\" to buy something.", m_pPlayer->GetCID());
    }
  }
}

If players are on that entity, it'll show a broadcast to the player after 1 second!

When having more than one TILE, at the beginning, the first command should have an if.
However, the second TILE, the third and so on, shall have an else if at the beginning instead.

if(GameServer()->Collision()->IsTile(m_Pos, TILE_MYTILE))
{
 if(Server()->Tick() % (1 * Server()->TickSpeed()) == 0)
   {
    GameServer()->SendBroadcast("              ~ Shop ~\nWrite \"/shop\" to buy something.", m_pPlayer->GetCID());
    }
  }

else if(GameServer()->Collision()->IsTile(m_Pos, TILE_FREEZE_OUT))
{
     Unfreeze();
}

Entities are the same; you just need to place your new entity on entities.png, after where the rifle is.
Entities (Teeworlds 0.6.4)

To make detecting ranks like admin or moderator, you need to modify other files. We'll see this in another tutorial!
I hope you liked this tutorial. If you had any troubles understanding please tell me and I'll help you with your doubts. smile

Old Teeworlds ex-gamer | Teeworlds ex-tutorial maker

2

Re: [TUTORIAL] Coding: Handling entities

(Y)

Teeworlds [ friends ] clan
Some YouTube Stuff about Teeworlds