How to get the property name from key? - godot

I'm trying to get a list of all the properties and the values that are being changed from an animation player node,
but how do I get the property name of node that the animation key is changing?
maybe something like this:
var ani=aniplayer.get_animation('running');
for i in range(0,ani.get_track_count()):
var key_idx=ani.track_find_key(i,0,0);
print("property=>",ani.get_key_property(i,key_idx)," new value=>",ani.track_get_key_value(i,key_idx));

Let us say you have an animation player:
var aniplayer := $AnimationPlayer as AnimationPlayer
And you get an animation by name:
var ani := aniplayer.get_animation("running")
As you know you can get the number of tracks the animations has:
var track_count := ani.get_track_count()
And, of course we can loop:
for track_id in range(0,track_count):
pass
Let us see, you want to know what property is being set. To get the property, we need to get the path, and extract the property from there:
for track_id in range(0,track_count):
var track_path := (ani.track_get_path(track_id) as String).split(":")
var node_path := track_path[0]
var property_path := track_path[1]
Addendum:
There is another way to get the property path from a NodePath: get_concatenated_subnames.
Or we can get the elements separated with get_subname.
So this is the property name:
var property_path = ani.track_get_path(track_id).get_subname(0)
I guess this is good time as any to bring attention to the fact that AnimationPlayer can animate sub-properties. With get_subname(0) you get the property, but not the sub-property.
For example, you don't have to animate position, you can animate position.y and you would do that by adding :y at the end of the track name in the Animation panel. I describe how to do it with more detail elsewhere. In this case get_subname(0) will give you position, but get_concatenated_subnames() will give you position:y.

Related

getting position of a node in gdscript

hi I'm trying to make a frog that jumps when the player gets close I tried this
onready var playerpos = get_parent().get_node("player").position
And
onready var playerpos = get_parent().get_node("player").global_position
and get this error
Invaled get index 'position' (on base: 'null instance')
Null instance means that the object you are trying to get, doesn't exist. And that is, because you are asking for its position, before it even enters the scene.
However, what you are trying to do, is completely unnecessary. You already have a main scene, so that should be able to access all of its nodes global position, without having to call the entire object.
Access your player's position from the main scene, not the frog scene.
That is, if you want the players position. What you want to do, however, does not require that.
You need to have an area node attached to the frog, that detects and sends a signal, when it detects a physics object. After which you simply check, whether the physics object is player or not, and execute the appropriate action.
According to GDDoc below:
https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html#literals
You can use the dollar sign to get node:
onready var player = $player
onready var player_pos = player.position
onready var player_global_pos = player.global_position
Or, you can get the player's position directly:
onready var player_pos = $player.position
onready var player_global_pos = $player.global_position

How do I make a kinematic body (3d) follow a player

I've been making a FPS in Godot and I'm having a hard time getting the kinematic body (the enemy) to go towards the player. Could someone please help?
The simplest way to do this is to get the player's position, compare it to the enemy position, and make the enemy move towards it every frame.
Full example code is at the bottom.
To get the player's position you first need a reference to it. You can usually do this through storing the reference in global singleton (autoload) or by exposing a public property.
If you are doing it with a global singleton, then you get the position by calling var player_position = my_singleton.player.global_transform.origin
If you are using an exported property, then you would get the position by calling var player_position = get_node(path_to_player).global_transform.origin
Once you have the player position, you can compare it to the enemy by writing var direction_to_target = player_position - global_transform.origin from inside the enemy node.
Now in order to follow the player, we override the _physics_process method with something like this:
### Inside the enemy script
var ENEMY_SPEED= 50
func _physics_process(delta):
var player_position = my_singleton.player.global_transform.origin
var direction_to_target = (player_position - global_transform.origin).normalized() # We normalize the vector because we only care about the direction
move_and_slide(direction_to_target * ENEMY_SPEED) # We multiply the direction by the speed

Hololens Placing Objects With Spatial Understanding

I have been running SpatialUnderstandingExample scene from holo-toolkit. Couldnt figure out how to place my objects into the scene. I want to replace those small boxes that comes default with my own objects. How can I do that?
Thanks
edit: found the draw box but how do i push my object there?
edit2: finally pushed an object at the position but still code is very complicated its messing up with the size and shape of my object. Will try to make it clean and neat.
It's been a while since I've looked at that example so hopefully I remember its method name's correctly. It contains a "DrawBox" method that is called after a successful call to get a location from spatial understanding. The call that creates the box looks something like this:
DrawBox(toPlace, Color.red);
Replace this call with the following (assuming "toPlace" contains the results from the spatial understanding call and "model" contains the model you are trying to place there):
var rotation = Quaternion.LookRotation(toPlace.Normal, Vector3.up);
// Stay center in the square but move down to the ground
var position = toPlace.Postion - new Vector3(0, RequestedSize.y * .5f, 0);
// instantiate the hologram from a model
GameObject newObject = Instantiate(model, position, rotation) as GameObject;
if (newObject != null)
{
// Set the parent of the new object the GameObject it was placed on
newObject.transform.parent = gameObject.transform;
}

Referrring to movie clips using variable strings

I want to know how to use a variable string to reference a movie clip in a hierarchy in AS2.
For example, and please forgive my newbie coding:
If my variable is defined as:
_root.MovieName = "Bob";
Then I'd like to be able to write:
_root.MovieName.ChildClip.gotoAndPlay("Label");
Where MovieName is the string "Bob" and not an actual instance called "MovieName". So Flash looks for an instance of "Bob" and goes into the child clips from there.
Is there any way to do this?
_root actually is a reference to the "root" of the movie, which also inherits a bunch of properties, it behaves like an object, so yes, you can do things like the following:
trace(_root["Bob"]); //Should return the instance.
var movieName = "Bob";
trace(_root[movieName]); //Should be the same.
I FOUND THE ANSWER!
In order to refer to movie clip instances using variables, first declare that variable as a string, then use the this[] handler. Here is the code that worked for me and the page that held it:
// CREATE THE STRING
var newString:String = "movieClipInstanceName";
// ASSUMING YOU ALREADY HAVE A MOVIECLIP WITH AN INSTANCE NAME OF "movieClipInstanceName" ON STAGE,
//CHANGE THE ALPHA OF THE MOVIECLIP TO 0
this[newString].alpha = 0;
And the page:
http://www.kirupa.com/forum/showthread.php?327501-Converting-String-to-Movie-Clip-Instance-Name
Big thanks to everyone who pitched in to help me!

How can I get the properties from a property Group of a Custom Control as an Object?

I'm working on a Custom Control that displays markers on Google Maps. I have a couple of properties like "maptype" , "zoom", etc. It is easy to access them in Javascript: I can use #{javascript:compositeData.zoom} to get the value of the zoom property.
Now this is my problem: I use a group of properties for each marker.
The name of the group is "marker" and a marker has 6 properties: "title", "layer", "infotext", "icon", "address" and "animation".
If I try to access the group with
var markers = #{javascript:compositeData.marker};
I'm getting an error in firebug:
missing : after property id var markers = [{layer=2,
address=Oldenzaal, animation=DROP, icon=/ogo_notes.png...
an arrow is pointing to the first = between layer and 2
(I am not allowed to put in an image in stackoverflow)
If I use
var markers = #{javascript:'"' + compositeData.marker + '"'};
markers is an Object, but each Object contains a string with all the propperties of the marker.
I know I can do some coding to make an object of each string, but this is not easy if not all propperties are required. If a propperty is not required than it will not show up in the string.
I guess that there must be a more easy way to get each marker as an object so I can get the value of the icon with code like:
var icon = marker.icon
How can I do this?
You can use compositeData.marker.icon to get the property icon inside the group marker. If you have checked "Allow multiple instances" for the group then to get the properties you will have to go:
compositeData.marker[0].icon
compositeData.marker[1].icon
and so on...
Update 26-Apr-2012 (Naveen)
To use it with client side javascript you can try to put the value in a hidden input field like this:
<xp:inputHidden id="hdnIcon">
<xp:this.defaultValue><![CDATA[#{javascript:var value = new Array();
for (var i=0 ; i<compositeData.marker.length ; i++) {
value.push(compositeData.marker[i].icon);
}
return #Implode(value, ",");}]]></xp:this.defaultValue>
</xp:inputHidden>
The value of this hidden input field can be read through client-side javascript like this:
var value = document.getElementById("#{id:hdnIcon}").value.split(",");
for (var i=0 ; i<value.length ; i++) {
<YOUR CODE>
}
Another way to do this can be to convert compositeData.marker and its contents to a JSON string and then run the client-side javascript on it.

Resources