Hi all,
I new in lua coding and got this problem I don't know why, I tested everythink but it still don't work:
I'm making a survival mod and npc are supposed to spawn at predefined location, i a lot of different table but no one work...
The main code:
function Event()
--local tablemax = table.Count(usedtable)
local rand = math.random(1,table.Count(usedtable))
for i=1,math.random(1,4) do
local ent = ents.Create("npc_zombie")
ent:SetPos(usedtable[rand])
ent:Spawn()
end
end
and table shape:
flatgrass = {}
flatgrass[1]=Vector(800,-400,100)
flatgrass[2]=Vector(800,-500,100)
flatgrass[3]=Vector(900,-500,100)
usedtable is defined with this:
function poslist(ply,command,args)
if ply:IsAdmin() == true then
if (args != nil)then
usedtable = args
else
ply:SendHint( "Write something you idiot!",0.1)
end
else
ply:SendHint( "You're not admin!",0.1)
end
end
concommand.Add("/poslist",poslist)
Is there something like a synthax error?
Thank you all...
Can you please post the full error with the line where it's happening, and, put your code samples in [lua] tags.
.
[editline]14th May 2011[/editline]
Sorry for that, im new there...
error is: Timer error: line 131 'setpos' expected to have vector, got string...
[lua]
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "agaga.lua" )
AddCSLuaFile( "values.lua" )
include( 'shared.lua' )
include( 'agaga.lua' )
include( 'values.lua' )
function GM:PlayerSpawn( ply )
self.BaseClass:PlayerSpawn( ply )
ply:SetGravity( 0.75 )
ply:SetMaxHealth( 100, true )
ply:SetWalkSpeed( 200 )
ply:SetRunSpeed( 200 )
ply:SetJumpPower( 200 )
end
function GM:PlayerInitialSpawn( ply )
if ply:IsAdmin() then
ply:SetTeam(2)
else
ply:SetTeam(1)
end
end
function GM:PlayerLoadout( ply )
if ply:Team() == 1 then
ply:Give( "gmod_tool" )
elseif ply:Team() == 2 then
ply:Give( "weapon_physgun" )
ply:Give( "gmod_tool" )
end
end
----------ADDED FUNCTIONS----------
function GM:GetFallDamage( ply, speed )
speed = speed - 440 --580
return speed * (100/(1024-440)) --580
end
function GM:ScaleNPCDamage( npc, hitgroup, dmginfo )
if ( hitgroup == HITGROUP_HEAD ) then
dmginfo:ScaleDamage( 0.5 )
elseif ( hitgroup == HITGROUP_STOMACH ||
hitgroup == HITGROUP_CHEST)then
dmginfo:ScaleDamage( 0.35 )
elseif ( hitgroup == HITGROUP_LEFTARM ||
hitgroup == HITGROUP_RIGHTARM ||
hitgroup == HITGROUP_LEFTLEG ||
hitgroup == HITGROUP_RIGHTLEG ||
hitgroup == HITGROUP_GEAR ) then
dmginfo:ScaleDamage( 0.20 )
end
end
function GM:ScalePlayerDamage( npc, hitgroup, dmginfo )
if ( hitgroup == HITGROUP_HEAD ) then
dmginfo:ScaleDamage( 1 )
elseif ( hitgroup == HITGROUP_STOMACH ||
hitgroup == HITGROUP_CHEST)then
dmginfo:ScaleDamage( 1 )
elseif ( hitgroup == HITGROUP_LEFTARM ||
hitgroup == HITGROUP_RIGHTARM ||
hitgroup == HITGROUP_LEFTLEG ||
hitgroup == HITGROUP_RIGHTLEG ||
hitgroup == HITGROUP_GEAR ) then
dmginfo:ScaleDamage( 1 )
end
end
function DropWeapon(ply)
if (ply:GetActiveWeapon():IsValid()) then
ply:DropWeapon(ply:GetActiveWeapon())
end
end
concommand.Add("/dropweapon", DropWeapon)
hook.Add("DoPlayerDeath", "dropweaponondeath", DropWeapon)
local function DisableNoclip( objPl )
return objPl:IsAdmin()
end
hook.Add("PlayerNoClip", "DisableNoclip", DisableNoclip)
local function blockThings(ply)
return ply:IsAdmin()
end
hook.Add( "PlayerSpawnObject ", "blockThings", blockThings )
hook.Add( "PlayerSpawnSENT", "blockThings", blockThings )
hook.Add( "PlayerSpawnSWEP", "blockThings", blockThings )
hook.Add( "PlayerSpawnNPC", "blockThings", blockThings )
hook.Add( "PlayerSpawnVehicle", "blockThings", blockThings )
function UseTool( pl, tr, toolmode )
if toolmode == "nail" or toolmode == "rope" then
return true
else
return pl:IsAdmin()
end
end
hook.Add( "CanTool", "UseTool", UseTool )
--Hurted function add to heal pack functions
function GM:PlayerHurt(ply, attacker, health)
if (ply:Health() > 15)then
ply:SetWalkSpeed( 200 )
ply:SetRunSpeed( 200 )
ply:SetJumpPower( 200 )
elseif (ply:Health() < 15 and ply:Health() > 0)then
ply:SetWalkSpeed( 30 )
ply:SetRunSpeed( 30 )
ply:SetJumpPower( 0 )
ply:SendHint( "You are hurt, wait for the rescue!",0.1)
end
end
function Event()
--local tablemax = table.Count(usedtable)
local rand = math.random(1,table.Count(usedtable))
for i=1,math.random(1,4) do
local ent = ents.Create("npc_zombie")
ent:SetPos(usedtable[rand])--error there-------------------------------------------------------------------
ent:Spawn()
end
end
function poslist(ply,command,args)
if ply:IsAdmin() == true then
if (args != nil)then
usedtable = args
else
ply:SendHint( "Write something you idiot!",0.1)
end
else
ply:SendHint( "You're not admin!",0.1)
end
end
concommand.Add("/poslist",poslist)
function startsurvival(ply,command,args)
if (ply:IsAdmin() == true) then
if ( usedtable != nil)then
if (timer.IsTimer("randomizer") == false) then
timer.Create( "randomizer", 30, 0, Event)
else
ply:SendHint( "Survival is already started!",0.1)
end
else
ply:SendHint( "Give the name of the pos list /poslist",0.1)
end
else
ply:SendHint( "You're not admin!",0.1)
end
end
concommand.Add("/startsurvival",startsurvival)
function endsurvival(ply,command,args)
if (ply:IsAdmin() == true) then
if (timer.IsTimer("randomizer") == true) then
timer.Destroy( "randomizer")
else
ply:SendHint( "Survival is already ended!",0.1)
end
else
ply:SendHint( "You're not admin!",0.1)
end
end
concommand.Add("/endsurvival",endsurvival)
--------------------------------------------------DATA--------------------------------------------------
flatgrass = {}
flatgrass[1]=Vector(800,-400,100)
flatgrass[2]=Vector(800,-500,100)
flatgrass[3]=Vector(900,-500,100)
flatgrass[4]=Vector(700,-500,100)
flatgrass[5]=Vector(700,-400,100)
flatgrass[6]=Vector(700,-600,100)
flatgrass[7]=Vector(900,-600,100)
flatgrass[8]=Vector(900,-400,100)
flatgrass[9]=Vector(800,-600,100)
[/lua]
[i]usedtable[/i] is created in a console command:
[lua]function poslist(ply,command,args)
if ply:IsAdmin() == true then
if (args != nil)then
usedtable = args[/lua]
That means the table is full of strings and SetPos expects vectors.
oh your right! thanks gonna try to solve the problem!
humm how will I say him to use a table named like that if it exist?
I mean you have a string and you have a number and you combine them to have a key of a table?
or with a string identificate a table?
Edit: I d'ont explained what it's for: there's some table and you tell which one you would use (like flatgrass table) and i would like to select one and use poses...
[lua]flatgrass = {}
flatgrass[1] = Vector(800, -400, 100)
flatgrass[2] = Vector(800, -500, 100)
flatgrass[3] = Vector(900, -500, 100)
local function cc_addpos(ply, cmd, args)
if ply:IsAdmin() ~= true then ply:SendHint("You need to be admin to do this", 0.1) return end
--take each argument and turn it into a number we can use
local x = tonumber(args[1]) or 0
local y = tonumber(args[2]) or 0
local z = tonumber(args[3]) or 0
--flatgrassbeeing a table of positions
table.insert(flatgrass, Vector(x, y, z))
ply:SendHint("Position '"..x..", "..y..", "..z.."' has been added to the list", 0.1)
end
concommand.Add("/addpos", cc_addpos)[/lua]
Like that, kinda.
This is useful thanks, but my problem is the same: i have a string and i would like it to be used to indicate which table we gonna use: there for example 3 table you type the name of one and it load it...
thanks for your help guy...
put the table key as a string. Then compare each tables key to a string that is inputted. The key acts as a name.
Edit:
Also storing individual vectors in a table takes up a lot more memory then if you did:
Table = {}
Table[X] = {100,200,300}
Table[Y] = {900,800,700}
Table[Z] = {500,600,400}
and then did
myVector = Vector(Table[X][i],Table[Y][i],Table[Z][i])
But you don't to worry about that unless you plan on making a huge gamemode. Or do you... :D
[QUOTE=Remscar;29832438]put the table key as a string. Then compare each tables key to a string that is inputted. The key acts as a name.
Edit:
Also storing individual vectors in a table takes up a lot more memory then if you did:
Table = {}
Table[X] = {100,200,300}
Table[Y] = {900,800,700}
Table[Z] = {500,600,400}
and then did
myVector = Vector(Table[X][i],Table[Y][i],Table[Z][i])
But you don't to worry about that unless you plan on making a huge gamemode. Or do you... :D[/QUOTE]
What, no it doesn't, you do realise a vector is just a table with the x , y and z indexes filled with the coordinates, right? also your way requires more computing and memory later on when he needs the table.
[QUOTE=TGiFallen;29833272]What, no it doesn't, you do realise a vector is just a table with the x , y and z indexes filled with the coordinates, right? also your way requires more computing and memory later on when he needs the table.[/QUOTE]
Actually no.
By storing the information the way i suggested it is filling the array of the table and not the hash section which takes up a lot more memory. Later on you are only grabbing 3 numbers to turn into 1 vector, so its a lot quicker then grabbing a vector, since vectors are stored inside hash and not array.
you all sound like to help but I'm a dumb french guy and need example especially for:
put the table key as a string. Then compare each tables key to a string that is inputted. The key acts as a name.
I saw that there was SQL function, is it possible to check with it if a table exist?
Is it possible to get all tables and for all check if table[name]=something and then use it?
Gonna do it, aother way, I did that
[lua]
if (flatgrass[NAME]=="flatgrass")then
usedtable ={}
usedtable = table.Copy(flatgrass)
ply:SendHint( "Poslist loaded successfully, what about starting survival now?",0.1)
end
[lua/]
but it is possible to do usedtable = flatgrass?
I'd suggest reading or re-reading [url=http://www.lua.org/pil/2.5.html]Section 2.5, Tables[/url] in the Lua manual. You can assign a table to another table.
Example
[lua]
local dataTables = {} --Initialize dataTables as a table
dataTables["flatgrass"] = {} -- Initialize table key flatgrass as a table
dataTables["flatgrass"][1] = Vector(1,0,0) -- vector for flatgrass
dataTables["flatgrass"][2] = Vector(0,1,0) -- vector for flatgrass
dataTables["flatgrass"][3] = Vector(0,0,1) -- vector for flatgrass
dataTables["construct"] = {} -- Initialize table key construct as a table
dataTables["construct"][1] = Vector(1,0,2) -- vector for construct
dataTables["construct"][2] = Vector(0,3,0) -- vector for construct
dataTables["construct"][3] = Vector(2,0,1) -- vector for construct
local tableToUse = "NoData" -- Setup the tableToUse, but not as a table we just need to init the variable
-- I set it to "NoData" so other functions can tell if it's not set easier
concommand.Add("set_data_table", function(ply, cmd, args) -- this can be called like set_data_table flatgrass
-- don't forget to check args[1] is not nil before using
if table.HasValue(dataTables, args[1]) then -- if args[1] is a valid key for the dataTables then we
tableToUse = dataTables[args[1]] -- Set the tableToUse to the table contained at dataTable[args[1]]
end
end)
local someOtherFunctionToDoOtherStuff()
if tableToUse == "NoData" then return end -- If the table hasn't been set yet exit this funciton
end
[/lua]
I think is might be what you wanted to know, hope this helps.
What hes saying is yes, that would work.
Thank you all for your help but I've got so much pos to store, I gonna use txt files...
Thanks alot
Sorry, you need to Log In to post a reply to this thread.