## Gspöt - retained GUI lib

pgimeno
Party member
Posts: 2132
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Gspöt - retained GUI lib

Jack Dandy wrote:
Fri Jun 07, 2019 3:13 pm
Quick question: Is there a way to change an IMAGE'S color values?
If you mean tint, you can override the Gspot.util.drawimg method. The default is:

Code: Select all

	drawimg = function(this, pos)
local r, g, b, a = love.graphics.getColor()
setColor(255, 255, 255, 255)
love.graphics.draw(this.img, (pos.x + (pos.w / 2)) - (this.img:getWidth()) / 2, (pos.y + (pos.h / 2)) - (this.img:getHeight() / 2))
love.graphics.setColor(r, g, b, a)
end,

setColor is the internal version-agnostic function, but unless you need compatibility with 0.9.1+, you don't need it; you can use love.graphics.setColor instead.

Santvarg
Prole
Posts: 7
Joined: Sat May 18, 2019 12:42 pm

### Re: Gspöt - retained GUI lib

Been awhile but thanks, that worked out after some tweaking to keep it from doing the coordinate translation for the gui when it shouldnt, but this seems to be working fine. Menu buttons work, and the hidden buttons on npcs work properly and are clickable even after moving the player around. Thanks alot for this, this problem had brought me to a screeching halt

Code: Select all

function gui.getmouse(this)
if gamestate == 1 then
camera:set()
local x, y = love.graphics.inverseTransformPoint(love.mouse.getPosition())
camera:unset()
return x, y
else
local x, y = love.graphics.inverseTransformPoint(love.mouse.getPosition())
return x, y
end
end

love.mousepressed = function(x, y, button)
if gamestate==1 then
local x, y = gui.getmouse()
gui:mousepress(x, y, button)
end
gui:mousepress(x, y, button)
end

function love.draw()
...
local x, y = love.graphics.inverseTransformPoint(love.mouse.getPosition())
gui.getmouse(x, y)
...
end

Santvarg
Prole
Posts: 7
Joined: Sat May 18, 2019 12:42 pm

### Re: Gspöt - retained GUI lib

Now having a different problem related to this library:

Code: Select all

function npcspawn()
for i=1, #curmapnpc[1] do
if #npcs[i] < curmapnpc[1][i][2] then --not important for this case
for o=#npcs[i]+1, curmapnpc[1][i][2] do
...
npcbutton.i = {}
npcbutton.i.o = gui:hidden("test", {x = npcposx, y = npcs[i][o][3]-(npcimgx[i]/2), w = npcimgx[i], h = npcimgx[i]})
function npcbutton.i.o:click(this, x, y)
playertargetid=i
playertargetnpc=o
end
function npcbutton.i.o:enter(this)
love.mouse.setCursor(pointcursor)
end
function npcbutton.i.o:leave(this)
love.mouse.setCursor(defaultcursor)
end
end
end
end
end

gui:rem(npcbutton.playertargetid.playertargetnpc)

This code causes an error

Code: Select all

main.lua:525: attempt to index a nil value
Despite that, I remove the npcs from their own table using the same exact global variables
any variation in format ive tried doesnt work, tried using npcbutton[o]:click(this, x, y) and using table.remove() but some funky errors pop up on the first part

everything else works, player destroying npcs removes them from their table, they go poof and respawn function takes over some time later and spawns a new one, but i can never get the buttons to go poof too

pgimeno
Party member
Posts: 2132
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Gspöt - retained GUI lib

Santvarg wrote:
Wed Jul 10, 2019 9:11 am
Now having a different problem related to this library:

Code: Select all

function npcspawn()
for i=1, #curmapnpc[1] do
if #npcs[i] < curmapnpc[1][i][2] then --not important for this case
for o=#npcs[i]+1, curmapnpc[1][i][2] do
...
npcbutton.i = {}
npcbutton.i.o = gui:hidden("test", {x = npcposx, y = npcs[i][o][3]-(npcimgx[i]/2), w = npcimgx[i], h = npcimgx[i]})
function npcbutton.i.o:click(this, x, y)
playertargetid=i
playertargetnpc=o
end
function npcbutton.i.o:enter(this)
love.mouse.setCursor(pointcursor)
end
function npcbutton.i.o:leave(this)
love.mouse.setCursor(defaultcursor)
end
end
end
end
end

gui:rem(npcbutton.playertargetid.playertargetnpc)

This code causes an error

Code: Select all

main.lua:525: attempt to index a nil value
There's too little information here to get an idea of what you're trying to do or where the problem is. For example, I don't know what line 525 is. Also, I don't know if 'npcbutton' is defined as a local or not, and what value it receives.

