Page 62 of 91

Re: "Questions that don't deserve their own thread" thread

Posted: Sun Apr 17, 2016 5:38 am
by Guard13007
@Vimm I don't see anything wrong there. Double check your typing and what's the exact error / what line does it error on?

Also, did you know you can just

Code: Select all

color = {255, 255, 255} -- r, g, b
love.graphics.setColor(color)
?

Instead of trying to name your RGB values, just have numerically indexed RGB. :)

Re: "Questions that don't deserve their own thread" thread

Posted: Thu Apr 28, 2016 2:25 am
by Beelz
So my brother wants to learn how to code(he does GFX) and asked me for help... I figured where better to start than Lua and Love?

So, we've been going through the basics and he wanted to know more about OOP. Now the problems I face are simple:
  • I'm a bad teacher.
  • I suck.
Would anyone be willing to look this over and give me(and him) a quick hand?

Re: "Questions that don't deserve their own thread" thread

Posted: Thu Apr 28, 2016 1:32 pm
by pedrosgali
I'll give it a go, you weren't very specific with the problem but I'll tell you the same things I tell most people I'm trying to teach OOP to.

Code: Select all

--Let's make a class library--
--We first make a table as our base class object..
local class = {}

--Then we give it an extension function, this is what you call to make your new class.
--The setmetatable function is what ties the new class to the class library and any sub classes you make

function class:extend(subClass)
  return setmetatable(subClass or {}, {__index = self})
end

--Then add a constructor function, this function is what you call to make a new copy of your object think of it like a big rubber stamp that stamps out objects.
--Notice the (...) at the end this will take any arguments you pass the function and pass them straight to the objects init function...

function class:new(...)
  local copy = setmetatable({}, {__index = self})
  return copy, copy:init(...)
end

--The init function you would write for most objects themselves because it is where you set up your objects.
--I put one here so it won't crash if I forget to write one
function class:init(...) end

--Always remember to return your table!
return class
Now we have a class library let's start making some classes...

Code: Select all

--first off require the class library...
local class = require "class"

--Now let's make a shape...
local shape = class:extend() --This is a base class so we don't pass a parent class

--First we write the init function that will be called as the object gets created...
--Notice we don't have to write the function shape:new(...) as that is in the class library and any function calls to shape will
--look in the shape table first and if the function is not found it will look to the parent which in this case is class

function shape:init(x, y, w, h, col)
  self:setPosition(x, y)
  self:setSize(x, y)
  self.col = col
end

--Let's write those functions we just called as well :P

function shape:setPosition(x, y)
  self.x, self.y = x, y
end

function shape:setSize(w, h)
  self.w, self.h = w, h
end

function shape:getColour()
  return self.col[1], self.col[2], self.col[3], self.col[4]
end

--Also something to draw to the screen...

function shape:draw()
  love.graphics.setColor(self:getColour())
  love.graphics.rectangle("fill", self.x - self.w / 2, self.y - self.h / 2, self.w, self.h)
end

return shape
So our rectangle shape is cool but what if we want a circle?

Code: Select all

  --first off require the shape library
  local shape = require "shape"
  
  --Start making a circle...
  local circle = shape:extend()
  
  --Circles need fewer arguments than the basic shape to define them so let's write a new init...
  function circle:init(x, y, rad, col)
    self:setPosition(x, y) --We can still use any functions we wrote in shape so our circle will use setPosition() from shape
    self.radius = rad
    self.col = col
  end
  
  --We probably need a new Draw too as rectangles can be a bit too quadrilateral to truly capture the essence of a circle...
  
  function circle:draw()
    love.graphics.setColor(self:getColour())
    love.graphics.circle("fill", self.x, self.y, self.rad)
  end
  
  return circle
So as you can hopefully see from the code above, metatables that we set in the class lib are like an "If you can't find it here then look here" inheritance system. Because shape is a class and circle is a shape, circle can see any functions made in shape OR class so even though we never wrote the function for circle:new we can still call it...

Code: Select all

local circle = require "circle"

function love.load()
  myCircle = circle:new(50, 50, 10, {255,255,255,255})
end

function love.draw()
  myCircle:draw()
end
This is because it looks for circle:new and can't find one so it checks the metatable and sees that shape is a thing! I'll look there.
Sadly nothing under shape:new either but thanks to shape's metatable I now know class is a thing! Upon looking in class it finds the function new and because of the ":" it knows that the self being referred to in that function is circle without us having to write.

myCircle = circle.new(circle, 50, 50, 10, {255,255,255,255})

So basically using the ":" for our objects instead of the "." means we don't have to write the name twice.

It's important that I stress that I have not run this code, It is written more as a guide and totally off the top of my head but I hope it helps you and your brother. :)

Re: "Questions that don't deserve their own thread" thread

Posted: Thu Apr 28, 2016 2:01 pm
by Beelz
Sorry for being vague in my post about what I was asking... Anyways thank you for that, it will surely help us! :ultrahappy:

Re: "Questions that don't deserve their own thread" thread

Posted: Sat Apr 30, 2016 2:21 pm
by fixylol
how do i make a double not return "1.0e+010" (or something like that) when i call it?

Re: "Questions that don't deserve their own thread" thread

Posted: Sat Apr 30, 2016 3:20 pm
by pgimeno
That's a vague question. If that's its value, that's what it's going to return.

If you mean when displaying it, you can format the float e.g. with string.format("%.5f", yourfloat) for 5 decimal places. That will return the string "10000000000.00000" for the above float.

Re: "Questions that don't deserve their own thread" thread

Posted: Wed Jun 22, 2016 3:14 pm
by Tst_
I've found out recently that Love crashes if I use more than 2Gb of ram on the 64bit version. Any ideas as to why?

Re: "Questions that don't deserve their own thread" thread

Posted: Wed Jun 22, 2016 4:32 pm
by slime
LuaJIT has a hard limit of 2GB for Lua-allocated memory (tables, strings, numbers, etc.) The limit doesn't apply to memory created by LOVE though, e.g. love objects like ImageData don't count towards that limit.

Re: "Questions that don't deserve their own thread" thread

Posted: Wed Jun 22, 2016 4:44 pm
by Trebgarta
Would that limit count for memory allocated by code written in C loaded through ffi?

Re: "Questions that don't deserve their own thread" thread

Posted: Wed Jun 22, 2016 4:51 pm
by slime
It would count for things allocated via ffi.new, but not for things allocated via ffi.C.malloc, although you have to explicitly free the malloc'd memory when it's garbage-collected: http://luajit.org/ext_ffi_api.html#ffi_gc