ProjectsWhat's NewDownloadsCommunitySupportCompany
Forum Index » S.T.A.L.K.E.R.: Shadow of Chernobyl Forum » Mod discussion
Replacing an item via script

Posted by/on
Question/AnswerMake Newest Up Sort by Descending
  07:01:25  2 March 2015
profilee-mailreply Message URLTo the Top
ddraig
Senior Resident
 

 
On forum: 06/19/2012
 

Message edited by:
ddraig
03/02/2015 7:01:47
Messages: 158
Replacing an item via script

Maybe I'm a bit tired but I've been racking my brains with this for the past half-hour and haven't made any real progress:

I'm trying to replace a specified object with a different object, and once the new object is spawned, immediately removing the old object.

This is what I've come up with:


function replace_suit()
	local id
	local obj
	for id = 0,65535 do
		obj = level.object_by_id(id)
		if obj and obj:name() == "ts_sunrise_surprise" then
			alife():create("outfit_stalker_m3",obj:position(),obj:level_vertex_id(),obj:game_vertex_id())
			alife():release(obj, true)
		end
	end
end



And various combinations either result in the new suit being spawned, while the old one remains, or the new suit not spawning at all.

I'm probably missing something incredibly simple, does anyone else have a fresh perspective on this?
  14:40:56  2 March 2015
profilee-mailreply Message URLTo the Top
SacriPan
Senior Resident
 

 
On forum: 09/20/2009
 

Message edited by:
SacriPan
03/02/2015 14:45:31
Messages: 319
Hi.

Maybe the object is offline. I don't think you can get it with "level.object_by_id(id)" if it is.
If it fails, try to get the server instance with "obj = alife(): object(id)"
Also, you use ":name()" to compare to a section. I dont know if it's wrong but I would use ":section()" instead. To avoid a physic collision between the 2 objects, I would also delete first, then create. Also, dont forget to exit the loop once you did what you wanted.
function replace_suit()

local obj
local f_position
local f_level_vertex_id
local f_game_vertex_id


	for id = 0, 65535 do
		obj = level.object_by_id(id)
		
		if obj and obj:section() == "ts_sunrise_surprise" then
			f_position		=	obj:position()
			f_level_vertex_id	=	obj:level_vertex_id()
			f_game_vertex_id	=	obj:game_vertex_id()
			
			alife():release(obj, true)
			alife():create("outfit_stalker_m3", f_position, f_level_vertex_id, f_game_vertex_id)
			exit for
		else
			obj = alife():object(id)
			
			if obj and obj:section() == "ts_sunrise_surprise" then
				f_position		=	obj:position()
				f_level_vertex_id	=	obj:level_vertex_id()
				f_game_vertex_id	=	obj:game_vertex_id()
			
				alife():release(obj, true)
				alife():create("outfit_stalker_m3", f_position, f_level_vertex_id, f_game_vertex_id)
				exit for
			end
		end
	end
	
	
end

On a side note, declaring "id" as local is not necessary here, as the "for loop" limits its scope to the loop already.
  17:12:20  2 March 2015
profilee-mailreply Message URLTo the Top
TKGP
Senior Resident
 

 
On forum: 01/25/2013
 

Message edited by:
TKGP
03/02/2015 17:12:50
Messages: 454
Some things to note.
- "exit for" isn't a thing, you mean break or return
- your second clause would fire even if the object was online, but didn't match the section
- alife():​object() returns a server object reference, not a client object, so the methods like section(), position() etc don't exist
- alife():release() takes a server object, not a level/client object
- you don't need to worry about collision as objects don't come online immediately anyways

Putting it all together...

function replace_suit()
	for id = 0, 65535 do
		local servObj = alife():object( id )
		if servObj and servObj:section_name() == "ts_sunrise_surprise" then
			alife():create( "outfit_stalker_m3", servObj.position, servObj.m_level_vertex_id, servObj.m_game_vertex_id )
			alife():release( servObj, true )
			return
		end
	end
end

  18:36:49  2 March 2015
profilee-mailreply Message URLTo the Top
ddraig
Senior Resident
 

 
On forum: 06/19/2012
Messages: 158
The reason I'm using name() rather than section() is because the suit is spawned in the all.spawn and I can't really edit it without the spawn breaking horribly, so I'm using the name as that's the only real identifier this suit has as a unique object (it doesn't have a story id)

I did get it to work, eventually. Here's what I used:


function replace_suit()
	local id
	local obj
	for id = 0,65535 do
		obj = level.object_by_id(id)
		if obj and obj:name() == "ts_sunrise_surprise" then
			alife():create("outfit_stalker_m3",obj:position(),obj:level_vertex_id(),obj:game_vertex_id())
			alife():release(alife():object(obj:id()), true)
		end
	end
end

  12:25:04  3 March 2015
profilee-mailreply Message URLTo the Top
SacriPan
Senior Resident
 

 
On forum: 09/20/2009
 

Message edited by:
SacriPan
03/03/2015 12:25:21
Messages: 319
@TKGP

---QUOTATION---
- "exit for" isn't a thing, you mean break or return
---END QUOTATION---



Too much 4GL for me I think

For the rest, it's so true... Thanks for stepping in

I should have kept working on my programs instead of trying to escape from work

@ddraig
Glad you got it working in he end!
 
Each word should be at least 3 characters long.
Search:    
Search conditions:    - spaces as AND    - spaces as OR   
 
Forum Index » S.T.A.L.K.E.R.: Shadow of Chernobyl Forum » Mod discussion
 

All short dates are in Month-Day-Year format.


 

Copyright © 1995-2021 GSC Game World. All rights reserved.
This site is best viewed in Internet Explorer 4.xx and up and Javascript enabled. Webmaster.
Opera Software products are not supported.
If any problem concerning the site functioning under Opera Software appears apply
to Opera Software technical support service.