# using const members in classes

## Recommended Posts

As i've mentioned a few times, the objects in my games are code wrapped in an AngelScript class like below:
class MyClass
{
float x;
float y;
float depth;
[code here]
}
I would like to add one more member just under depth called "id" but i only want it to be read-only (and use "objectData->GetPropertyPointer()" to get its pointer and change its value). The value represents the ID of the NPC and can be used in some functions. I tried defining it as "const uint id;" but it woudnt let me use const in a class like that. At the moment i have to use a function called GetId() but i would much rather have "const id". Heres what an NPC file looks like:
WIDTH 32
HEIGHT 32

//Tiles for the bush
TILEBLOCK 2 2
0x0200 0x0300
0x0201 0x0301
END

CLIENTCODE
int sprite;
int[] tiles;

void OnCreate()
{
tiles = GetNpcTiles(id);
depth = 0xFFFF;
}

void OnDraw()
{
DrawTiles(tiles, x, y, 2, 2);
}

{
}

{

DrawTiles(tiles, x, y, 2, 2);
}

bool OnPlayerCollision(int playerId)
{
//return true to indicate that this will block the player
return true;
}

bool OnCarry(int playerId)
{
//return true to indicate that this can be carried by the player
return true;
}

bool OnWarpCollision(const string& level, float nextX, float nextY)
{
//If npc touches a warp rectangle than
//change its level, x and y
WarpNpc(id, level, nextX, nextY);
return true;
}
END

SERVERCODE
//server-side equivelant goes here
END


##### Share on other sites
Well i figured out a way around it. I register a global called "const uint id" and i set its value just before i call an npc event.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628314
• Total Posts
2982021

• 9
• 9
• 13
• 11
• 14