Advertisement

Context specific variables

Started by April 22, 2012 03:00 PM
1 comment, last by saejox 12 years, 7 months ago
Hi,

I'm in the progress of exposing Npc ai routines to AngelScript.
Every npc is created/destroyed by the game engine and has its own ai context.

i want to do something like this (AngelScript code)


float lastSeenPlayer = 0;
void BasicMeleeAggressive(Npc @npc)
{
PatrolLeftAndRight(npc);
lastSeenPlayer += TimeSinceLastFrame();

if(npc.IsPlayerInLOS(10))
{
lastSeenPlayer = 0;
}

if(lastSeenPlayer < 5)
{
npc.GoTo(pc.GetXPosition(), pc.GetYPosition());
}

}


problem is, more than one npc object call this script. I have read in manual that lastSeenPlayer is global between contexts.

ofcourse i can add something like this to npc object

unordered_map<string, float> scriptFloats;
float GetFloat(const string &varname)
{
...
}
void SetFloat(const string &varname, float value)
{
...
}


passing a string is not really ideal for performance.

Is there a way to let each context own their global variables?

Thank you
No, at least not yet.

The context only holds the stack for a script execution, i.e. local variables inside functions. All global variables are stored in the module, thus shared between all executions of scripts from that module.

To have unique variables for each NPC you can use script classes. Each NPC would then have its own instance of the script class.


class NpcController
{
float lastSeenPlayer = 0;

void BasicMeleeAggressive(Npc @npc)
{
PatrolLeftAndRight(npc);
lastSeenPlayer += TimeSinceLastFrame();

if(npc.IsPlayerInLOS(10))
{
lastSeenPlayer = 0;
}

if(lastSeenPlayer < 5)
{
npc.GoTo(pc.GetXPosition(), pc.GetYPosition());
}
}
}


The game sample in the SDK gives a complete example of how this can be done.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
That will work :)

thank you.

This topic is closed to new replies.

Advertisement