Access variable value from Nape body I collided with



  • I'm using nape physics, I got 2 objects both with a health variable and a damage variable.
    On collision I'd like to subtract object1's damage from object2's health and vice versa.

    I set up a collision listener according to this answer https://stackoverflow.com/questions/35105322/flxnapesprite-and-collisions
    but I'm at a loss as to how to get anything out of the InteractionCallback variable I have in the onCollide() function.

    If anyone's got any ideas on how to achieve this or something equivalent I would be very grateful, thanks in advance



  • I believe (and hope) I may be close to the solution, I found a repository of a game made with Nape here https://github.com/spipnl/game-challenge, this game seems to be able to achieve the type of interaction that I want.

    The important bit starts at line 229 of the PlayState (also pasted below), however this syntax seems to not be working for me as the .userData give the error "echo.Body has no field userData" (very strange since I'm not using Echo).

    private function onPlayerStartsCollidingWithPlatform(i:InteractionCallback):Void
        {
            var platform:Platform = cast(i.int2, Body).userData.data;
            var colArb:CollisionArbiter = cast(i.arbiters.at(0));        
    
            // When the player starts 'standing' on the platform, reset the number op jumps
            if (colArb.normal.y < 0)
            {
                player.resetJumps();
                player.isTouchingPlatform = true;
            }
    
            if (colArb.normal.y >= 0 && platform.breakable)
            {
                platform.isStomped();
            }
        }
    
        private function onPlayerIsCollidingWithPlatform(i:InteractionCallback):Void
        {
            var platform:Platform = cast(i.int2, Body).userData.data;
            platform.isStanding();
        }
    

    Hopefully I'll be able to solve this sooner or later, feel free to suggest if you were able to achieve anything similar!



  • The problem with the syntax of the line

    var platform:Platform = cast(i.int2, Body).userData.data;
    

    is resolved by swapping it with

    var platform:Platform = i.int2.castBody.userData.data; 
    

    But then another problem emerges from my code:

    public function CollPlayerToAnything(clbk:InteractionCallback) {
            var cstPlayer:Player = clbk.int1.castBody.userData.data;
            cstPlayer.ChangeIntegrity(-1);
            trace("Collision detected!!!");
    }
    

    I got this code in the function that the listener calls, when commenting out the first 2 lines casting the player's body the game runs fine and "Collision detected" is traced.

    However if I try including the 2 lines the game freezes on collision and this error is returned:

    TypeError: Cannot read property 'ChangeIntegrity' of undefined
    at PlayState.CollPlayerToAnything (c:\Users\username\Documents\Repositories\GitHub\FlixelGame\source\PlayState.hx:71:3)
    at zpp_$nape_spaceZPP$Space.step (c:\HaxeToolkit\haxe\lib\nape-haxe4\2,0,21\zpp_nape\space\Space.hx:3332:17)
    at nape_space_Space.step (c:\HaxeToolkit\haxe\lib\nape-haxe4\2,0,21\nape\space\Space.hx:508:9)
    at flixel_addons_nape_FlxNapeSpace.update (c:\HaxeToolkit\haxe\lib\flixel-addons\2,7,5\flixel\addons\nape\FlxNapeSpace.hx:170:4)
    at flixel_FlxGame.update (c:\HaxeToolkit\haxe\lib\flixel\4,6,3\flixel\FlxGame.hx:745:3)
    at flixel_FlxGame.step (c:\HaxeToolkit\haxe\lib\flixel\4,6,3\flixel\FlxGame.hx:677:3)
    at flixel_FlxGame.onEnterFrame (c:\HaxeToolkit\haxe\lib\flixel\4,6,3\flixel\FlxGame.hx:545:6)
    at openfl_display_Stage.dispatchEvent (c:\HaxeToolkit\haxe\lib\openfl\8,9,5\src\openfl\events\EventDispatcher.hx:402:5)
    at openfl_display_Stage.dispatch (c:\HaxeToolkit\haxe\lib\openfl\8,9,5\src\openfl\display\DisplayObject.hx:1418:17)
    at openfl_display_Stage.__broadcastEvent (c:\HaxeToolkit\haxe\lib\openfl\8,9,5\src\openfl\display\Stage.hx:1177:6)



  • This problem was also solved thanks to a fantastic correction by @codescapade on the Discord server.
    In my player class I was setting the userData by doing

    // adding CBODY
    CBODYPlayer = new CbType();
    body.cbTypes.add(CBODYPlayer);
    CBODYPlayer.userData.data = this;
    trace("Player's CBODY created");
    

    when instead I should've been doing

    // adding CBODY
    CBODYPlayer = new CbType();
    body.cbTypes.add(CBODYPlayer);
    body.userData.data = this;
    trace("Player's CBODY created");
    

    Now everything works as expected and I'm able to access any field of the 2 objects colliding.



  • @gioele-bencivenga
    A little correction on accessing the user data, the line

    var platform:Platform = i.int2.castBody.userData.data;
    

    actually freezes the game on some occasions, so I swapped it for

    var platform:Platform = i.int2.userData.data;
    

    and this seems to be working better.



Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.