What seems apparent is that this is not a problem in Gspot. npcbutton.i is equivalent to npcbutton["i"], which uses the string "i" as key, not the value of the variable i. You seem to expect it to behave as if it was npcbutton[i].

Furthermore, the parameter list for the functions defined in the snippet you have pasted uses colon syntax, and additionally includes a 'this' parameter. That should not matter in this case, because there are no more parameters and you're not using it anyway, but using that construction could cause problems in some other part of the code.

Santvarg wrote:
Wed Jul 10, 2019 9:11 am
Despite that, I remove the npcs from their own table using the same exact global variables
any variation in format ive tried doesnt work, tried using npcbutton[i][o]:click(this, x, y) and using table.remove() but some funky errors pop up on the first part
If you want i and o to be indices into the npcbutton table, then that's the right syntax, so don't expect that changing things at random will solve your problem. If you get "funky" errors that way, then these should be addressed. In that little snippet, you have again a problem with the parameter list, and in this case it does have potential to cause problems. The parameter list of the function is <table>, <x>, <y>. If you use colon syntax, an implicit 'self' parameter is inserted, so in your case, npcbutton[i][o]:click(this, x, y) is equivalent to npcbutton[i][o].click(self, this, x, y) which has an extra parameter that should not be there.

Santvarg
Prole
Posts: 7
Joined: Sat May 18, 2019 12:42 pm

### Re: Gspöt - retained GUI lib

line 525 is the gui:rem line at the bottom, and npcbutton = {}, mb, i forgot it was the load function and not there. This is the only function that addresses it otherwise
I thought

Code: Select all

[i][o]
was the same as .i.o , from my errors, and what you say, its pretty obvious they're not, woops lol
Is there another way to set functions for each instance of button other than using a colon ? Im just using what ive learned from the documentation. using:

Code: Select all

