Jump to content

Inventory, item placing help


Tekken

Recommended Posts

Posted

Hello guys, I want to create an inventory made out of pictures, all good and nice,  but, well let's say that math wasn't my favorit class in school and I'm a little confuse in DX functions cause they require a lot of calculus that I don't know, my question is how can I make a inventory like this (see the picture below) ?

4xMKbRWTrrGbJwKIovTWd6PF2uQuW948rtEkBWO9

A - Large items.

B - Small items.

What I want to know is how to make the pictures place like in the image?

 

Thank you!

Posted

You'll have to use the width of the panel and two variables as X offset and Y offset, so you can easily change the rectangle's position.

1. You start your X offset and Y offset at 0.
2. You draw the first item, and then you add the width of the first item to the X offset + some pixels.
3. If you have enough space to draw the next item in the same line, you repeat step 2.
4. If you don't, you reset the X offset and add the height of the items + some pixels to the Y offset.

Use a loop to draw them.

Posted
local sX, sY = guiGetScreenSize()
local bgW, bgH = 390, 675 -- Background item size
local bgX, bgY = (sX-bgW)/2, (sY-bgH)/2 -- Background pos
local lW = 160 -- Large item width
local sW = 80 -- Small item width

local itemTable = {
	"large",
	"large",
	"large",
	"small",
	"small",
	"small",
	"small"
}

function drawItems()
	dxDrawImage(bgX, bgY, bgW, bgH, "background.png")
	local offsetX, offsetY = 30, 30
	for i = 1, #itemTable do
		outputChatBox("i: " .. i .. " X: " .. offsetX .. " Y: " .. offsetY)
		if bgW - offsetX < (itemTable[i] == "large" and lW or sW) then
			offsetX = 30
			offsetY = offsetY + 90
		end
		dxDrawImage(bgX+offsetX, bgY+offsetY, itemTable[i] == "large" and lW or sW, 80, itemTable[i] .. "item.png")
		offsetX = offsetX + (itemTable[i] == "large" and lW or sW)+10
	end
end
addEventHandler("onClientRender", root, drawItems)

Try to understand the code. If you have issues, feel free to reply.

  • Thanks 1
Posted

Thank you, exactly what I needed.

I don't understand this part:

(itemTable[i] == "large" and lW or sW)

If you can explain it that'd be really nice.

Posted (edited)

That is basically an inline-if statement based on ternary operators (similar to : ? in many other languages including PHP, C, C++, etc)

See more: https://en.wikipedia.org/wiki/%3F:https://en.wikipedia.org/wiki/%3F:#Lua

Here's my attempt at explaining it:

AND:
If the left operand is a logical trueand returns the RIGHT operand.
If the left operand is a logical falseand returns the LEFT operand.
OR:
If the left operand is a logical trueor returns the LEFT operand.
If the left operand is a logical falseor returns the RIGHT operand.

Following these laws, the statement is executed in this way:

where itemTable does equal "large":

itemTable[i] == "large" evaluates true;
true and lW -- left operand evaluates into a logical true, and therefore and returns the right operand, lW;
lW or sW -- left operand evaluates into a logical true, and therefore or returns the left operand, lW;
-- The result of this evaluation is lW.

where itemTable does NOT equal "large":

itemTable[i] == "large" --evaluates false;
false and lW -- left operand evaluates into a logical false, and therefore and returns the left operand, false;
false or sW -- left operand evaluates into a logical false, and therefore or returns the right operand, sW;
--The result of this evaluation is sW.

Basically,

function iif(a, b, c)
  return a and b or c
end

--equivalent to
function iif(a, b, c)
  if a then
    return b
  else
    return c
  end
end

 returns b if a is true, otherwise c.

Edited by MrTasty
  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...