Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Gosu::Grid, passing draw an array
- - By Beamerr Date 2017-08-23 16:13
Hi all,

I get a type error cannot pass array into integer. I understand what's going on but I don't know how to effectively pass draw an array so it draws each image. Any hair keeping strategies appreciated?

class Cell < Gosu::Grid::Cell

  def size
    object.width
  end
  
private

  def object
    Dir.glob("PNG-cards-1.3/*.png").each do |file|
          
      @object ||= Gosu::Image.new(window, "PNG-cards-1.3/#{file}", true)
    end
  end
end

class SequenceGame < Gosu::Window
 
  def initialize width=1280, height=800, fullscreen=true
    super
   
    @grid = Gosu::Grid.new(self)
    @grid.default_cell = Cell.new(self, 0, 0)

  end

  def button_down id
    close if id == Gosu::KbEscape
  end

  def update
   
  end

  def draw
    @grid.draw
  end

end
Parent - - By RunnerPack Date 2017-08-25 00:51 Edited 2017-08-25 01:17
(Note: The "Showcase" forum is for showing off completed projects. Please start posting your questions in the "Exchange" forum. Thanks.)

This is a Ruby question, not a Gosu one. You might want to take a look at some Ruby tutorials before continuing with Gosu. The language is both powerful and readable, but it has some quirks and special ways of doing things that will take some getting used to.

The way to do something with each item in an array is to use it's Enumerable methods. One of these is Array#each. It can be used like so:

@grid.each { |g| g.draw }

Look for documentation on Ruby's Array class (or the Enumerable mixin module) for more information.

EDIT: Note that this assumes your Gosu::Grid class behaves as an Array (or also mixes in Enumerable), which there is no evidence of in the above code. Since I can't find any documentation for the Grid class, I don't know if this is the case.

EDIT2: Are you using this? https://github.com/shemerey/gosu_grid

If so, it looks like the Grid class does respond to #draw as you would expect, so you must be using it incorrectly somewhere else. What line actually gives the "cannot pass array into integer" error?
Parent - - By jlnr (dev) Date 2017-08-25 07:28
I've moved both topics to Gosu Exchange.

TIL that there's a gosu_grid gem :)
Parent - - By Beamerr Date 2017-08-29 11:36 Edited 2017-08-29 12:17
Sorry, wrongly posted. Thanks for getting back to me.

I do understand that I need enumerable to effectively deal with 'each' image which I had tried. That piece of code I gave was the probably the worst example.

Here's the section I've been working on including some commented stuff which also doesn't work.

Yeah I'm using the link you provided in EDIT2.

I'm actually getting a runtime error: cannot read 10_of_clubs.png on the current code (Dir.new). I can't use glob because it returns a string.

I don't understand why I can't just open a file pass it to and array and then read with 'each'.

As you say maybe Gosu::Grid doesn't work in this way. I don't understand why it doesn't? Why wouldn't it be designed to deal with an array.

Do you think the gem will make any difference?

class Cell < Gosu::Grid::Cell

  def size
    object.each do |image|
      image.width
    end
  end
  
=begin
     @object = Array.new
     Dir.glob("PNG-cards-1.3/*.png") do |x|
      @object.push Gosu::Image.new(x) 
     end

    @object.each do |x|
      @object.push Gosu::Image.new(window, x, true)
    end
=end

private

  def object

    Dir.new("PNG-cards-1.3").each do |file|
      next if File.directory? file
        @object ||= Gosu::Image.new(window, file, false)
    end
  end
end


class SequenceGame < Gosu::Window
 
  def initialize width=1280, height=800, fullscreen=true
    super
   
    @grid = Gosu::Grid.new(self)
    @grid.default_cell = Cell.new(self, 0, 0)
   

  end

  def button_down id
    close if id == Gosu::KbEscape
  end

  def update
   
  end

  def draw
    @grid.draw
  end

end


Thanks again!
Parent - By jlnr (dev) Date 2017-09-02 16:22
I haven't looked at the source code of the grid gem, but drawing a grid of things isn't hard to do in pure Ruby/Gosu. Maybe that's easier than getting the grid library to work. The Cptn Ruby example game contains a tiled map that works like a grid:

https://github.com/gosu/gosu-examples/blob/master/examples/cptn_ruby.rb
Up Topic Gosu / Gosu Exchange / Gosu::Grid, passing draw an array

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill