#The game crashes when interacting with an object

64 messages · Page 1 of 1 (latest)

brave karma
#

Hi! I created an inventory and chest interaction system, but now the game crashes when the player presses the interaction key on any object where the collider is located. In addition, I attached the script only to the chest, but for some reason the script spreads to other TileMap objects. After the crash, it throws two errors and puts me in the player scenario. I have already done a lot, but I have not found a solution 😦

#

Basic FPS Controller Code: https://pastebin.com/yJwwpkCu
Shikashi's Fantasy icons Pack: https://cheekyinkling.itch.io/shikashis-fantasy-icons-pack

Follow along as I code the structure of an RPG type inventory system in Godot 4.

=== Intro / Setup ===
0:00 Intro
0:45 Project Setup

=== Resource Data Structure ===
2:50 Item Data Resource
4:27 Slo...

▶ Play video
brave karma
#

Chest Script

wicked frost
#

Either change the collision layers and masks so that the raycast do not collide with your tilemap or add your object to a group and check that before continuing the script

#

But I see you have a Node2D, and that node does not have collisions

#

I cannot check the tutorial video at the moment, but maybe you missed something

brave karma
#

Hmmm

#

Thanks) I changed the layer, but now the ray doesn't want to interact with objects. I would like to attach the chest to the TileMap system in the future, but at the moment it is separated, but at the same time there is interaction with all objects that have a collision, and I do not know what is the matter. I can send you a video about how the error occurs

#

The error is triggered when the interaction button (E) is pressed

rugged basalt
#

The error suggest its triggering the player_interaction function on a tilemap, not a player. So whoever is colliding should have that function.

brave karma
#

Wow.. But I don't understand how this sends a signal to TileMap : D

rugged basalt
#

I believe raycast can only collide with whatever layer mask you give it, maybe is also colliding with the tilemap where it should only be colliding with the chest and just returns the first collision it finds.

#

Im not sure how your layer masks are setup but seems to be the problem either that or the raycast mask.

brave karma
#

Hmm, is it possible to attach a chest with a code in TileMap? I tried to do this, but after that the script was activated on absolutely all TileMap objects (for example, a tree). About the layer, I can throw it to you

rugged basalt
#

Okay so a couple of things, 1. Your raycast is only detecting for collisions on layer 1.
2. Your tilemap IS layer 1 (thats why raycast is colliding with it) and can collide with layer 1 (itself).
3. Your chest should have a collisionshape of some sort to detect collisions to begin with , then put it in a layer to detect and be detected. I see its just a Node2D so i dont know if it has any collisions.
4. You can change layer names in the project options 2d Physics layers so you can setup like "world" "player" and "object" entities and place their collisions accordingly.

#

Layer just represents what layer is on and Mask what that layer can collide with. So ideally object layer should be colliding with player only. Player with chest and world, world with player and raycast with object only. That way the ray cast knows for sure the collider is a player as it only collides with player layer.

#

Assuming the tilemap has no collisions otherwise it will get you stuck, as collisions should only be applied to walls on a 2D game.

brave karma
#

Wow, that looks really cool! I will definitely check it out now 🙂

#

I changed the TileMap layer to 2 and now there is no crash when interacting with TileMap objects, however, when interacting with a separate chest, a crash occurs and already with a similar error

#

Could it be the same, but with a player?

brave karma
brave karma
brave karma
#

I'm also interested in one question. If I want to add a script chest to TileMap, how do I make other TileMap objects not react to this script except for the chest?

rugged basalt
#

Sorry for late answer.
First, the error text tells you good info about whats going on, now is trying to find the function on a StaticBody2D node. However i see youre using Sprite as the root node. So its trying to get a function of the static body which has no script itself ,the root sprite does, so id recommend always use the physics body as root then all its components as children.
As for your second question, im not sure if i understand what you mean if you mean add a script to a tilemap? You can add a script to pretty much everything. If you want to avoid interactions between objects you can use layers and masks also groups, you can use is_in_group("chests") or whatever to check if whatever node youre colliding/interacting is the one.

brave karma
#

It's okay) Thank you. I'll try to do what you said. Could I communicate with you in private messages for convenience? I would like to ask something else 🙂

brave karma
#

Can I do it this way?

wicked frost
#

ideally the StaticBody2D is the root node

brave karma
#

Thanks

brave karma
brave karma
#

Hey, hello again! I'm studying this topic more and more, but I don't understand one thing. I still can't call the chest opening system. I changed the beam to area2d, and now I do not know what to put in place of these lines

#

The comments under the guide say the following, but I did not fully understand their meaning

#

I have tried many things

rugged basalt
#

Area2D has built in functions for detecting areas AND/OR physics bodies entering and/or leaving.You need to make use of layers and masks. In the Node/Signals part of the inspector on an Area2D , you can call signals such as area_entered() or body_entered() etc. Just double click them to connect and add them to the node and put the code there.

brave karma
#

I tried to do it, but I didn't really understand what to put in there. I'll try.

#

Hmm, once I did a similar system and I will try to do it by the example of her

rugged basalt
#

Check the inspector of the Hitbox area. In the "Node" tab where all signals are. You will see the list of signals available for that node. There you can select and dounle click one to connect and add it to the code, where it will trigger. You dont need to make the signal in code as a custom one.

#

Like i said Area2D already has built in signals to detect all kind of collisions. You will see them in that list.

#

As long as the layers and masks are setup correctly and the area detects the correct layer it should trigger.

brave karma
#

Yes, thank you 🙂 So far, I'm studying and trying it all, and I really hope that I can understand everything

#

However, I get this error. However, I am getting this error. Is this exactly what I need to do with the nodes?

rugged basalt
#

Area2D doesnt have is_colliding function , that is a function for RayCast2D nodes. Area2D does not need any code to detect its collisions, it just needs the layers and masks to be configured properly and their signals trigger upon collision with the layers.

brave karma
#

Good. Thanks again 🙂

brave karma
#

The chest window still cannot be called 😦

#

This is the chest script

#

However, if I delete these two lines, then "interaction()" will give the error "Function "interaction()"not found in base self."

#

I still think I could have framed instead

brave karma
rugged basalt
#

You dont need to check if the area was entered, the signal of the hitbox area is the one taking care of that, you only need 1 signal, the hitbox one.

brave karma
#

It's just that these lines of code were used in the guide, but if you remove them, then errors begin

brave karma
#

Do you have an example of what I could replace these lines with? Or another guide to the inventory where I could learn this