Games How do I distinguish between different "sources" of Callback?

Discussion in 'Game Development' started by Gunther, Nov 2, 2018.

  1. Gunther

    Gunther Active Member Licensed User

    The Box2d Raycast do have the ability to set the name of the CallBack procedure.

    void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2);
    in XUI2D only the World do have the .RayCast member. Means the World_RaycastCallback is getting always the answer to the question.

    How do I distinguish between different "sources" in a single callback procedure than? Sure, the source is always the world object, but may it is requested be different procedures with different useage of the answer. like 2 different chatacters at unknown times. Who ask the question answered in the callback some more efficient than setting a special boolean IF THEN structure?

    If the boolean structure is the single way how we make sure that the set boolean was really the one wihch ask the world for the RayCast result? I mean this an async process. So we should know/check and not assume the the last answer was the right answer to the last question the world, am I right?
    Last edited: Nov 3, 2018
  2. Erel

    Erel Administrator Staff Member Licensed User

    The Sleep(0) in the walking character example is not needed. I've removed it.
    The code is:
    Private Sub Panel1_Touch (Action As Int, X As Float, Y As Float)
    If Action = Panel1.TOUCH_ACTION_DOWN Then
    Dim p As B2Vec2 = X2.ScreenPointToWorld(X, Y)
    'cast a ray from the kid to the point
           RaycastBody = Null
           RaycastPoint = p
           world.RayCast(, p)
    If RaycastBody <> Null And RaycastBody.IsDeleted = False Then
    'ray hit a body
               RaycastBody.TimeToLiveMs = 1
    End If
    End If
    End Sub

    Private Sub World_RaycastCallback (Fixture As B2Fixture, Point As B2Vec2, Normal As B2Vec2, Fraction As Float) As Float
    Dim bw As X2BodyWrapper = Fixture.Body.Tag
    'ignore static bodies
       If bw.Body.BodyType = bw.Body.TYPE_STATIC Then Return -1
       RaycastBody = bw
       RaycastPoint = 
    'return fraction to limit the ray up to the current fixture.
       'The result is that the last event will be the closest body.
       Return Fraction
    End Sub
    The raycast callback happens synchronously. This means that it starts when you call world.Raycast and ends on the next line.
    Gunther likes this.
  3. Gunther

    Gunther Active Member Licensed User

    I would be grate if the listener of the Callback would be a variavle as it in the original. The code is more structured than.

    Just assume that one Raycast Survey is for Static Bodys only the other for Dynamic Bodys only. Then the RayCast-CallBack reduces to just handing over the local variables to gobal variables (RaycastBody, RaycastPoint, etc) and the further processing must be done in the following lines of code of the World.Raycast calling.

  4. Erel

    Erel Administrator Staff Member Licensed User

    You can easily solve it using a single global variable.
    B4X works with events. It was possible to make the event name dynamic however it will not be changed.
    Gunther likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice