I tried in various ways to spawn a entity, basically I tried spawning a ammo pack and a health pack.
At later stage I wanted to use a custom model for that, great would be to able to spawn other kind of entities aswell. However all I got was a crashing server or crashing client. It just never worked out.
I suppose I am doing something very basic wrong, but unfortunately all lua scripts I could find do not spawn any items. If any they change with what a player spawns, or they directly influence the player.
What I am looking for is some way to spawn stuff, just like I can do with mapscripts, create{}…
[LUA] spawn a entity?
in it’s current state, it doesn’t appear that the lua bindings really support something like this. a lot of the entity fields are set to read only, no method to set a think function (that i could find) or ent->nextthink, etc. most likely you could get a few simple items spawned, but it’s doubtful that they would provide much functionality.
thats not possible iirc. All lua’s we used for competetive configsets always had the problem that we could only reuse or move existing entities for our use, but not spawn them independently
Yeah when I started lua I was pumped up and ready to start dropping tanks from the sky. The best you can do in terms of spawning is Temp entities. This will allow you to create stuff like explosions and sounds - but not spawn anything.
Soak
So it seems, I have to mimic ET code…
I read the source, and found code for the cheat “give”. I will try to mimic that with LUA code… maybe that does the trick.
I also found somewhere else code, don’t know anymore where, which did use g_spawn() and then it did unlink the entity before setting anything. So it seems to me I have not done something basic wrong… it just seems to be not trivial to spawn a entity. Else I am thinking of spawning some at mapinit, and duplicate/modify them later on, but I don’t like that approach (also no clue if I can duplicate it/modify without crashing)
Thanks Soak, there is even some code from Phenomenon, wouldnt had thought that playing a sound is an entity too.
local tempentity = et.G_TempEntity(et.gentity_get(clientnum, “r.currentOrigin”), EV_GLOBAL_CLIENT_SOUND)
et.gentity_set(tempentity, “s.teamNum”, clientnum)
et.gentity_set(tempentity, “s.eventParm”, et.G_SoundIndex(soundfile))
Lastly I found this explanation, which I think will help me understand whats going on (in that directory is more stuff):
http://icculus.org/~phaethon/q3mc/outline1.html
I think I will start experimenting on etpro instead of etpub, since it appears no one has ever used these lua cmds, it might be problematic in etpub - since there is no test case available.
Well this is what I got till now… It crashes at setting the classname, I tried weapon_sten and item_health. If that is left out, it does not crash instantly.
Trying to mimic the orginal code, I used this:
http://ioqsrc.vampireducks.com/df/d1c/g__cmds_8c-source.html#l00230
I suppose there is something wrong with the classname I used. Maybe another day I have an idea.
module_name = "spawnitem"
module_version = "0.2"
Author = "ailmanki"
Description = "Spawnitem experiments"
--
--
--
-- DEFAULT VALUES
function et_InitGame( levelTime, randomSeed, restart )
et.G_Print(string.format("%s module loaded
",module_name))
et.RegisterModname(string.format("%s v%s", module_name, module_version))
end
function et_ClientSpawn( clientNum, revived )
if ( revived == 0 ) then
end
end
function et_ClientCommand( clientNum, command )
if ( command == "spawn" ) then
local ent = et.G_Spawn()
printmsg("G_Spawn")
et.trap_UnlinkEntity(ent)
printmsg("UnlinkEntity")
local clientOrigin = et.gentity_get(clientNum,"r.currentOrigin")
printmsg("getOrigin ")
et.gentity_set(ent,"s.origin", clientOrigin)
printmsg("s.origin")
et.gentity_set(ent,"r.mins",{-20,-20,-20})
printmsg("r.mins")
et.gentity_set(ent,"r.maxs",{20,20,20})
printmsg("r.maxs")
et.gentity_set(ent,"s.eType", 2) --ET_ITEM = 2
-- et.gentity_set(ent,"s.modelindex",) --store item number in modelindex
et.gentity_set(ent,"s.modelindex2",0) --zero indicates this isn't a dropped item
et.gentity_set(ent,"r.contents", 0x40000000) -- CONTENTS_TRIGGER
et.gentity_set(ent,"r.currentOrigin", clientOrigin)
printmsg("r.currentOrigin")
et.gentity_set(ent,"classname", "weapon_sten")
printmsg("classname")
et.trap_LinkEntity(ent)
printmsg("linkEntity")
return 1
elseif (command == "test") then
local clientOrigin = et.gentity_get(clientNum,"r.currentOrigin")
printmsg(clientOrigin[1])
return 1
end
return 0
end
function et_RunFrame( levelTime )
et_leveltime = levelTime
end
-- print message to either client's console or (if clientID == nil) to server console
-- http://wolfwiki.anime.net/index.php/User:Major_Zeman#Print_message_to_console
function printmsg(message, clientID)
if not message then
return
end
-- replace "s in message with 's
local dummy
message, dummy = string.gsub(message, "\"", "'")
if clientID then
et.trap_SendServerCommand(clientID, "print \"".. message .."^7
\"")
else
et.G_Print(message .."^7
")
end
end
Try to set another stringtype gentity variable, I think the string part of et_gentity_set is broken. If that does work properly, your gentity struct might be different from the original mod.
Also, can’t you debug the crash ?
Debug the crash, no I have not tried todo that. I don’t know how.
et.gentity_set(ent,"message", "weapon_sten")
printmsg("message")
I tried the fieldname message instead of classname, and it did work, though it crashes shortly after - but it did print the last msg.
The CONTENTS_TRIGGER and ET_ITEM are not from ET, so those could be totally wrong.
try reading my first response again … things like classname are READ ONLY. you cannot set them with lua.
your best bet is to use et.G_SpawnGEntityFromSpawnVars, but that can only be used in Init and ETPro is the only mod that has it.
Re-reading your first post + what I know now about entities, it is doubtful that I could get something useful out of this.
Classname isn’t flagged to readonly, it’s flag is set to 0, which in my eyes, is nothing.
However G_SpawnGEntityFromSpawnVars sounds like it might work.
in NQ it certainly is / was marked read only. at any rate, both NQ and ETPub have fields marked read only that make et.G_Spawn() extremely limited; most likely for safety reasons.