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

Override native C++ method with squirrel?
lapayo
#1 Posted : Wednesday, March 28, 2012 5:04:11 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/27/2012(UTC)
Posts: 6
Location: Germany

Thanks: 2 times
Was thanked: 0 time(s) in 0 post(s)
Hello,
I have a short questions:
Is it possible to override native c++ methods within a squirrel script?

I tried the following:
Code:
class TestClass
{
public:
    void doEcho()
    {
        echo();
    }
    virtual void echo()
    {
        std::cout << "parent" << std::endl;
    }
};

in C++ and
Code:
class ExtraClass extends TestClass
{
    function echo()
    {
        ::print("Child")
    }
}

instance <- ExtraClass()
instance.doEcho()

in Squirrel.
But the call in Squirrel still calls the parent echo method.
Is this possible without porting the TestClass to Squirrel?

Thanks in advance,

Simon
cue
#2 Posted : Wednesday, March 28, 2012 6:06:20 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 1/3/2011(UTC)
Posts: 60
Man

Thanks: 0 times
Was thanked: 4 time(s) in 4 post(s)
Of course it's not possible. There is no way a Squirrel function can make it into the virtual table of a C++ class. This doesn't make any sense, C++ is compiled, Squirrel is interpreted at run time.
1 user thanked cue for this useful post.
lapayo on 3/28/2012(UTC)
lapayo
#3 Posted : Wednesday, March 28, 2012 6:23:05 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/27/2012(UTC)
Posts: 6
Location: Germany

Thanks: 2 times
Was thanked: 0 time(s) in 0 post(s)
Thanks for your answer.
Do you have an idea how to get a similar result?

What I wanted to do:
Extend the nativ Entity implementation with squirrel and push it to an native EntityManager which updates the entities every frame. For this I wanted to override the update method, so the entity can implement his own update method.


Thanks in advance,

Simon
flipper
#4 Posted : Wednesday, March 28, 2012 6:30:45 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/27/2012(UTC)
Posts: 5

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
beware, i'm a squirrel noob

maybe you could sq_call a closure and return at the top in virtual void echo, if the closure (echo/update) is in the squirrel class table (or meta table etc)
cue
#5 Posted : Wednesday, March 28, 2012 7:00:40 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 1/3/2011(UTC)
Posts: 60
Man

Thanks: 0 times
Was thanked: 4 time(s) in 4 post(s)
lapayo: I don't think this sort of coupling (as you describe it) is possible, at least I can't think of a way to implement it. If you want a C++ manager class to be able to handle both, C++ entity classes and Squirrel entity classes, you need to make an entity interface and two implementations. The first implementation is the C++ entity and the second is a wrapper around a Squirrel class instance which simply calls the respective squirrel functions of the wrapped squirrel instance.
1 user thanked cue for this useful post.
lapayo on 3/28/2012(UTC)
lapayo
#6 Posted : Wednesday, March 28, 2012 7:58:37 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/27/2012(UTC)
Posts: 6
Location: Germany

Thanks: 2 times
Was thanked: 0 time(s) in 0 post(s)
Thanks for your idea.
I am currently trying to implement it but have some problems.

How can I check if there is an class with the name an the create an instance from within c++ from the class?
And how can I then check if method xyz exists in the squirrel class?

I am using Sqrat for this.

Thanks in advance,

Simon
cue
#7 Posted : Thursday, March 29, 2012 11:12:31 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 1/3/2011(UTC)
Posts: 60
Man

Thanks: 0 times
Was thanked: 4 time(s) in 4 post(s)
Sorry, I don't use Sqrat, so I can't help you on that.

If you were to use Squirrel's C API: sq_get() retrieves a slot from a table/class/array and sq_createinstance() creates a new instance of a class, look them up in the documentation.
lapayo
#8 Posted : Saturday, March 31, 2012 12:53:54 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 3/27/2012(UTC)
Posts: 6
Location: Germany

Thanks: 2 times
Was thanked: 0 time(s) in 0 post(s)
Thanks for your help.
With trail-and-error I found a way to do it with Sqrat
The following works:
Code:
Function func(RootTable(vm), L"ExtraClass");

        if(!func.IsNull())
        {

            Object obj = func.Evaluate<Object>();
            Function func2(obj, L"echo");
            if(!func2.IsNull())
            {
                func2.Execute();
            }

        }

ExtraClass is my class I am creating.
echo() is the method I want to call.
Everything works as expected. :)
(I don´t know how "dirty" this solution is :D
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.100 seconds.