• Getting coordinates from an entity in the map
    28 replies, posted
I have two entities in my map named "Blue" and "Red" that I want to use as coordinates for spawning objects, but they will be different on each map, so I need to grab the vector of these points. How can I go about this? (Sorry for not posting code, I don't even have a rough idea where to start.)
Entity:GetPos()
If by "name" you mean class name then: [lua] local blue = ents.FindByClass("Blue")[1] local red = ents.FindByClass("Red")[1] [/lua] Then you can do this: [lua] local redpos = red:GetPos() [/lua]
Ah, thank you man, that seems to do what I need, sorry I didn't find that sooner. [editline]27th November 2013[/editline] Now it is returning that as nil... [CODE] if SERVER then function GM:Initialize() timer.Create( "time", 2, 0, spawnRed) end function spawnRed() local melon = ents.Create("npc_zombie"); local red = ents.FindByClass("Red")[1]; local redpos = red:GetPos(); melon:SetPos(redpos) melon:Spawn() end end[/CODE] I have triple checked that all those values are correctly spelled and referring to the right things... you guys see anything that stands out?
Edited, I see what you are trying to do now sorry =p
[QUOTE=Ultravisitor;42996065]Ah, thank you man, that seems to do what I need, sorry I didn't find that sooner. [editline]27th November 2013[/editline] Now it is returning that as nil... [CODE] if SERVER then function GM:Initialize() timer.Create( "time", 2, 0, spawnRed) end function spawnRed() local melon = ents.Create("npc_zombie"); local red = ents.FindByClass("Red")[1]; local redpos = red:GetPos(); melon:SetPos(redpos) melon:Spawn() end end[/CODE] I have triple checked that all those values are correctly spelled and referring to the right things... you guys see anything that stands out?[/QUOTE] Is Red or Blue the Hammer Targetname instead? Try ents.FindByName instead.
The aim of the script is to spawn a zombie on top of the entity that I have labeled in hammer "Red" using the field labeled "Class" in the entity's properties like so: [IMG]http://i.imgur.com/PDYFJKh.png[/IMG] every two seconds (for testing purposes). Also, ents.FindByName is returning nil as well.
[QUOTE=Ultravisitor;42998715]The aim of the script is to spawn a zombie on top of the entity that I have labeled in hammer "[B]Red[/B]" using the field labeled "Class" in the entity's properties like so: [IMG]http://i.imgur.com/PDYFJKh.png[/IMG] every two seconds (for testing purposes). Also, ents.FindByName is returning nil as well.[/QUOTE] Unless you define the entity in Lua it will never be created in the map anyway. In Garrysmod/Lua/Entities/ make a folder named "red" and put this in it: [lua] ENT.Type = "point" [/lua]
Also, have you created that entity in Lua ( lua/entities/ folder )?
Wouldnt it be better to put it in the gamemode entities folder? Also, if I did so, it would be gamemodes/*name*/entities/red? And am I putting that code in a .lua file named red, in a folder named red?
[QUOTE=Ultravisitor;42998819]Wouldnt it be better to put it in the gamemode entities folder? Also, if I did so, it would be gamemodes/*name*/entities/red? And am I putting that code in a .lua file named red, in a folder named red?[/QUOTE] It doesn't really matter if you put in into lua/entities or gamemodes/*name*/entities/. ( I didn't know you are making a gamemode ) Depending on what type of entity you want to do, but you have next options: gamemodes/*name*/entities/red.lua - Loaded by both client and shared, I suggest this method gamemodes/*name*/entities/red/cl_init.lua -- Loaded by client only gamemodes/*name*/entities/red/init.lua -- Loaded by server only [editline]27th November 2013[/editline] For what to put in those files, see entities in base and sandbox gamemodes.
Ah, shit, I should have mentioned that I was making a gamemode. So, I've tried this using the red.lua method, and the error message is still the same: [QUOTE][ERROR] gamemodes/leagueofmelons/gamemode/shared.lua:9: attempt to index local 'red' (a nil value) 1. unknown - gamemodes/leagueofmelons/gamemode/shared.lua:9[/QUOTE] with line 9 being [CODE]local redpos = red:GetPos()[/CODE] I can't find anything in any gamemode where all they are doing is defining a point based on an entity in the map, so all red.lua contains is [CODE]ENT.Type = "point"[/CODE] Do I need to do anything else or is this entirely for the purpose of defining what type of entity? Thanks for the help by the way. I haven't scripted in a while and this is all really useful information.
local red = ents.FindByClass("red")[1]
Agh, still not working. Same error and all. Looking back in the console to when it loads in the map just after "Making workshop gamemode available for client download ", the map is erroring in red(the color of the text is red, that is): [QUOTE]Attempted to create unknown entity type red! Can't init red Attempted to create unknown entity type blue! Can't init blue Nav File is wrong or something (1) [/QUOTE] Thats the map itself returning that error, right?
That means those entities doesn't exist. So you failed to make an entity with classname red.
What can be done about that? Do you know of a more efficient way to create something to spawn scripted entities?
You should create your custom entity in Lua not in the Hammer editor and then you change the position of your entity depending of the map.
I have a mapper who is working on a map, the one I have is just for testing and the locations, distances and such will change map-to-map so I need the code to be flexible and work with any map no matter where the entities are. Is this possible using only lua?
Yes it is, but you seem to be failing at creating the entity. Just copy and paste one of existing entities and rename it. Then proceed to editing its code.
[QUOTE=Ultravisitor;43000227]I have a mapper who is working on a map, the one I have is just for testing and the locations, distances and such will change map-to-map so I need the code to be flexible and work with any map no matter where the entities are. Is this possible using only lua?[/QUOTE] Just use a info_target in the map, give it a unique name such as redPoint, then: [code]if SERVER then function GM:Initialize() timer.Create( "time", 2, 0, spawnRed) end function spawnRed() local melon = ents.Create("npc_zombie"); local red = ents.FindByName("redPoint")[1]; local redpos = red:GetPos(); melon:SetPos(redpos) melon:Spawn() end end[/code] and to debug, try: [code] PrintTable(ents.FindByName("redPoint")) [/code] to see if it's finding the ent...
[QUOTE] [ERROR] gamemodes/leagueofmelons/gamemode/shared.lua:3: attempt to call global 'printTable' (a nil value) 1. unknown - gamemodes/leagueofmelons/gamemode/shared.lua:3 [/QUOTE] I have a SENT I am trying to load in that it is not recognizing, I think it has to do with that, it simply will not recognize any of my entities.I have them in gamemodes->*name*->entities->entities. One file is named red.lua and then I have the SENT melon I am trying to load in, which is in a folder containing a shared, init, and cl_init. Shared in the gamemode file looks like this: [CODE] function GM:Initialize() timer.Create( "time", 2, 0, spawnRed) end function spawnRed() local melon = ents.Create("melon") red = ents.FindByName("red")[1] redpos = red:getPos() melon:SetPos("redpos") melon:Spawn() PrintTable(ents.FindByName("red")) end[/CODE] Can anyone explain how to declare entities to a gamemode? I am completely at a loss and there are no guides for doing this. The script loads in zombies at fixed vectors fine, so it has to be in the gamemode's ability to access my entities folder.
gamemodes/<gamemode name>/entities/entities/<ent name as a folder (possibly no spaces?)> and in that folder, you need an init.lua, for example: [code]ENT.Base = "base_point" ENT.Type = "point" function ENT:KeyValue( key, value ) key = string.lower(key) if (key == "targetname") then self.Entity:SetName(value) end if (key == "team") then self.Entity.TeamNumber = tonumber(value) end end[/code] That is an ent I used to mark spawn points in one of my game modes :P Are you sure you're loading your game mode? When you load it, does it give you any errors? A note with PrintTable, it's actually upper case P, not lower case, what is the output of it? An obvious solution to your problem: [code]hook.Add("InitPostEntity", "SpawnStuff", function () local melon = ents.Create("melon") melon:SetPos(ents.FindByName("red")[1]:GetPos()) melon:Spawn() end)[/code] This hook is better because it runs once everything has spawned, instead of when the map starts -- when nothing exists yet, I don't think a timer will cut it here. For the record, [b]this should go into a server side game mode file, such as init.lua, NOT shared.lua![/b] If the aim is to spawn one zombie, why not just place the zombie in hammer?
If you need to find really specific vectors in the future for npc positions and such i recommend wiremod. The gps tool makes it really easy for you to find exact vectors.
[CODE]hook.Add("InitPostEntity", "SpawnStuff", function () local melon = ents.Create("npc_zombie") red = ents.FindByName("red") redpos = red:getPos() melon:SetPos("redpos") melon:Spawn() end)[/CODE] Returns [QUOTE][ERROR] gamemodes/leagueofmelons/gamemode/init.lua:9: attempt to call method 'getPos' (a nil value) 1. v - gamemodes/leagueofmelons/gamemode/init.lua:9 2. unknown - lua/includes/modules/hook.lua:82[/QUOTE] I've had it spawning zombies because the SENT I am trying to get it to spawn it doesn't seem to be recognizing, so I'm isolating my problems. I want them to be spawned on an interval, which of course would be why hammer is not applicable. And I'm pretty sure its loading the gamemode, it wouldnt be returning errors this way if it hadnt, right?
Can you not fix simple errors? The guy misspelled GetPos.
Also ents.FindByName() returns a table not an entity.
God, I'm sorry to keep this thread going. I've been staring at this for hours and looking at different codes, I cant for the life of me tell what is wrong with this. It is recognizing the spawn point now, because when I replace "sent_melon" with "npc_zombie", it spawns a zombie at the predefined location. init.lua [code]AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include( "shared.lua" ) hook.Add("InitPostEntity", "RedSpawn", function() local red = ents.FindByName("redminions")[1] local redpos = red:GetPos() local melon = ents.Create("sent_melon") melon:SetPos(redpos) melon:Spawn() PrintTable(ents.FindByName("redminions")) end)[/code] entities/entities/sent_melon/init.lua [code]AddCSLuaFile() DEFINE_BASECLASS("base_anim") ENT.Spawnable = false ENT.AdminSpawnable = true ENT.AutomaticFrameAdvance = true function ENT:Initialize() self:SetModel("models/props_junk/watermelon01.mdl") self:PhysicsInit( SOLID_VPHYSICS ) self:SetMoveType( MOVETYPE_VPHYSICS ) self:SetSolid( SOLID_VPHYSICS ) local phys = self:GetPhysicsObject() end[/code] Which returns in console: Attempted to create unknown entity type sent_melon! [code][ERROR] gamemodes/leagueofmelons/gamemode/init.lua:10: Tried to use a NULL entity! 1. SetPos - [C]:-1 2. v - gamemodes/leagueofmelons/gamemode/init.lua:10 3. unknown - lua/includes/modules/hook.lua:82 [/code] I can't tell if the procedure for making a SENT has changed, if I'm missing something obvious, or if I never knew how to do this in the first place, but the new wiki doesn't seem to have anything on making SENTs and I am completely lost here. Also, if its any use, I have tried putting the .lua for a balloon in the same folder, renamed like my SENT and the same error was returned. Any help would be appreciated, and you guys have been a huge help so far.
idk if gmod has changed or something, but I've never seen: [code]DEFINE_BASECLASS("base_anim")[/code] Back when I used to do this stuff, it was done with: [code]ENT.Type = "anim"[/code] I'm not sure if it's needed, but you may need: [code]function ENT:Draw() self:DrawModel() end[/code] in a client side file (cl_init.lua) [code]ENT.AutomaticFrameAdvance = true[/code] isn't that code for NPCs? To play the animation on the models etc? I don't think there is any animations on a melon... [code]local phys = self:GetPhysicsObject()[/code] Why grab the physics object then do nothing with it? [code]ENT.Spawnable = false ENT.AdminSpawnable = true[/code] Not 100% sure, but shouldn't that go into a shared.lua file? On the other hand, since this is a gamemode that I assume isn't derived from sandbox, do they even do anything? [code]PrintTable(ents.FindByName("redminions"))[/code] The point of this is, you look in the console to see if it found any entities, apart from this, it is useless to have in your code. I'll just leave this here: init.lua: [code] -- Send client side files to the client AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") -- Load in the shared file include("shared.lua") -- Called when the entity is created function ENT:Initialize() -- Set the model of the entity self:SetModel("models/props_junk/watermelon01.mdl") -- Give it collisions / physics (this is your code, i assume it is correct) self:PhysicsInit( SOLID_VPHYSICS ) self:SetMoveType( MOVETYPE_VPHYSICS ) self:SetSolid( SOLID_VPHYSICS ) end [/code] shared.lua: [code] -- What to base this entity off of, I think anim is correct, not 100% sure for physics props though ENT.Type = "anim" ENT.Base = "base_anim" -- Most of this isn't needed ENT.PrintName = "Some print name I guess" ENT.Author = "Ash47" ENT.Contact = "dont" ENT.Purpose = "To show you stuff" ENT.Instructions = "spawn it i guess" ENT.Spawnable = false ENT.AdminSpawnable = true [/code] cl_init.lua [code] -- Include the shared file include("shared.lua") -- Not sure if these are really needed -- Run when the entity is created, I remember reading somewhere that this is needed (though I am probably wrong) function ENT:Initialize() end -- Runs when this entity is drawn function ENT:Draw() -- Draw the model of this entity self:DrawModel() end [/code]
Most of what you mentioned that wasnt needed I literally added from existing SENTs I had found just on the off chance that they would fix it. I tried changing those things, as well as copying what you put verbatim, and the error has persisted. [editline]29th November 2013[/editline] Nevermind, It worked, sorry I had made a mistake. Thank you very much, internet people!
Sorry, you need to Log In to post a reply to this thread.