LUA variable issues

Discussions about ET modding (sdk code, player/weapon modeling)

Moderators: Forum moderators, developers

Post Reply
Feengur
Posts: 6
Joined: Tue Jul 25, 2006 3:52 pm
Location: Starkvegas
Contact:

LUA variable issues

Post by Feengur »

Hi. I was on a local tj server with a /save /load mod on it and I decided that I would write one of my own with better features to kill the abuse. I ran into a few problems however....

One of my functions is !removesaves (clientNum) , which obviously removes a certain players saved slots.

Code: Select all

function removesaves(playerID,clientID)

		apos[clientID] = nil  --allied position NULL
		xpos[clientID] = nil  --axis position NULL
		et.trap_SendServerCommand(playerID, "cpm MSG HEREr\"" ) -- calling player
		et.trap_SendServerCommand(clientID, "cpm MSG HERE\"" )
		
end

for some reason however this does NOT null the value at that table location. I have tried putting many different values in that table position. I have even modified the "save" command to store the position of a non-existant entity in that table location... but the bitch just won't change. The save/load functions work perfectly, just not this. Am i missing something here?

Here is the code for the 'save' function so you can see that the table is the same:

Code: Select all

function save(playerID,teamID)

	if teamID == 1 then

		apos[playerID] = et.gentity_get(playerID, "origin")
		et.trap_SendServerCommand(playerID, "cpm ^7Position - ^1saved\"" )

	  elseif teamID == 2 then

        xpos[playerID] = et.gentity_get(playerID, "origin")
		et.trap_SendServerCommand(playerID, "cpm ^7Position - ^1saved\"" )
		
	end
	
  return 1
  
end
wth? any ideas? Thanks.

-Feengur
Feengur pwn ftw!
User avatar
ReyalP
Posts: 1663
Joined: Fri Jul 25, 2003 11:44 am

Post by ReyalP »

I suggest you put in some prints to see what is actually going on. Without seeing the whole code, it's impossible for us to say.
send lawyers, guns and money
Feengur
Posts: 6
Joined: Tue Jul 25, 2006 3:52 pm
Location: Starkvegas
Contact:

Post by Feengur »

Well, i have put prints behind each function, so as they execute in the server i can follow the routes that the calls are being executed, but it seems as though lua is just not writing to the same table, or.... it's just not writing anything at all.

Here is the code in it's entirety (i know the structure is probably shiet : ) ):

Code: Select all

--[[ ZeN mod - by Feengur ]]--

zen_mod=0.5

pos= {}
apos={}  
xpos={}  
sd = {}    
wd = {}    
gd = {}    


--[[ MISC CLIENT FUNCTIONS ]]--


function et_ClientConnect( clientNum, firstTime, isBot)

		pos[clientNum] = nil
		apos[clientNum] = nil
		xpos[clientNum] = nil
		sd[clientNum] = false
		wd[clientNum] = false
		gd[clientNum] = false

end


function et_ClientDisonnect(clientNum)


		pos[clientNum] = nil
		apos[clientNum] = nil
		xpos[clientNum] = nil
		sd[clientNum] = false
		wd[clientNum] = false
		gd[clientNum] = false

end

function getTeam(playerID)

	 return et.gentity_get(playerID, "sess.sessionTeam")

end


--[[ END ]]--


--[[ SERVER COMMAND INTERCEPTION ]]--

function et_ClientCommand(clientNum,command)

	if command=="save" then

		scheck(clientNum)
		return 1
		
	end
	
	if command=="load" then
	
		lcheck(clientNum)
		return 1
		
	end
	
	if command=="donotfollow" then
	
		donotfollow(clientNum)
		return 1

	end
	
	if command=="notwanted" then
	
		notwanted(clientNum)
		return 1
		
	end


	return 0
	
end

function et_ConsoleCommand()

	if et.trap_Argv(0)=="goto" then
	
	    gcheck(et.trap_Argv(1), et.trap_Argv(2))
		return 1
		
	end
	
	if et.trap_Argv(0)=="iwant" then

	    wcheck(et.trap_Argv(1), et.trap_Argv(2))
		return 1

	end

    if et.trap_Argv(0)=="sdisable" then

		sdisable(et.trap_Argv(1), et.trap_Argv(2))
		return 1

	end
	
    if et.trap_Argv(0)=="senable" then

		senable(et.trap_Argv(1), et.trap_Argv(2))
		return 1

	end

    if et.trap_Argv(0)=="rmsaves" then

		rmsaves(et.trap_Argv(1), et.trap_Argv(2))
		return 1

	end

	return 0
	
end

--[[ END ]]--


--[[ COMMAND AVAILABILITY CHECKS ]]--

function speccheck(playerID)

	if getTeam(playerID) == 3 then

		et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7Not while spec!\"" )
		return false

	  elseif getTeam(playerID) ~= 3 then

	    return true

	end

end

function scheck(playerID)

	if sd[playerID] then

		et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7You are not allowed to save at this time\"" )
		return

	  else

		save(playerID,getTeam(playerID))
		return 1

	end
	
end

function lcheck(playerID)

	if sd[playerID] then

		et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7You are not allowed to load at this time\"" )
		return

	  else

		load(playerID,getTeam(playerID))
		return 1

	end

end

function gcheck(playerID, clientID)

	if gd[clientID] then
	
       	et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7The player does not wish to be followed\"" )
		return

	  else
	  
		goto(playerID, clientID)
	    return 1

	end
    
end

function wcheck(playerID, clientID)

	if wd[clientID] then

       	et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7The player does not wish to be followed\"" )
		return
		
	  else

		iwant(playerID, clientID)
		return 1
		
    end

end



--[[ END ]]--




--[[ COMMANDS BEGIN ]]--


function save(playerID,teamID)

	if teamID == 1 then

		apos[playerID] = et.gentity_get(playerID, "origin")
		et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7Position - ^1saved\"" )

	  elseif teamID == 2 then

        xpos[playerID] = et.gentity_get(playerID, "origin")
		et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7Position - ^1saved\"" )
		
	end
	
  return 1
  
end

function load(playerID,teamID)

	if teamID == 1 then
	
	    et.gentity_set(playerID, "origin", apos[playerID])
	    et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7Position - ^1loaded\"" )
	    
	  elseif teamID == 2 then
	  
	    et.gentity_set(playerID, "origin", xpos[playerID])
	    et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7Position - ^1loaded\"" )
	    
	end
	
  return 1
  
end

function iwant(playerID,clientID)

		pos[clientID] = et.gentity_get(clientID, "origin")
		et.gentity_set(clientID, "origin", et.gentity_get(playerID, "origin"))
		et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7Your previous postion has been saved. Use ^1!^7goback to return\"" )
	
end

function goto(playerID,clientID)


		  et.gentity_set(playerID, "origin", et.gentity_get(clientID, "origin"))

		
end

function gotohell(playerID,clientID)

		et.gentity_set(clientID, "health", -200)
		et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7LoL~1!\"" )
		et.trap_SendServerCommand(clientID, "cpm \"^7|^1Z^7e^1N^7| ^7Die~1!!!!\"" )
		
end

function goback(playerID)

		et.gentity_set(playerID, "origin", pos[clientID])
		pos[clientID] = nil
		et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7Previous position restored\"" )

end

function rmsaves(playerID,clientID)

		apos[clientNum] = nil
		xpos[clientNum] = nil
		et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7Saved positions have been removed from the player\"" )
		et.trap_SendServerCommand(clientID, "cpm \"^7|^1Z^7e^1N^7| ^7Your saved positions have been removed\"" )
		
end

function sdisable(playerID,clientID)

		sd[clientID] = true
		et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7Player commands revoked\"" )
		et.trap_SendServerCommand(clientID, "cpm \"^7|^1Z^7e^1N^7| ^7Your ability to ^1save^7/^1load^7 has been revoked\"" )
		
end

