Archive 17/01/2023.

ScriptObject and the scope of node parameter, again [SOLVED]

ghidra

I’m kind of stuck on how the “node” parameter is working in a scriptObject.

Using node.scene, and node.parent are working as per my late post http://discourse.urho3d.io/t/angelscript-createscriptobject-and-external-class-solved/342/1, at least as long as it is in the update function.
If for example I call a function from update, like raycasting (using the example from navigation.as). If I have a function called raycast, this throws errors, null exception:

bool raycast(float maxDistance, Vector3& hitPos, Drawable@& hitDrawable){
    hitDrawable = null;
    IntVector2 pos = ui.cursorPosition;

    Camera@ camera = node.scene.GetComponent("Camera");
    Ray cameraRay = camera.GetScreenRay(float(pos.x) / graphics.width, float(pos.y) / graphics.height);
   
   ......

   return false;
}

The GetComponent(“Camera”) doesnt actully return a camera.

also, as another example…
If I do what i am trying in a separate function in update, as long as I pass the uint id it works this way:

void Update(float timeStep){
	Node@ cameraNode = node.scene.GetNode(_camera_id);
	Camera@ camera = cameraNode.GetComponent("Camera");
	....
}

but I dont pass the uint id and try to use a string like:

void Update(float timeStep){
	Camera@ camera = node.scene.GetComponent("Camera");
	....
}

It does not work, although I feel like I see this methodology used in the examples to grab static meshes and animated meshes etc… that is, not having to get the node before grabbing the component.

Just looking for some clarification on what I might be overlooking.
Thank you.

friesencr

You don’t usually have a camera directly under the scene since you don’t move the scene.

This code:

would be looking for a camera on the scene. The scene inherits from node so it can but it likely isn’t what you want.

The string is actually the type of component you want. Urho will fetch the first component of that type on that node. The string is actually converted to a StringHash internally, not that that matters. In this case the type is a Camera. The camera is a component. GetChild

In all of the example code the app holds a sharedptr to a node that is holding the camera. This is the easiest way of handling it.