with 'struct' suggestion

Comments

7 comments

  • Justin McFall

    I don't fully understand what you're trying to do, so this might be irrelevant, but is this something the "method" function could help with?

    0
    Comment actions Permalink
  • Adrian Nelson

    I am using method_get_self() but have found that it does NOT work in a static function within a struct which is probably to be expected although the static function does have access to the struct 'instance' variables

    0
    Comment actions Permalink
  • Justin McFall

    It will work *in* a static function, it just won't work if you're using a static function as the reference. If you're doing something like

    callback = function(){ ... }

    So you could do something like:

    ds_list_add( callbackList, someStruct.foo);

    for( var i = 0; i < ds_list_size( callbackList); ++i){
    callbackList[| i]();
    }

    If you want to perform a universal function within the context of a given struct, you could use:

    ds_list_add( messageManager.messageListeners, self);

    // messageManager
    foo = function(){ ... }

    for( var i = 0; i < ds_list_size( messageListeners); ++i){
    method( messageListeners[| i], foo);
    }

     

    0
    Comment actions Permalink
  • Devon Mullane

    Hmm. I think I have a piece of code that works like this and so I'm wondering if your case is one I haven't seen or it would help. In your pub /sub you have the self reference as the sender and/or recipient, in which case with( struct ) { func() } should work. Can you elaborate on what exactly is failing? In my case I'm using static methods as well, but my context may be different.

    0
    Comment actions Permalink
  • Adrian Nelson

    My code has now been significantly re-factored and is now working - I'm still not convinced that with (struct) works, you will probably find you have a game object instance and not the struct instance - but that could have been due to the code I had at the time.

    0
    Comment actions Permalink
  • Devon Mullane

    The project only has one object, and it's the one doing the with call.  But, this is why I ask about your particular circumstances.  Consider the following:

    ex = { say : function() { show_debug_message( "ex" ); } }

    with ( ex ) {
    say();

    }

    Works as expected.  The same goes for structs returned from constructors.  Now, in the example I was talking about, I can definitively say it's a struct:

    var _feature	= features[| _i++ ].struct;
    var _do = variable_struct_get( _feature, _event );

    if ( is_undefined( _do ) == false ) {
    log( features[| _i - 1 ].struct );
    with ( _feature ) {
    _do();

    }

    }

    As far as I know, with() should be working with structs.

    0
    Comment actions Permalink
  • Adrian Nelson

    I think my usage was slightly different, I was several recursions into my pub / sub messaging where my game object sent a message which was subscribed to by a function within a struct, that function then needed to send a message of its own that a different function within a different struct had subscribed to and then it needed to use the data in the message which itself was a struct with a method variable (i.e a function pointer to yet another function within a different struct again) that actually did something.

    I had originally started off using 'with' around the actual message sending but once I'd then recursed into another send with it's own 'with' around the second call I think this is where it went screwy. Part of this may have been my pub/sub system which has now changed to ensure it now doesn't allow infinite message passing that could potentially happen.

    The above sounds more complicated than it really is as I have an entity component system with a game map composed of a 2 dimensional array of cells (which are structs with functions) which then has a list of entities in that cell (a different struct with its own functions) and entities have their own list of components (yet another different struct with its own functions).

    This is very flexible as each component has a single purpose and so game entities are just collections of components essentially with one behaviour per component which makes it easy to create new game entities with unique abilities and to modify them at runtime and the game logic is simple as it can check components between entities to see what the outcome of their interaction is according to simple rules based on different components.

    Phew!

    Thanks for your thoughts though - I may revisit 'with' in the future as it would make my system a bit simpler but its working right now so I'm going to carry on for a while. Thanks.

     

    0
    Comment actions Permalink

Please sign in to leave a comment.