function senable(playerID,clientID)

		sd[clientID] = false
		et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7Player commands restored\"" )
		et.trap_SendServerCommand(clientID, "cpm \"^7|^1Z^7e^1N^7| ^7Your ability to ^1save^7/^1load^7 has been restored\"" )

end

function donotfollow(playerID)

		if gd[playerID] == false then

			gd[playerID] = true
            et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7!goto's : ^1disabled\"" )
		    
		  elseif gd[playerID] == true then
		  
		    gd[playerID] = false
            et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7!goto's : ^1enabled\"" )

		end
		
end

function notwanted(playerID)

		if wd[playerID] == false then

			wd[playerID] = true
            et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7!iwant's : ^1disabled\"" )

		  elseif wd[playerID] == true then

		    wd[playerID] = false
            et.trap_SendServerCommand(playerID, "cpm \"^7|^1Z^7e^1N^7| ^7!iwant's : ^1enabled\"" )

		end

end

--[[ END ]]--
The command rmsaves is what i'm concerned about above anything else, but i think it has more to do with the fact that my variables aren't acting properly. My checks before the functions are working properly as far as the prints go, so I know that the code is following through as it should... but like I said, my variables aren't returning the values that they should.

I'm thinking now that since I am unfarmiliar with vars in lua that my declaration is incorrect? Maybe the global variables declared up top should be declared as global, or is this automatic? And since they are all tables, do I need to declare indexes for the tables as well? i.e

Code: Select all

 global pos[1 to 100] = {}
I've read the lua reference, but it seems to be very vague on this subject. Thanks for the help!

-Feengur
Feengur pwn ftw!
Feengur
Posts: 6
Joined: Tue Jul 25, 2006 3:52 pm
Location: Starkvegas
Contact:

Post by Feengur »

oh yeah, i have noticed a few errors since the post and I have fixed the 'goback' function. :P , referencing the wrong id's :P . I've added print(apos[clientID]) to the rmsaves function, but it's not printing or returning any values?
Feengur pwn ftw!
User avatar
ReyalP
Posts: 1663
Joined: Fri Jul 25, 2003 11:44 am

Post by ReyalP »

All lua variables are global unless explicitly declared local (except for things like function parameters).

It looks to me like load doesn't check if apos[playerID] is nil. Calling et.gentity_set with a with a nil value for origin probably results in an error.

Also note that

Code: Select all

function rmsaves(playerID,clientID)

      apos[clientNum] = nil
      xpos[clientNum] = nil 
isn't using playerid.
send lawyers, guns and money
Feengur
Posts: 6
Joined: Tue Jul 25, 2006 3:52 pm
Location: Starkvegas
Contact:

Post by Feengur »

omg....... how in the hell did i miss that? I couldn't have missed that after all this time!?!? I'll try and let you know of the results. Thanks! :D
Feengur pwn ftw!
Feengur
Posts: 6
Joined: Tue Jul 25, 2006 3:52 pm
Location: Starkvegas
Contact:

Post by Feengur »

well, i've tried it and it's still not nulling out the value at that table position....
Feengur pwn ftw!
User avatar
ReyalP
Posts: 1663
Joined: Fri Jul 25, 2003 11:44 am

Post by ReyalP »

Feengur wrote:well, i've tried it and it's still not nulling out the value at that table position....
You need to find out what is actually happening. As I suggested before, you can use prints to do this. You should also be checking the server console for lua related errors, as your previous code was almost certainly generating some.

The problem is logic errors in your code, not the table assignment statement being mysteriously broken.
send lawyers, guns and money
Feengur
Posts: 6
Joined: Tue Jul 25, 2006 3:52 pm
Location: Starkvegas
Contact:

Post by Feengur »

I've fixed the problem.....

I'm using this mod with etadmin, and arguments from et_ConsoleCommand() are returned as strings, NOT integers. So a simple use of 'tonumber(clientID)' fixed all the problems. Now i can clean up the code. Thanks for your help.

-Feengur
Feengur pwn ftw!
Post Reply