Skip to content

Commit

Permalink
Merge pull request #144 from godot-nim/143-missing-field-instanceid-i…
Browse files Browse the repository at this point in the history
…n-class-area3d

Fix: object stringification
  • Loading branch information
panno8M authored Jan 6, 2025
2 parents a7e9ece + edeb415 commit b49e18e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/gdext/gdinterface/objects.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ proc castTo*(obj: Object; p_class_tag: pointer): ObjectPtr =
proc getInstanceID*(self: Object): GDObjectInstanceID =
interfaceObjectGetInstanceId CLASS_getObjectPtr self

proc getClassName*(self: Object): StringName =
discard interfaceObjectGetClassName(CLASS_getObjectPtr self, environment.library, addr result)
9 changes: 4 additions & 5 deletions src/gdext/surface/classutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import gdext/core/gdrefs

export objects.destroy
export objects.getInstanceID
export objects.getClassName

proc instantiate_internal*[T: SomeEngineClass](Type: typedesc[T]): T =
let objectPtr = classDB.constructObject(classname Type)
Expand Down Expand Up @@ -61,11 +62,9 @@ proc singleton*[T: SomeClass](_: typedesc[T]): T =
else:
result = cast[T](cache)

proc `$`*[T: SomeClass](self: T): string =
if self.isNil: return $T & "(nil)"
result = $T & "(ID: 0x" & self.instanceID.toHex & ")"
when compiles self.name():
return $self.name() & " [" & result & "]"
proc `$`*[T: Object](self: T): string =
if self.isNil: return $self.getClassName & "(nil)"
$self.getClassName & "(ID: 0x" & self.getInstanceID.toHex & ")"

export onInit, onDestroy
method notification*(self: Object; p_what: int32) {.base.} = discard
Expand Down
6 changes: 5 additions & 1 deletion src/gdext/surface/nodeutils.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import std/macros

import gdext/core/gdclass
import gdext/core/builtinindex
import gdext/gen/[builtinclasses, classindex]
import gdext/classes/gdNode
Expand All @@ -13,4 +14,7 @@ template `/`*[T: Node](self: Node; sub: typedesc[T]): T = self/($sub) as sub

proc instantiate*[T_Node: Node](T: typedesc[T_Node]; name: string): T =
result = classutils.instantiate(T)
result.name = gdstring name
result.name = gdstring name

proc `$`*(self: Node): string =
$self.name() & " [" & $Object(self) & "]"
2 changes: 1 addition & 1 deletion src/gdext/surface/variantutils.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import gdext/gdinterface/[variants, exceptions]
import gdext/gdinterface/[variants, exceptions, extracommands]

import gdext/core/[typeshift, builtinindex]
import std/[strformat]
Expand Down
13 changes: 12 additions & 1 deletion testproject/runtime/nim/src/classes/gdextnode/typedef.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import testutils
import std/tables
import std/[tables, strutils]

import gdext
import gdext/gdinterface/native
Expand Down Expand Up @@ -48,6 +48,14 @@ proc test_Object(self: GDExtNode) =
check self == Engine.getSingleton(classname GDExtNode).as GDExtNode
check self == GDExtNode

test "stringify":
let obj1: Object = instantiate Object
let obj2: Object = Input.singleton
check ($obj1).startsWith "Object"
check ($obj2).startsWith "Input"

destroy obj1

proc test_RefCounted(self: GDExtNode) =
suite "RefCounted":
test "reference counting":
Expand Down Expand Up @@ -85,6 +93,9 @@ proc test_Node(self: GDExtNode) =

check node == node2

test "stringify":
check ($self).startsWith "GDExtNode"

proc test_Resource(self: GDExtNode) =
suite "Resource":
test "reference counting":
Expand Down

0 comments on commit b49e18e

Please sign in to comment.