Godot, GDScript - Play Animation on Right Click - godot

Any insight onto why this code doesn't work?
When I right click, the game crashes and gives the error: "Invalid call. Nonexistent function 'play' in base 'Array'".
func _ready():
anim_Play = get_tree().get_nodes_in_group("AnimationPlayer")
func_input(event):
if Input.is_action_pressed("aim"):
anim_Play.play("AimSights")

I guess from your code that you are trying to get a reference to your AnimationPlayer node, it fails and you get an Array instead.
It happens because you are using get_nodes_in_group (which returns an Array of nodes in a group), instead of get_node, which returns a node.
Invalid call. Nonexistent function 'play' in base 'Array
Means your are trying the call the play method (found in AnimationPlayer) from an Array object, that does not exist.
You would get AnimationPlayer like
var anim_Play = get_node("./path/to/your/AnimationPlayer")

Response to your question
get_nodes_in_group(group) returns an Array of nodes that are both in the SceneTree and in group group.
Let's say there is one AnimationPlayer node in the group "AnimationPlayer". We'll fetch it like:
var anim_player = get_tree().get_nodes_in_group("AnimationPlayer")[0]
Notice the [0]. That is called an accessor. We access the array at element 0. Now, we can call play:
anim_player.play("AimSights")
Do note: it is an error to access a non-existent element of an array.
Recommendation
This seems like an inappropriate use of groups. I recommend you use a node path, like svarog suggested, if the animation player is in the same scene as the script.
Additionally, it will help to read or google about some fundamental programming concepts: specifically Objects and Arrays.
Lastly, read over the scenes and nodes page from Godot's documentation: https://docs.godotengine.org/en/3.1/getting_started/step_by_step/scenes_and_nodes.html
The whole getting started guide on the Godot's documentation is an invaluable resource for learning Godot. It will help you greatly and it's not too long of a read.
Good luck!

Related

get_node crashing game when node is deleted

First of all, I'm new to programming in general so I'm kind of assuming there's a simple answer to this question, I just couldn't seem to find it anywhere.
I'm making a simple platformer game with enemies that move toward the player. I used this code in the enemy's script underneath the physics process to get the player position:
player_position = get_parent().get_node("Player").get_position
However, upon the player being queue_freed when health reaches 0, the game crashes immediately and I get a null error due to there being no Player node. How can I work around this?
You could just set $Player.visibility to false instead of freeing, or you could check if the player exists first using get_parent().has_node("Player")
When you destroy the player, the physics process function is still trying to get the player node, even though it doesn't exist. So, as Lucas said, you could replace:
player_position = get_parent().get_node("Player").get_position
with something like...
if get_parent().has_node("Player"):
player_position = get_parent().get_node("Player").get_position
(Before setting player_position, it will check if the player node even exists)
I think you could use weakref (documentation here).
If you declare a weak reference:
var player_ref: WeakRef = null
and store that reference:
func store_player_node():
var player_ref = weakref(get_parent().get_node("Player"))
Then you can access that reference later:
if player_ref.get_ref():
player_position = player_ref.get_ref().get_position
Weakref has advantage over using get_parent().get_node("Player"). Let's imagine the following scenario:
Player dies and its node is removed from the parent node's children.
New node is created with name Player and added to the scene tree at the same place as the dead Player.
get_parent().get_node("Player") will return a node and the code will not crash. However, it will be a new Player node, not the old one, and I think this is usually undesired.
I hope this helps!

The global.game_width not set before reading it

global variable name 'game_width' index (100009) not set before reading it.
at gml_Object_objTextbox_Create_0 (line 16) port_x = (global.game_width
- box_width - port_width) * 0.5
I created in Game global.game_height and width, but it doesn't work. What am I supposed to do?
Looks like the objtextbox object is created earlier than the game object.
And because the game object creates the variable, it cannot find the global.game_width before it's initialised.
Inside the Text Layer Properties on the left sidebar, you'll see all the objects that are loading inside the room. The game object is not shown there, so you've to place the game object inside the room (and change the order they're loaded first) to let it work.
I'm not sure if this still applies as you've already rewrote your code, but that's something to remember for the next time.

message filter for bot's messages undefined node.js

I am having a bot set up the beginnings of a game. A human inputs the command of /startbrawl to initiate the setup of the game (creating the deck objects). But the two players need to be identified first. I have a message sent from another command that says "Player A is #[username A]. Player B is #[username B]." in the channel this game is happening in. I want the bot from this new command to look at the first message sent in the channel, which is always the "Player A is etc..." message (and is always sent by the bot) and pull both usernames from it in order to specify for this new command who is player A and who is player B. The code I have most recently (after trying multiple things) is this:
if (userInput.startsWith("!startbrawl") === true) {
message.channel.fetchMessages().then(messages => {
const botMessages = messages.filter(message => message.author.bot);
console.log(botMessages.mentions.members.first()) //this will be Player A. I'd repeat same for Player B with .last instead.
}
}
This gives me an error:
(node:15368) UnhandledPromiseRejectionWarning: TypeError: Cannot read
property 'first' of undefined.
I have made the last line be console.log(botMessages) to get all the info about the messages the filter finds. But trying to extract only part of it gives issues about not being defined, or just a result of undefined with no errors. Either way, something isn't working how I think I need it to.
The only other thing I've debated trying is exporting variables from the command prior to this new command. Player A and Player B are defined in the command used to make the channel that this new command is then used in. However, I've never had luck with exporting variables when I've used it in other instances. I use a command handler, so I'm not sure if that affects how exporting variables works... Which method would work best to set up the card game? I'm a novice in general just figuring things out as I go, so some advice (beyond take a course, look up basics, etc) is greatly appreciated. I've taken an online course for javascript and I work best figuring things out first hand.
Thanks for the help in advance!
As botMessages is a collection you wanna get a Message object out of it by doing
botMessages.first()
So try logging something like
botMessages.first().mentions.members.first()

Unable to return ref point using linkConnectionPoint function

I created a rect element with magnet property set to true. When I try to connect a link to the element, it gets connected to a point on the perimeter of the rectangle.
Then, I tried using linkConnectionPoint function as defined in the following code snippet:
linkConnectionPoint
But, the function is not been executed anytime I create a new link.
Is there any issue in the way of defining the function?
Any help is highly appreciated.
Please read source code.
https://github.com/clientIO/joint/blob/master/src/joint.dia.link.js#L1250
if joint.dia.Paper.perpendicularLinks is true, then never use linkConnectionPoint option...
so, please set 'perpendicularLinks' to false.

Guide.ShowSignIn(1, false) Value not in range exception

In a new Xna game I wroted this:
GamerServicesComponent gsc = new GamerServicesComponent(this);
gsc.Initialize();
Components.Add(gsc);
if(!GamerServicesDispatcher.IsInitialized)
GamerServicesDispatcher.Initialize(Services);
And in the Update method
if (Keyboard.GetState().IsKeyDown(Keys.S))
if (!Guide.IsVisible)
Guide.ShowSignIn(1, false); // true doesn't solve it nor 2 or 4 as paneCount
I'm receiving a
Value does not fall within the expected range.
Anybody?
It seems that Guide.ShowSignIn() can't be used anymore. I Don't know the reason nor if it is really true. But I couldn't get it working with ShowSignIn.
The way I had to login was by pressing the Home button.
The Guide will appear and you can simply follow the Guide to login.
Waiting a few frames before calling Guide.ShowSignIn() works for me. I don't call it until my game has loaded all assets and at least I frame has been rendered.

Resources