Author Topic: Question: How can i check if an object exists  (Read 1516 times)

0 Members and 1 Guest are viewing this topic.

Question: How can i check if an object exists
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

 

Offline m!m

  • 211
Re: Question: How can i check if an object exists
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?

 
Re: Question: How can i check if an object exists
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"
« Last Edit: July 05, 2015, 11:07:06 am by Askahain »

 

Offline zookeeper

  • *knock knock* Who's there? Poe. Poe who?
  • 210
Re: Question: How can i check if an object exists
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.

 

Offline m!m

  • 211
Re: Question: How can i check if an object exists
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.

 
Re: Question: How can i check if an object exists
Thank you very much  :D

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