Hard Light Productions Forums

Modding, Mission Design, and Coding => The Scripting Workshop => Topic started by: Askahain on July 05, 2015, 08:42:25 am

Title: Question: How can i check if an object exists
Post by: Askahain on July 05, 2015, 08:42:25 am
Hello,

can somebody give me a hint how i can check if an object exists ?

Example:

i have a script in the "explosion-sct.tbm"  that uses the value from "mn.Ships[1].Class.Name" but if the ship is destroyed the script generates errors.

The error messages:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


Could not find index 'Class' in type 'object'

------------------------------------------------------------------
ADE Debug:
------------------------------------------------------------------
Name:      (null)
Name of:   (null)
Function type:   (null)
Defined on:   0
Upvalues:   0

Source:      (null)
Short source:   
Current line:   0
- Function line:   0
------------------------------------------------------------------


------------------------------------------------------------------

stack traceback:
   [C]: ?
   [string "explosion-sct.tbm - On Frame"]:21: in main chunk
------------------------------------------------------------------

1: Userdata [object]
2: String [Class]
------------------------------------------------------------------

LUA ERROR: [string "explosion-sct.tbm - On Frame"]:21: attempt to index field 'Class' (a nil value)

------------------------------------------------------------------
ADE Debug:
------------------------------------------------------------------
Name:      (null)
Name of:   (null)
Function type:   (null)
Defined on:   0
Upvalues:   0

Source:      (null)
Short source:   
Current line:   0
- Function line:   0
------------------------------------------------------------------


------------------------------------------------------------------

stack traceback:
   [C]: ?
   [string "explosion-sct.tbm - On Frame"]:21: in main chunk
------------------------------------------------------------------

------------------------------------------------------------------


<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

the Line 21 in "On Frame" is

if ship.Class:isValid() then


so how can i check if the object "class" is existing ?

the check "if ship:isValid()" (a few lines before) is true.

With kind regards

Askahain
Title: Re: Question: How can i check if an object exists
Post by: m!m on July 05, 2015, 08:45:54 am
First you have to check if the ship object is actually valid:
Code: [Select]
ship:isValid()
If that returns true you can use the Class field.

EDIT: Wait, you already did that, can you post the whole script?
Title: Re: Question: How can i check if an object exists
Post by: Askahain on July 05, 2015, 11:02:43 am
Hello,

i tryed to reduce the skript to a minimal version with the error.
Code: [Select]
$On State Start:
[
sendinginterval=0.02
]

$On Frame:
[
if timestamp == nil then
timestamp = 0
end

if mn.getMissionTime() > timestamp then
timestamp = mn.getMissionTime() + sendinginterval

plr = hv.Player
ship = mn.getObjectFromSignature(plr:getSignature())

if ((plr:isValid()) and (ship:isValid())) then

shipinfo={}

if ship.Class ~= nil then
table.insert(shipinfo,ship.Class.Name)
end

end

end
]


the base version was from the auxiliary instruments script.

Edit: i try to use this with the diaspora mod

Edit2: the "if ship.Class ~= nil then" line was the next test. before the teste the line contains "if ship.Class:isValid() then"
Title: Re: Question: How can i check if an object exists
Post by: zookeeper on July 05, 2015, 12:02:13 pm
You can't assume hv.Player to be valid, especially if/when your hook runs outside GS_STATE_GAME_PLAY, nor (I think) for it to necessarily to be a ship in some situations.

What I usually do is this:

Code: [Select]
    local player = hv.Player

    if player ~= nil and player:isValid() then

If you get problems when the player dies, or when you restart the mission, or when you quick restart on death, then you might want to add an extra mn.getMissionTime() > 0.1 check to that (you have a timestamp check, but it doesn't seem like it'd work across mission restarts). Quick restart especially tends to cause all sorts of unintuitive weirdness that always breaks my scripts before I add that safeguard.
Title: Re: Question: How can i check if an object exists
Post by: m!m on July 05, 2015, 12:06:58 pm
If you require that hv.Player is a ship then you must check the type of the object. You can use getBreedName() to get the type of the object:
Code: [Select]
if hv.Player ~= nil and hv.Player:isValid() and hv.Player:getBreedName() == "Ship" then
    ...
end
That will make sure that the player object is a ship.
Title: Re: Question: How can i check if an object exists
Post by: Askahain on July 06, 2015, 12:40:45 am
Thank you very much  :D

The missing part was that i don't check if the object is a ship.