Jump to content

[HELP] change indexes in a table


opnaiC

Recommended Posts

Hello,

I am scripting a new chat and the stuff I scripted is working. But now I have a problem. In my chat I am printing the first 13 messages in dxDraw. But now I want all indexes to +1, so if there come a 14th message it gets visible.

local offset = 14

function drawChat()
    for i, v in ipairs (chatbox) do
	    if (i < offset) then
	        exports.login:dxDrawBorderedText(v, 36, (210 - ((offset-i)*15)), 411, 210, tocolor(255, 255, 255, 255), 1.00, "default-bold", "left", "top", false, false, false, false, false)
        elseif (i == 14) then
            --what to add ? I want all index to +1, so the new message will get the first index		
        end		
	end
end
addEventHandler ("onClientRender", root, drawChat)

 

Link to comment
5 hours ago, IIYAMA said:

table.insert(chatbox, 1, newItem)

This is how you can insert items in to the first index and move the rest of the items up.

(Couldn't merge anymore with previous post)

local offset = 14

function drawChat()
    for i, v in ipairs (chatbox) do
	    if (i < offset) then
	        exports.login:dxDrawBorderedText(v..", "..i, 36, (210 - ((offset-i)*15)), 411, 210, tocolor(255, 255, 255, 255), 1.00, "default-bold", "left", "top", false, false, false, false, false)
        elseif (i > 13) then
			table.insert(chatbox, 1, chatbox[i])
        end		
	end
end
addEventHandler ("onClientRender", root, drawChat)

Tried this but the game starts freezing. ... Maybe because auf the eventHandler.... I can´t find a solution.

Link to comment
  • Moderators

You are filling your table with infinity items.

elseif (i > 13) then
	local value = chatbox[i] -- get the value
	chatbox[i] = nil -- delete the item
	table.insert(chatbox, 1, value) -- re-insert
end	

 

Link to comment
16 minutes ago, IIYAMA said:

You are filling your table with infinity items.


elseif (i > 13) then
	local value = chatbox[i] -- get the value
	chatbox[i] = nil -- delete the item
	table.insert(chatbox, 1, value) -- re-insert
end	

 

It is also not working correctly. I show you a picture maybe you can help me.

wCvhHze.png

In the output you can see the text and then the index number. So I want the text with the index 1 to disappear (it should be in the table but not visible). Now I want the new message to output where you can see the text with the 13th index number. The other outputs should move up. BUT the old output should save, so I have it easier to do a scroll function later.

Edited by opnaiC
Link to comment
2 minutes ago, IIYAMA said:

elseif (i > 13) then
	table.remove(chatbox, 1)-- delete the first item, and move all higher items one down.
end	

I thought you wanted to insert items at the beginning.

 

I know I did it now, when you posted this message :D. Thank you. Its working. So, I have to save the message that got deleted in another table if I want to do a scroll function and return them to the table, right ?

 

Like that:

elseif (i > 13) then
		    table.insert (chatboxBuffer, v[1])
		    table.remove (chatbox, 1)
end		

 

Edited by opnaiC
Link to comment
  • Moderators

yea you can do that.

 

Or just using 1 table. And start looping at a different index.

for i=math.max(#chatbox - 13, 1), #chatbox do
  local v = chatbox[i]
end

(not sure if you have to use: - 12, - 13 or - 14 to accomplish this)

Link to comment

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...