Jump to content

I need a simple text function


manuelhimmler

Recommended Posts

I have scriptet a text function:

function GameTextForPlayer(player,text,milliseconds,r,g,b,zeile)
if zeile == nil then
	zeile=1
end
 
if zeile == 1 then
	if isTimer(gametimer[1][player]) then
		killTimer(gametimer[1][player])
		textDestroyDisplay(gametext[1][player])
	end
	gametext[1][player] = textCreateDisplay()
	textDisplayAddObserver(gametext[1][player],player)  
	local scrx = getElementData(player,"player.sx")
	local scry = getElementData(player,"player.sy")
	local textItem = textCreateTextItem(tostring(text), 0.5, 0.2875, 0,tonumber(r), tonumber(g), tonumber(b), 255, 3, "center","center",0)
	textDisplayAddText(gametext[1][player],textItem)
	gametimer[1][player] = setTimer(textDestroyDisplay,tonumber(milliseconds),1,gametext[1][player])	
else
	if isTimer(gametimer[zeile][player]) then
		killTimer(gametimer[zeile][player])
		textDestroyDisplay(gametext[zeile][player])
	end
	gametext[zeile][player] = textCreateDisplay()
	textDisplayAddObserver(gametext[zeile][player],player)  
	local scrx = getElementData(player,"player.sx")
	local scry = getElementData(player,"player.sy")
	local textItem = textCreateTextItem(tostring(text), 0.5, 0.2875+((zeile-1)*0.045), 0,tonumber(r), tonumber(g), tonumber(b), 255, 3, "center","center",0)
	textDisplayAddText(gametext[zeile][player],textItem)
	gametimer[zeile][player] = setTimer(textDestroyDisplay,tonumber(milliseconds),1,gametext[zeile][player])	
end
 
end

but there is often a error in this row "gametext[1][player] = textCreateDisplay()" called "table index is nil" and then the text is show the whole time, also when I destroy it.

Link to comment
but there is often a error in this row "gametext[1][player] = textCreateDisplay()" called "table index is nil"

This means that you're doing something like this:

table [ nil ]

In the case of "gametext[1][player]", I'd say only "player" can become nil. Are you sure you're passing the right player through to the function?

Link to comment

While the discussion moved on, I'll point out here that you are getting errors because you are forgetting that tables need to be defined before they are indexed.

myTable[1][1] --will error
myTable[1] --will also error
myTable = {}
myTable[1] -- will now return nil (index exists now, but no value has been entered yet)
myTable[1][1] --will error
myTable[1] = {}
myTable[1] --will return TABLE: (memory address)
myTable[1][1] --will return nil

or you can just do:

myTable = {
{}
}  -- creates a double-array table of size myTable[1][1]

so remember you need to initialize tables. Also remember however that initializing a table will wipe the table blank too, so initialize the table at a spot that only is executed once, or add:

if type(myTable) ~= "table" then
   myTable = {}
end

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