1. Hey Guest, is it this your first time on the forums?

    Visit the Beginner's Box

    Introduce yourself, read some of the ins and outs of the community, access to useful links and information.

    Dismiss Notice

Problem with function handles

Discussion in 'Modding Help' started by sinitreo, Apr 28, 2014.

  1. sinitreo

    sinitreo Shopkeep Stealer
    1. Zen Laboratories

    Messages:
    66
    @Geti, posting my cb problem here.

    1. Right now I tried to reproduce my issue with the following code attached to knight blob:

    Code:
    #define SERVER_ONLY
    funcdef void CALLBACK();
    onInit(CBlob@ this){
        CALLBACK@ cb = @somefunc;
        cb();
    }
    void somefunc(){
        print('Callback worked!');
    }
    
    I get no compile errors, but when I join and switch to/respawn as knight, my blob does not appear. There are no errors printed neither on the server nor on the client.

    2. Some time ago, I was trying to use callbacks in classes to implement advanced GUI system on top of native one.
    This is the error I got when launching server:

    Code:
    //../../source/as_compiler.cpp:3831: void asCCompiler::Error(const asCString&, asCScriptNode*): Assertion `node' failed.
    //[03:50:44] Closing console device: Signal 6 received
    Removing callback invocation fixed it but also made it useless.
     
  2. Geti

    Geti Please avoid PMing me (poke a mod instead) THD Team Administrator Global Moderator

    Messages:
    3,730
    Oh, I'm not sure if function handles work. We use classes almost like functors to get around that issue for things like votekick etc

    you can use opcall for "real" functors that use the () operator, but we use named functions for clarity. Check out DefaultVotes.as and how it uses VoteFunctor from VoteCommon.as to derive the votekick and vote nextmap behaviours as extensions to VoteFunctor. It also sets some stuff in a shared class to get stuff easily into the vote GUI.

    This could be extended by storing GUI rendering callback functors.

    Also your SERVER_ONLY there will mean you definitely wont get printing happening on the client console even if it were to work :^)
     
  3. sinitreo

    sinitreo Shopkeep Stealer
    1. Zen Laboratories

    Messages:
    66
    UPD: CContextMenu completely misses any description in objects.txt. Or am I blind?

    Well classes as functors do the trick. However, would be cool if I could do like this:

    Code:
    void buttonClick(){
       ....
    }
    ...
    button.OnClick(buttonClicked);
    ...
    
    Instead of

    Code:
    shared class OneOfMillionsOfButtonHandlers : ButtonHandler{
      OneOfMillionsOfButtonHandlers(){}
      void OnClick(){
      ...
      }
    }
    ...
    
    ...
    button.OnClick(new OneOfMillionsOfButtonHandlers());
    ...
    
    Though second one provides more flexibility.

    The coolest thing would be lambdas <3
    (Can I dream a bit after all :D)
    Code:
    button.OnClick(_=>{
    //Do stuff
    })
    
     
    Last edited: Apr 28, 2014
  4. Geti

    Geti Please avoid PMing me (poke a mod instead) THD Team Administrator Global Moderator

    Messages:
    3,730
    Re: CContextMenu - Afaik there's nothing bound other than its class name at the moment, probably because some function returned a pointer to a context menu and MM didn't want to wrap it with a binding that dropped the return value so he just bound the class instead.

    Yeah it'd be cool to have function handles, we might need to update our AS version but every time we do that we need to test the entire game to make sure nothing drastic has broken (has happened a few times...) so we generally delay it quite a bit, or ignore minor releases. Not sure if function handles are meant to be included in our current version or not.

    Re: Lambdas - lol :^)
     
  5. LightTab2

    LightTab2 Drill Rusher
    1. Zen Laboratories

    Messages:
    83
    An example from my code, ah and you should first use GUI::DrawPane(Vec2f, Vec2f);
    Code:
    [...]
    GUI::DrawText( "Team7^", Vec2f(getScreenWidth()/200, getScreenHeight()/30 + offset) , Vec2f(getScreenWidth()/8, getScreenHeight()/30 + getScreenHeight()/60 + offset), allycolor , true, true);
    EDIT: Irrlicht handle also printf function, I think.