Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


- - - - -

Creating a link between a script context and game entity


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Miracle Dog   Members   -  Reputation: 122

Like
0Likes
Like

Posted 17 August 2004 - 03:15 AM

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...

Sponsor:

#2 abrken   Members   -  Reputation: 130

Like
0Likes
Like

Posted 17 August 2004 - 03:46 AM

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.

#3 Andreas Jonsson   Moderators   -  Reputation: 3414

Like
0Likes
Like

Posted 17 August 2004 - 07:02 AM

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

#4 Miracle Dog   Members   -  Reputation: 122

Like
0Likes
Like

Posted 17 August 2004 - 03:53 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS