scripting - calling method relation() on NPC crashes

Arszi
Scavenger
Posts: 36
Joined: 04 Jan 2018, 06:45

scripting - calling method relation() on NPC crashes

Postby Arszi » 04 Apr 2018, 20:07

I just can not figure this one out:

Code: Select all

local obj = alife():object(target) --target is an id
local relation = obj:relation(db.actor)


This code crashes with the following error:

FATAL ERROR

[error]Expression : <no expression>
[error]Function : CScriptEngine::lua_pcall_failed
[error]File : ..\xrServerEntities\script_engine.cpp
[error]Line : 204
[error]Description : fatal error
[error]Arguments : LUA error: ...ll of pripyat_coc15\gamedata\scripts\task_objects.script:497: attempt to call method 'relation' (a nil value)


stack trace:

0023:0056269C xrCore.dll, xrDebug::fatal()
0023:6E8D266D lua51.dll


Strangely, methods, like object:alive() are working, as expected, BUT method object:id() fails. Instead, if I use object.id, it does not crash.

What could be the problem?
I saw calling npc:relation(db.actor) a lot.
The object I received with alife():object() is not the correct "class"? How could I receive the correct "object", to be able to call relation() ?

User avatar
DoctorX
Trespasser
Posts: 57
Joined: 10 Mar 2016, 00:36

Re: scripting - calling method relation() on NPC crashes

Postby DoctorX » 04 Apr 2018, 20:59

It's because alife():object returns a server object. :relation is for a game object.

You can use level.object_by_id(target) to get the game object for the npc.

Arszi
Scavenger
Posts: 36
Joined: 04 Jan 2018, 06:45

Re: scripting - calling method relation() on NPC crashes

Postby Arszi » 05 Apr 2018, 17:51

Thank you! I get it now.

I tried it, it was working in some cases, but then I encountered a scenario, when a bandit was on the same level as me, and "level.object_by_id(target)" returned null. It was alive, for sure.

What could cause this method to return null in this case?

User avatar
DoctorX
Trespasser
Posts: 57
Joined: 10 Mar 2016, 00:36

Re: scripting - calling method relation() on NPC crashes

Postby DoctorX » 05 Apr 2018, 19:59

Probably moved far enough away to switch offline.

I find it's best to use server objects and methods as much as possible, since they are always valid regardless if the object is online or offline. Restrict use of game objects and methods to situations where there is no alternative, and you can guarantee the object is online, for instance during a dialog between the actor and the npc.

There's a short article that links to a lengthy discussion with some really good information on the differences between online and offline objects here:

http://sdk.stalker-game.com/en/index.php?title=Online_and_offline_alife

Arszi
Scavenger
Posts: 36
Joined: 04 Jan 2018, 06:45

Re: scripting - calling method relation() on NPC crashes

Postby Arszi » 06 Apr 2018, 16:59

Thank you very much! This article helped me a lot.


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests