Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:11:20+00:00 2026-06-14T15:11:20+00:00

I am trying to create an architecture that would use C/C++ as scripting language.

  • 0

I am trying to create an architecture that would use C/C++ as “scripting language”. I already have a prototype working based on:

http://www.codeproject.com/Articles/29999/Compiling-C-code-at-runtime

and

http://runtimecompiledcplusplus.blogspot.com

My prototype allows me to recompile a dynamic link library / shared object and reload it at runtime. I only have a small problem lets take in example the following code:

[ bot.c ]

typedef struct
{
    float health;

    float speed;

    bool  alive;

} Bot;

Bot bot = { .health = 100.0f,
        .speed  = 1.0f,
        .alive  = true };

void OnCollision( void )
{
    bot.health -= 10.0f;

    if( bot.health <= 0.0f )
    { bot.alive = false; }
}


void OnUpdate( void )
{
    if( bot.alive )
    {
        /* Update animation */
    }
}

If the bot.c script is assigned to 1 bot its ok, but if I assign the callbacks to multiple bots they share the same data (1 get hit, they all get hit!). How can run the bot script “separately” for each bots?

I already research on coroutine in C/C++ (using setjmp/longjmp) but its risky for C++ constructors & destructors (and also force you to integrate macros inside your functions, which in my case is not an options for my users).

I was thinking about running multiple threads, but the same problem will occur (since the data comes from a dll/so it is shared). Im not too familiar with fork/co-process but it does not seems to apply for my case. And I do not think that pipes are an option as well… Im pretty much stuck.

Is there anyway to solve this problem?

ps: Yes I know that scripting language like Lua or Javascript (V8) have built-in coroutine or in the case of Lua lua_thread that would fix my issue but I want to stick with a C/C++ interface for the users to code since its for gaming performance is critical!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:11:20+00:00Added an answer on June 14, 2026 at 3:11 pm

    You don’t need anything like coroutines. The problem is that you have a single global variable holding the data and you simply need multiple instances of it.

    Now the standard shared library machinery won’t allow you to duplicate the static data of the module, but you don’t need to. Just don’t allocate the data statically, but create multiple instances of it.

    In C++ it would be easier to use, but somewhat more difficult to implement since you’d need to compute the mangled symbol names and correct incantation to do new and delete. The script would simply look like:

    class Bot {
        float health;
        float speed;
        bool  alive;
        Bot()
        void OnCollision();
        void OnUpdate();
    }
    Bot::Bot() : health(100.0f), speed(1.0f), alive(true) {}
    ...
    

    You’d need to know the class name, but that could be derived from the module name or something like that.
    You could write a template that would generate bunch of static functions to somewhat portably handle the initialization. Like:

    struct ModuleBase {
        void *(*Init)();
        void (*Done)(void *);
        void (*Collision)(void *);
        void (*Update)(void *);
        ModuleBase(void *(*I)(), void (*D)(void *), void (*C)(void *), void (*U)(void *)) : Init(I), Done(D), Collision(C), Update(U) {}
    };
    template <typename T>
    class Module : public ModuleBase {
        static void *InitFunc() { return static_cast<void *>(new T()); }
        static void DoneFunc(void *x) { delete static_cast<T *>(x); }
        static void CollisionFunc(void *x) { static_cast<T *>(x)->OnCollision(); }
        static void UpdateFunc(void *x) { static_cast<T *>(x)->OnUpdate(); }
    public:
        Module() : ModuleBase(&InitFunc, &DoneFunc, &CollisionFunc, &UpdateFunc) {}
    };
    

    used like

    Module<Bot> bot;
    

    at the end of the “script”. Than you just look that symbol up and call the function pointers in it.

    In C it would be more difficult to use, because you’d need to explicitly write init and deinit functions, but you already know how to call it:

    struct Bot { ... }
    // NO INSTANCES HERE!
    void *Init(void) {
        Bot *bot = malloc(sizeof(Bot));
        bot->health = 100.0f;
        bot->speed = 1.0f;
        bot->alive = true;
        return bot;
    }
    void Done(void *bot) {
        free(bot);
    }
    void OnCollision(void *void_bot)
    {
        Bot *bot = void_bot;
        ...
    }
    void OnUpdate(void *void_bot)
    {
        Bot *bot = void_bot;
        ...
    }
    

    In either case you simply create any number of instances, either with operator new[] and Bot constructor or with Init and call the functions/methods with appropriate argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create an application that is based on module level security.
I'm trying to think of a way to create a plugin architecture for my
I am trying to use the backbone architecture in my MVC application. I created
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I'm trying to get the hang of MVC architecture. Say I have a plist
Currently I'm learning about neural networks and I'm trying to create an application that
I’m trying to reevaluate our n-layer architecture and would love to get some suggestions
I'm trying to create a jQuery like architecture in my app but i could
I was reading http://www.infoq.com/interviews/trelford-functional and trying to understand the alternative approach to OO. Q
So, im trying to write some code that utilizes Nvidia's CUDA architecture. I noticed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.