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

[Solved] Using a squirrel script as a callback inside a C++ class (Sqrat)
thaumaturgist
#1 Posted : Sunday, January 08, 2012 3:07:59 PM(UTC)
Rank: Member

Groups: Registered
Joined: 1/8/2012(UTC)
Posts: 17
Location: New Hampshire

Thanks: 1 times
Was thanked: 0 time(s) in 0 post(s)
Sorry if this is a bit of a newb question.

The game engine I'm writing has an entity system, where a a new entity is created for each folder in a map folder. I have the directories set up like so:

Code:

maps
    map
    |    entity01
    |    |    entity.json
    |    |    init.nut
    |    |    update.nut
    |    |    destroy.nut



Each time an entity is created, the scripts in its folder are compiled, and they run at appropriate times. This all works fine.

The problem I'm having is that obviously the scripts aren't attached to an Squirrel instance of a class, so I can't call member functions. Is there a better way I could be handling this?
thaumaturgist
#2 Posted : Tuesday, February 28, 2012 3:03:17 AM(UTC)
Rank: Member

Groups: Registered
Joined: 1/8/2012(UTC)
Posts: 17
Location: New Hampshire

Thanks: 1 times
Was thanked: 0 time(s) in 0 post(s)
Figured I would check back in and follow up on this... Activity here seems a bit low. :/

I've since abandoned this tactic in favor of deriving my entities from a base class written in C++, but this could still come in handy somewhere.

Code:


Sqrat::Script myScript(vm);

//    Load the entity script
try
{
    myScript.CompileFile("entity.nut");
}
catch (Sqrat::Exception& err)
{
    std::cout << "Loading script 'entity.nut' failed with error:" << std::endl;
    std::cout << err.Message() << std::endl;
}

//    Run the loaded script to register callbacks
try
{
    this->myScript.Run();
}
catch (Sqrat::Exception& err)
{
    std::cout << "Run script 'entity.nut' failed with error:" << std::endl;
    std::cout << err.Message() << std::endl;
}

//    Bind callbacks
Sqrat::Function myFunction = Sqrat::RootTable().GetFunction("init");


The script will be searched for a function named "init" and bind it to the C++ variable myFunction. You can then call it with parameters from C++.

Hopefully this helps somebody out.
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.071 seconds.