Advertisement

Creating a link between a script context and game entity

Started by August 17, 2004 09:15 AM
2 comments, last by Miracle Dog 20 years, 3 months ago
I want to be able to give a script context a pointer to a class, and then have that class passed to all the functions called in that script without the user having to pass it manually. so I can have an enemy script like this:

// spider.script
int main()
{
  if(get_health() < 10)
  {
    escape();
  }
  else
  {
    // attack!!
  }
}
where "get_health()" looks something like this:

int get_health(void *pointer_to_class)
{
  return pointer_to_class->health;
}
Then I can have one script for each type of enemy, then in my program loop through each game_entity and call the relevant module, passing a pointer to itself. I hope that made sense. If there's a way better way of doing it, let me know, but I want to keep it relatively high level and avoid using OOP style scripting...
my point of view solution is :

without OOP and keeping your script as this

in your game_entity loop, register the entity your are looping in
a singleton (or global variable if you prefer) :

for all my entities

GlobalSingleton::GetInstance()->SetActiveEntity(&theentity);
pCtx->Execute();

or

for all my entities

gpActiveEntity = &theentity
pCtx->Execute();


then in your get_health function modify the header to be

int get_health()

in the get_health core function call something like this :

return GlobalSingleton::GetInstance()->GetActiveEntity()->health

or like that (no singleton)
return gpActiveEntity->health;

Hope it can help.
Advertisement
AngelScript itself doesn't have any way of passing hidden variables.

If you want your scripts to look exactly like you said, then what abrken said would be the best solution.

Otherwise I don't think it would be too much to ask of the script writers to pass a pointer to the entity between functions, whether you do this with C style functions or object methods. In my opinion it is not the syntax that makes OOP difficult to beginners, it is the concept of creating object and relationships through inheritance, or aggregation or whatever you'd like. Just using existing objects and methods are no more difficult than normal procedural languages.

[Edited by - WitchLord on August 18, 2004 7:02:07 AM]

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

Thanks for your replies. I tried what abrken said, it worked until I tried calling a script from within a script (because the pointer would change halfway through, once the parent script regained control it wouldn't have the correct pointer). So I decided to rethink my implementation, and pass around pointers.

This topic is closed to new replies.

Advertisement