PDA

View Full Version : What is the prototype of the UserDefined function of TBGL_ENTITYCREATEFUNCSLOT ?



D.J.Peters
18-05-2013, 17:06
I saw different versions of working user defined func slot's TBGL_ENTITYCREATEFUNCSLOT().

Thank you for pointing me in the right direction.

Joshy


SUB without params

TBGL_ENTITYCREATEFUNCSLOT(%sScene, %eBackground, %eCameraPivot, "Background_Render")
' ...
Sub Background_Render()
' ...
end subFUNCTION with params

TBGL_EntityCreateFuncSlot(%sScene, %eCar, 0, "Car_Render")
' ...
function Car_Render(sceneID as long, entityID as long, startModelID as long) as long
' ...
end function

Petr Schreiber
19-05-2013, 09:51
In thinBasic, there is no internal difference between SUB and FUNCTION, so you are free to choose.

Regarding parameters - none is used when procedure is automatically fired using Scene mechanism.
The proper way to pass data into procedure fired using Scene mechanism is via entity user data.

The official template for custom entities can be found in thinAir, as TBGL/actor_object.taguit.
You can read this post (http://www.thinbasic.com/community/showthread.php?t=12080&p=88614&viewfull=1#post88614) to get the logic behind creation of custom entities from this template.


Petr

Petr Schreiber
19-05-2013, 15:34
Hi Joshy,

I understand your confusion, but what I wrote is still valid.

Internally, TBGL uses thinBasic_FunctionSimpleCall_ByPtr to simply call the function - without any kind of parameters.
The parameters are passed exclusively by setting data to entity via TBGL_EntitySetUserData, and then retrieved using TBGL_CallingEntity / TBGL_EntityGetUserDataPointer.

I agree the shader example had Car_Render with parameters, but that was wrong. It was because original version did not used function slot and I forgot it there. You can see it because the function body does not use those parameters in any way.

I fixed the example once again, and commented, so you can see how to get parameters to entity correctly, please redownload here:
http://www.thinbasic.com/community/showthread.php?t=10033

I hope it is more clear now - SUB or FUNCTION, without parameters, data are passed in and read using specific commands.

Little citation from the linked example:


' --- --- --- --- --- --- --- --- --- ---
Function Car_Create(sceneID As Long, entityID As Long, carBody As Long, carWheel As Long) As Long

Dim data As CarRender_Params
' -- Entity
TBGL_EntityCreateFuncSlot(%sScene, %eCar, 0, "Car_Render")
' -- Store the data to structure
data.carWheel = carWheel
data.carBody = carBody
' -- Pack them to entity
TBGL_EntitySetUserData(%sScene, %eCar, data)

...

end function

Function Car_Render() As Long
' -- What is the calling entity?
Dim element As TBGL_TENTITYIDENTIFIER At TBGL_CallingEntity

' -- Read its data
Dim data As CarRender_Params At TBGL_EntityGetUserDataPointer(element.scene, element.entity)

...

End Function


Petr

D.J.Peters
19-05-2013, 15:36
I agree the shader example had Car_Render with parameters, but that was wrong.Ok now it's more clear :-)

from template:
'[!] Rendering
Function <object>_Render()
Dim element As TBGL_TENTITYIDENTIFIER At TBGL_CallingEntity
Dim data As <object> At TBGL_EntityGetUserDataPointer(element.scene, element.entity)


End Function

'[!] Methods
Function <object>_Method(sScene As Long, eEntity As Long)
Dim data As <object> At TBGL_EntityGetUserDataPointer(sScene, eEntity)

End FunctionFunction <object>_Render() will be trigger fromTBGL_SceneRender( SceneID ) is it right ?
and Function <object>_Method(sScene As Long, eEntity As Long) will be trigger from what ??

thank you

Joshy

Petr Schreiber
19-05-2013, 15:40
Function <object>_Method is just a prototype for custom method, specific to your use.

You can change it to as many user methods as you need, for example:


Function Car_EnableNitro(sScene As Long, eEntity As Long)

Dim data As CarData At TBGL_EntityGetUserDataPointer(sScene, eEntity)

data.useNitro = true

End Function

Function Car_DisableNitro(sScene As Long, eEntity As Long)

Dim data As CarData At TBGL_EntityGetUserDataPointer(sScene, eEntity)

data.useNitro = false

End Function


It is there just to provide encapsulated access to "object" properties and actions. It is up to programmer when to use those methods.
I like OOP programming, but ThinBASIC is not OOP language, so I emulate OOP this way.

It is described in Step #6 of the thread I have linked to previously (http://www.thinbasic.com/community/showthread.php?t=12080&p=88614&viewfull=1#post88614).


Petr

D.J.Peters
19-05-2013, 15:53
I got it all now
from "Custom WaterPlane actor entity"

again thank you.

Joshy