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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Locked
User avatar
Guard13007
Party member
Posts: 132
Joined: Sat Oct 25, 2014 3:42 am
Location: Internet, USA
Contact:

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

Post 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. :)
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

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

Post 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?
Attachments
oop_apr27.love
(2.75 KiB) Downloaded 132 times

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

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

Post 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. :)
Last edited by pedrosgali on Thu Apr 28, 2016 2:07 pm, edited 1 time in total.

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

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

Post 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:

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
fixylol
Prole
Posts: 21
Joined: Thu Feb 25, 2016 2:16 am

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

Post by fixylol »

how do i make a double not return "1.0e+010" (or something like that) when i call it?
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
Tst_
Prole
Posts: 7
Joined: Tue Dec 23, 2014 10:11 pm

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

Post 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?
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

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

Post 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.
Trebgarta
Prole
Posts: 33
Joined: Mon May 23, 2016 5:21 pm

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

Post by Trebgarta »

Would that limit count for memory allocated by code written in C loaded through ffi?
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

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

Post 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
Locked

Who is online

Users browsing this forum: BrotSagtMist, keharriso, Yolwoocle and 225 guests