YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

mutiple scripts, same functions
THACO
#1 Posted : Thursday, September 01, 2005 6:43:27 PM(UTC)
Rank: Member

Groups: Registered
Joined: 8/30/2005(UTC)
Posts: 6

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
One of the things I want to do for my game is have my player class and other classes call a specific function say update(delta_time) from a script. There would be a different script for each different character that the player might be. This means I would have multiple scripts that share the same function names becuase my class in c++ would just want to call update(delta_time) from the script. Currently to get this working in each script i put the prefix of the character name infront so now the function names are essentially different. Then when an instance of the class is created i set a variable in it to the name of the class. Then to call the function I just add the prefix of the name to update so it calls the function based on the name of the character.

example

2 scripts
bob.nut

function bobupdate(time)
{
//do stuff here
}

carl.nut
function carlupdate(time)
{
//do stuff possibly different than bobs
}

Then in c++ the update function for the class looks like this

void Player::update()
{
//call a squire function
char update[64];
sprintf(update,"%supdate",name);

SquirrelObject root = SquirrelVM::GetRootTable(); // gets the global scope
SquirrelObject upd = root.GetValue(update); //fetches the function from the root

SquirrelVM::BeginCall(upd);
SquirrelVM::PushParam(1);
SquirrelObject retval = SquirrelVM::EndCall();

// do more stuff below

}

This works fine for what I want but seems like there should be something better?

-Matt

p.s. Also hopefully tomorrow I will post a small tutorial of some of the things I have been fidling with, just want to clean it up.

pps Also on the documentation at least the online one in the Create C function
sq_newclosure(v,f,0,0); //create a new function
sq_newclosure only takes 3 parameters not 4
Rick
#2 Posted : Thursday, September 01, 2005 7:03:53 PM(UTC)
Rank: Member

Groups: Registered
Joined: 8/30/2005(UTC)
Posts: 26

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Why not make Update() be a function defined during runtime?

class ScriptPlayer extends Player
{
function Update()
{
}
}

Then it is contained within the class and you can easily access this. etc.
John Schultz
#3 Posted : Thursday, September 01, 2005 7:25:45 PM(UTC)
Rank: Member

Groups: Registered
Joined: 6/24/2005(UTC)
Posts: 241

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
It would appear you could do:



struct Player {

<span id="_ctl0_PostForm_Reply">  SquirrelObject upd; // Should be properly reference counted. Not clear what happens when reseting the function definition via script: new functionality? Error? Setting the table/slot entry to a non-function should also be possible, whereby a BeginCall() would then fail with an error.

...

</span>  void Player::init(void) {

<span id="_ctl0_PostForm_Reply">    char update[64];

    sprintf(update,"%supdate",name);

</span><span id="_ctl0_PostForm_Reply">    SquirrelObject root = SquirrelVM::GetRootTable(); // gets the global scope

    upd = root.GetValue(update); //fetches the function from the root </span>

<span id="_ctl0_PostForm_Reply">  } // init

</span><span id="_ctl0_PostForm_Reply">  void Player::update(void) { // Call a squirrel function: that's about as efficient as you can make it with a stack-based scripting language.

    SquirrelVM::BeginCall(upd);

    SquirrelVM::PushParam(1);

    SquirrelObject retval = SquirrelVM::EndCall();

     // do more stuff below

   }  // update

</span>};



You can reduce typing by wrapping common function call patterns with macros or using templates (can get complicated/may run slower if helpers are required. See LuaPlus implementation for more info (e.g. LPCD, LuaPlus Call Dispatcher)).



<span id="_ctl0_PostForm_Reply">John

</span>
THACO
#4 Posted : Friday, September 02, 2005 3:21:28 PM(UTC)
Rank: Member

Groups: Registered
Joined: 8/30/2005(UTC)
Posts: 6

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
I will run some tests, but yea I would end up wrapping a lot of things to suit my needs and make it look nicer. Thanks for your input

-Matt
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.102 seconds.