0

I am creating a basic little fighting game test build, however, I am recreating this from my previous work in Unity so I am mostly trying to do the same thing here but with no success. The biggest hiccup right now is that I want to create and remove HitBox nodes on demand. So, when a player throws a punch, the game should create a new Hitbox node (extending Area2D) that contains a CollisionShape2D with specific sizing to the punch. Enable it for the Active period, then disable it and remove it upon the move ending. What is the best way to do this from code?

To address potential feedback I do not want to resize premade child objects, I want to create them on demand in code or the animation tab, but preferably with code.

class_name HitBox
extends Area2D

var collisionShape = CollisionShape2D.new()
var collisionShapeType = CapsuleShape2D.new()

func _ready():
    collision_layer = 2
    collision_mask = 0
    monitorable = true
    
    add_child(collisionShape)
    collisionShape.name = "CollisionShape2D"
    collisionShape.set_shape(collisionShapeType)

The final line only seems to add a reference to a capsule collider to the shape variable of CollisionShape2D and not an actual capsuleshape. I also tried manually setting the shape field and creating the capsule in place, all of them throw an error or dont set the shape. I am trying to get it to effectively do the same thing you do when selecting shape from the dropdown menu, but in code during runtime.

Theraot
  • 31,890
  • 5
  • 57
  • 86
  • I do not understand, in which way this is not working? What do you mean by not an actual capsuleshape? – Theraot Jul 07 '23 at 23:36
  • Can you copy and paste the text of the error message into your post? – Camwin Jul 08 '23 at 01:09
  • CollisionShape2D requires a "Shape" Field to be filled with the typing Shape2D, in the editor this can be done using the dropdown menu but in code this needs to be set. When I set it to the CapsuleShape2D manually it returns ""~set_shape()" function: Argument should be "Shape2D" but is "GDScriptNativeClass" This same message is returned even if i append .new() to the end of the CapsuleShape2D. As far as I can see, CapsuleShape2D is a variable of type Shape 2D. The code i have now does assign a capsule shape correctly, but instead of loading a capsule like the dropdown does, it loads ObjID:9... – Julian Mccann Jul 08 '23 at 02:46
  • I ran out of characters, the object ID is 9223372061517284529. Where from the dropdown it normally just lists, "CapsuleShape2D". The other difference is that the dropdown selection adds the area in the editor view, it can be seen, and its properties become available to edit like radius and height. The OBJect ID does not grant these benefits and just sits as a reference to what a Capsuleshape2D is. – Julian Mccann Jul 08 '23 at 02:53
  • Simply moving the var declarations + assignments into the ready function works here!? The difference in the dropdown might not be relevant, the remote inspector might just work a bit differently. – Bgie Jul 08 '23 at 16:43
  • @JulianMccann So what you are trying is to execute this in the editor? I don't see `@tool` in the script, so this would be executing when the game runs. Are you really getting `GDScriptNativeClass` (I tried reproducing it and didn't get it)? The `GDScriptNativeClass` class is not exposed since Godot 3.2 (and I see the question tagged Godot 4), so you might want to report a bug here: https://github.com/godotengine/godot/issues - And about the Object ID, if you are looking at it from the remote scene tree, that is expected, even for shapes set from the inspector, but seen from remote scene tree. – Theraot Jul 09 '23 at 00:19
  • Moving declarations and assignments into the ready function does not seem to change anything. @Theraot I am running in godot4.1 and I am not trying to do this in the editor at all, I am trying to make something that can be executed completely in runtime as a method. So, when the player enters an animation, the appropriate collisions are created, and subsequently removed. Furthermore, shapes set by the editor are not seen as Obj ID's in the remote editor, they look identical to the local editor. Where the code set versions always appear as an object ID. – Julian Mccann Jul 09 '23 at 17:43
  • Is this what you are getting: https://stackoverflow.com/q/75141682/402022 ? – Theraot Jul 17 '23 at 19:34
  • No that is not, I found the solution but not all of it. The OBJ ID does function as the correct shape. I had been using an editor setting wrong and was reading the information wrong. so my original code does actually work. The GDSCRIPTNATIVE does seem like a bug but it never occurred in my working code so i figure i just did something wrong – Julian Mccann Jul 19 '23 at 03:42

0 Answers0