amirrajan / rubymotion-applied

RubyMotion documentation provided by the community. Submit a pull request to the docs for a free one year indie subscription.
Apache License 2.0
49 stars 9 forks source link

Vector_int2 trouble in GameplayKit GKGridGraph #115

Open calicoday opened 5 years ago

calicoday commented 5 years ago

Can’t seem to make GKGridGraph or GKGridGraphNode work because the position needs to be supplied as a vector_int2 and they’re getting garbled somehow.

In app simplest-mac, generated by motion create simplest-mac —template=osx and adding app.frameworks << "GameplayKit” to the Rakefile, I stick the following chunk in AppDelegate after buildWindow:

puts        
posvec = Pointer.new("{_vector_int2=ii}", 1)
puts("raw posvec[0] #{posvec[0]}, inspect: #{posvec[0].inspect}")
posvec[0] = [0, 0]
puts("=[0,0] posvec[0] #{posvec[0]}, inspect: #{posvec[0].inspect}")
altvec = Pointer.new("{_vector_int2=ii}", 1)
puts("raw altvec[0] #{altvec[0]}, inspect: #{altvec[0].inspect}")
altvec[0] = [2, 3]
puts("=[2,3] altvec[0] #{altvec[0]}, inspect: #{altvec[0].inspect}")
puts

#graph_wrong_type = GKGridGraph.alloc.initFromGridStartingAt(posvec,  width: 33,
#height: 28, diagonalsAllowed: false)
# => app/app_delegate.rb:65:in `applicationDidFinishLaunching:': expected 
#   instance of `Vector_int2', got `#<Pointer:0x100349570>' (Pointer) (TypeError)

graph = GKGridGraph.alloc.initFromGridStartingAt(posvec[0],  width: 33,
    height: 28, diagonalsAllowed: false)
print("graph pos.inspect: #{graph.gridOrigin.inspect}, ")
print("w: #{graph.gridWidth}, ")
print("h: #{graph.gridHeight}, ")
puts("diags: #{graph.diagonalsAllowed}")

node = graph.nodeAtGridPosition(posvec[0])
puts("graph node at posvec[0]: #{node.inspect}")
puts

# try position of simple [0, 0], instead of posvec[0]
graph_simple_arr = GKGridGraph.alloc.initFromGridStartingAt([0,0],  width: 33,
    height: 28, diagonalsAllowed: false)
print("graph [0,0] pos.inspect: #{graph_simple_arr.gridOrigin.inspect}, ")
print("w: #{graph_simple_arr.gridWidth}, ")
print("h: #{graph_simple_arr.gridHeight}, ")
puts
puts("  diags: #{graph_simple_arr.diagonalsAllowed}")
puts

# try posvec[0] and [0,0] on a different gkgrid method
noth = GKGridGraphNode.nodeWithGridPosition(posvec[0])
puts("noth: #{noth.inspect}, pos: #{noth.gridPosition.inspect}")
noth = GKGridGraphNode.nodeWithGridPosition([0,0])
puts("[0,0] noth: #{noth.inspect}, pos: #{noth.gridPosition.inspect}")

then rake gets:

raw posvec[0] #<Vector_int2:0x100753c40>, inspect: #<Vector_int2 x=0 y=0>
=[0,0] posvec[0] #<Vector_int2:0x100379350>, inspect: #<Vector_int2 x=0 y=0>
raw altvec[0] #<Vector_int2:0x100379c70>, inspect: #<Vector_int2 x=0 y=0>
=[2,3] altvec[0] #<Vector_int2:0x10037a3e0>, inspect: #<Vector_int2 x=2 y=3>

graph pos.inspect: #<Vector_int2 x=3648992 y=1>, w: 0, h: 33, diags: false
graph node at posvec[0]: nil

graph [0,0] pos.inspect: #<Vector_int2 x=3653024 y=1>, w: 0, h: 33, 
  diags: false

noth: #<GKGridGraphNode:0x100754220>, pos: #<Vector_int2 x=7681712 y=1>
[0,0] noth: #<GKGridGraphNode:0x1007531b0>, pos: #<Vector_int2 x=7680512 y=1>

The vector_int2s seem to be created and set nicely. And in fact, the method calls seem to take a Pointer value or [x,y] equally happily. But the graph or graphnode instance created seems to be garbled.

The same code chunk added to a simplest-ios RubyMotion app gives the same results.

FWIW, my guess at an equivalent chunk of Swift added to AppDelegate of a simplest xcode app for Mac:

var posvec = vector_int2(0, 0)
print("raw posvec.x: \(posvec.x), posvec.y: \(posvec.y), \(String(describing: posvec))")
posvec.x = 0
posvec.y = 0
print(".x=0,.y=0 posvec.x: \(posvec.x), posvec.y: \(posvec.y), \(String(describing: posvec))")
var altvec = vector_int2(0, 0)
print("raw altvec.x: \(altvec.x), altvec.y: \(altvec.y), \(String(describing: altvec))")
altvec.x = 2
altvec.y = 3
print(".x=2,.y=3 altvec.x: \(altvec.x), altvec.y: \(altvec.y), \(String(describing: altvec))")
print()

let graph = GKGridGraph(fromGridStartingAt: posvec, width: 33, height: 28, diagonalsAllowed: false)
print("graph pos.inspect: \(graph.gridOrigin), w: \(graph.gridWidth), h: \(graph.gridHeight), diags: \(graph.diagonalsAllowed)")
let node = graph.node(atGridPosition: posvec)
print("graph node at posvec[0]: \(String(describing: node))")
print()

gets:

raw posvec.x: 0, posvec.y: 0, int2(0, 0)
.x=0,.y=0 posvec.x: 0, posvec.y: 0, int2(0, 0)
raw altvec.x: 0, altvec.y: 0, int2(0, 0)
.x=2,.y=3 altvec.x: 2, altvec.y: 3, int2(2, 3)

graph pos.inspect: int2(0, 0), w: 33, h: 28, diags: false
graph node at posvec[0]: Optional(<GKGridGraphNode: 0x600000224260>)

which is more what I was expecting.