How would I be able to do this?

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.
Post Reply
louie999
Prole
Posts: 46
Joined: Fri Mar 06, 2015 9:01 am

How would I be able to do this?

Post by louie999 »

So I'm trying to create so methods here:

Code: Select all

local gui = {}
local gui.__index = gui
local buttontypes = {
"button", "text", "frame"
}
local lg = love.graphics
local lm = love.math

-- Private functions 

local function checkType(b)
	if type(buttontype) ~= "string" then
		error("[GUI]button type must be a string", 2)
	end
	for k, v in pairs(buttontypes) do
		if b == buttontypes then
			return true
		end
	end
	error("[GUI]"..b.." is an invalid button type")
end	

local function checkExist(name)
	for k, v in pairs(guihandler) do
		if name == v.name then
			return true
		else
			error("[GUI]"..name.." doesn't exist")
			return false
		end
	end
end

local function checkstring(s)
	if type(s) ~= "string" then
		error("[GUI]Must be a string", 2)
	end
	return true
end

local function checknumber(n)
	if type(n) ~= "number" or n < 0 then
		error("[GUI]Must be a positive number", 2)
	end
	return true
end
	
-- Public functions, methods
function gui.New(_type_, x, y, text)
	checkType(_type_)
	local newgui = {}
	setmetatable(newgui, gui)
	newgui.x = x or 0
	newgui.y = y or 0
	newgui.width = 50
	newgui.height = 50
	newgui.text = text or ""
	newgui.onclick = function() end
	newgui.onmousein = function() end
	newgui.onmouseout = function() end
	return newgui
end

function gui:SetPos(x, y)
	checknumber(x)
	checknumber(y)
	self.x = x
	self.y = y
end

function gui:SetX(x)
	checknumber(x)
	self.x = x
end

function gui:SetY(y)
	checknumber(y)
	self.y = y
end

function gui:SetText(text)
	self.text = text or ""
end
So here's my question, if say, I create 3 instances using gui.New() like:

Code: Select all

a = gui.New("button", 50, 50, "Iam a button!")
b = gui.New("button", 50, 100, "Iam also button!")
c = gui.New("button", 75, 150, "Iam not a button")
How can I iterate over all of those instances, like with a function:

Code: Select all

function gui:updateAll(dt)
--code--
end
it would iterate over all of those created instances, how can I do that? :death:

Code: Select all

fun = true
school = true

function isItFun()
    if school then
       fun = false
    end
    if not fun then 
       me:explode()
    end
end
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How would I be able to do this?

Post by bartbes »

Well, somewhere in your new function, you would add the newly created object to a table.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How would I be able to do this?

Post by ivan »

Another thing to note is:

Code: Select all

   for k, v in pairs(buttontypes) do
      if b == buttontypes then
         return true
      end
   end
And

Code: Select all

   for k, v in pairs(guihandler) do
      if name == v.name then
         return true
      else
         error("[GUI]"..name.." doesn't exist")
         return false
      end
   end
When you use tables, these two loops can be removed altogether, for example:

Code: Select all

local buttontypes = { button = true, text = true, frame = true }

assert(buttontypes[b], "unknown button type: " .. tostring(b))
louie999
Prole
Posts: 46
Joined: Fri Mar 06, 2015 9:01 am

Re: How would I be able to do this?

Post by louie999 »

Thx bartbes and ivan I will try those out :D

Code: Select all

fun = true
school = true

function isItFun()
    if school then
       fun = false
    end
    if not fun then 
       me:explode()
    end
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 214 guests