function npcbutton[i][o]:click(x, y)
, gives me the error:
syntax error; ( expected near [
on that line, same with a comma. Cant make sense of this
before, i tried setting it as

Code: Select all

npcbutton[i][o]:click = function(x, y)
this caused the funky errors i was talking about. Using a comma instead will run, but crashes with the error:
Gspot.lua:380: attempt to index local 'element' (a nil value)
with a traceback to this line:

Code: Select all

gui:rem(npcbutton[playertargetid][playertargetnpc])
if the destroyed npc(and therefore element) isn't the last in the table. note: the playertargetid and playertargetnpc are the i and o values for that specific npc, respectively

What im trynna do here is, as every npc is spawned, they spawn as

Code: Select all

NPCS[i][o]
, where i is the type of npc and o is the number of npc.
when they're killed, they are deleted with table.remove, which moves all other entries in that npc type down to fill the empty entry in the table. This works as intended as far as i can tell, any destroyed npcs are properly removed and the new ones spawn in the now vacant places at the end of the table

but when each npc is spawned, a hidden button is spawned with the same i and o values, but under npcbutton. a specific button with npc specific information, ie the position of the button is set as the same values as the npc's coordinates at creation (and properly edited as needed later)
what i wanna do is the same as the NPCS table, remove the button that was 'attached' to that npc, while keeping the other buttons in the corresponding positions in their table. Such that npc [1][2] has a button at npcbutton[1][2], and if that npc becomes npc[1][1] due to 1st npc death, the button also moves to npcbutton[1][1] after the original [1][1] button is removed from the table. This way the references for npc and button are interchangable between the two for purposes of selection

sorry for he lots of code blocks, kept setting stuff as italic

pgimeno
Party member
Posts: 2132
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Gspöt - retained GUI lib

Santvarg wrote:
Thu Jul 11, 2019 1:16 am
Is there another way to set functions for each instance of button other than using a colon ?
For normal functions you use dots, not colons. You use a colon just before the last element when you want to call a method in an instance of an object, because that passes the instance as an implicit parameter. In other words, if 'a' is an object (i.e. a table):

Code: Select all

function a:b(x, y)
end

-- is the same as:

function a.b(self, x, y)
end

Colon syntax at the time of calling a function, passes whatever is before the colon to the function.

Take a look at this chapter of the PIL, it explains the difference between dot and colon syntax in the context of OOP:

https://www.lua.org/pil/16.html

Santvarg wrote:
Thu Jul 11, 2019 1:16 am
Im just using what ive learned from the documentation. using:

Code: Select all

function npcbutton[i][o]:click(x, y)
, gives me the error:
syntax error; ( expected near [
on that line, same with a comma.
That's expected, yeah. You can't add sub-indices in function declarations, i.e. you can't do this:

Code: Select all

function x[1]() ...

But as you have noted, you can do

Code: Select all

x[1] = function () ...


Santvarg wrote:
Thu Jul 11, 2019 1:16 am
Cant make sense of this
before, i tried setting it as

Code: Select all

npcbutton[i][o]:click = function(x, y)
this caused the funky errors i was talking about. Using a comma instead will run, but crashes with the error:
Gspot.lua:380: attempt to index local 'element' (a nil value)
with a traceback to this line:

Code: Select all

gui:rem(npcbutton[playertargetid][playertargetnpc])
I don't think it will let you to place a colon in the assignment statement. Anyway, try this instead:

Code: Select all

npcbutton[i][o].click = function(self, x, y)

Santvarg wrote:
Thu Jul 11, 2019 1:16 am
sorry for he lots of code blocks, kept setting stuff as italic
No need to be sorry, it's quite readable.

Santvarg
Prole
Posts: 7
Joined: Sat May 18, 2019 12:42 pm

### Re: Gspöt - retained GUI lib

Thanks for the quick replies
using

Code: Select all

npcbutton[i][o].click = function(self, x, y)
(wasnt using self before,oversight)
returns the same error:

Code: Select all

Gspot.lua:380: attempt to index local 'element' (a nil value)
--with a traceback to this line:

gui:rem(npcbutton[playertargetid][playertargetnpc])
when it's not the last npc/button. confused because none of my elements are set as locals
edit: this error happens immediately after an npc is destroyed, other than the 5th/last one; when the game tries to remove the gui element of that npc, and before the npc is removed

pgimeno
Party member
Posts: 2132
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Gspöt - retained GUI lib

Santvarg wrote:
Thu Jul 11, 2019 1:57 am
Thanks for the quick replies
using

Code: Select all

npcbutton[i][o].click = function(self, x, y)
(wasnt using self before,oversight)
returns the same error:

Code: Select all

Gspot.lua:380: attempt to index local 'element' (a nil value)
--with a traceback to this line:

gui:rem(npcbutton[playertargetid][playertargetnpc])
Line 380 just tries to use the passed element parameter, so it looks like this means you're passing nil to it. Try printing npcbutton[playertargetid][playertargetnpc] right before calling gui:rem. If you get nil, you'll need to debug that.

Santvarg
Prole
Posts: 7
Joined: Sat May 18, 2019 12:42 pm

### Re: Gspöt - retained GUI lib

You're right, while playertargetid and playertargetnpc have values
npcbutton[playertargetid][playertargetnpc]
is nil

Means something's up with npcbutton's definitions
- it was the

Code: Select all

npcbutton[i] = {}
line, it was setting that every time the 'o' for loop iterated(thats alot), big bad
doesnt crash anymore, no matter which npc is destroyed
I think the remaining problem is not that much to do about the gui
every npc after the one destroyed, when clicked, addresses the new npc in that slot
ie: destroy npc 3, click the new npc 3(was npc 4), but it selects 4 instead(which was 5). clicking npc 4 (used to be 5) selects the new npc, if spawned. The new npc(5) selects itself as intended

Got a feeling its the click function not being updated, working on it now
thanks for the help!

edit:
thought i fixed it with

Code: Select all

if playertarget[4]<=0 then --if hp=0
gui:rem(npcbutton[playertargetid][playertargetnpc]) --removes button
table.remove(npcs[playertargetid], playertargetnpc) --deletes npc and moves table
for i=playertargetnpc, #npcs[playertargetid] do -- new part
npcbutton[playertargetid][i+1].click = function(self, x, y) -- redefines the click function of each button
playertarget=npcs[playertargetid][i]
playertargetnpc=i
playert=1
end
end
playercombat=0
playertarget=nil
player.fangle = player.angle
--do something spectacular, like blow up graphics
end

the problem was the click function wasnt being updated, a fault of my own, so the function:
starts at the position on the table where the npc used to be, and stops at the map's max number for that type of npc
changes the click function of the NEXT npc's button, to reference the npc that is now(after table.remove) in the spot the destroyed npc was in. and since the loop starts at the point in the table where changes occur, i can just use i to redefine all the things

zenith
Prole
Posts: 13
Joined: Sat Oct 12, 2013 5:44 pm

### Re: Gspöt - retained GUI lib

Hey! How to update label text on button click? Something like that:

Code: Select all

function love.load()
number=0
text=gui:text(number)

button=gui:button('Click me')
function button.click(this,x,y)
number=number+1
end
end

ping pong

### Who is online

Users browsing this forum: bludburn and 26 guests