Forums » Community Projects

Lua Plugin Trouble

Jul 10, 2008 Nosrath link
I'm been having a few issues with a plugin I'm developing. Mostly it's for practice, but I seem to be having some trouble. Here's the code for the plugin:

declare("Tester", {})

Tester.active = true

function Tester:OnEvent(event, charid)
if charid == GetCharacterID() then
ForEachPlayer(Tester.ShowPlayers)
end
end

function Tester.ShowPlayers(charid)
local charname = GetPlayerName(charid)
local faction = GetPlayerFaction(charid)
local factionname = FactionName[faction] or "Unknown"
local ship = GetPrimaryShipNameOfPlayer(charid) or "Unknown"
local distance = GetPlayerDistance(charid) or "Unknown"
if Tester.active then
if charname:match("^*") then
-- elseif charname:match("^Nosrath") then
elseif charname:match("Unknown") then
else
Tester.print (charname, ship, factionname, distance)
end
end
end

function Tester.CLI(_, data)
if data and data[1] then
if string.lower(data[1]) == "on" then
Tester.active = true
Tester.print("Tester online.")
elseif string.lower(data[1]) == "off" then
Tester.active = false
Tester.print("Tester offline.")
end
else
print("Usage:\t/tester [on|off] -- Activates or deactivates the tester.")
end
end

function Tester.print(text, text2, text3, text4)
print("\127ffff00" .. text .. "(" .. text2 .. ") - " .. text3 .. " (" .. text4 .. ")")
end

RegisterUserCommand("tester", Tester.CLI)
RegisterEvent(Tester, "PLAYER_ENTERED_SECTOR")

For some reason the "Get" functions I'm calling will only return a value sometimes. It seems relatively sporadic as to whether or not the functions return a value for the charid even through the only character id's I'm searching are id's that ARE in the same system.

Any ideas?
Jul 10, 2008 Scuba Steve 9.0 link
Which Get functions in particular? Some of those don't work outside of radar range. In particular, I know GetPrimaryShipNameOfPlayer(charid) and GetPlayerDistance(charid) don't.
Jul 11, 2008 Nosrath link
I've been having problems with most of the get functions. Even GetPlayerName sometimes.

And there have been times that a character I KNEW was in range didn't show up right with GetPrimaryShipofPlayer and GetPlayerDistance didn't show up. Radar range might be the problem some of the time... but I don't think it's my only one.
Jul 11, 2008 Scuba Steve 9.0 link
Oh, you're using PLAYER_ENTERED_SECTOR. Unfortunately, as I found in AlertMachine, that event is fired as soon as a player receives a nodeid from the sd. Unfortunately, by the time anyone receives that event:
A) The entering player's ship usually hasn't spawned yet, rendering GetPlayerShip() useless
B) The entering player's name hasn't been made properly available to the clients in the sector(Getting (reading transponder <charid>) readings)
C) Since the entering player's ship doesn't exist, GetPlayerDistance() doesn't work, and I'm not sure the faction dealies you're doing will work either.

Backing up a bit, here's probably the root of your problem:
""[
function Tester:OnEvent(event, charid)
     if charid == GetCharacterID() then
          ForEachPlayer(Tester.ShowPlayers)
     end
end

RegisterEvent(Tester, "PLAYER_ENTERED_SECTOR")
]""

While the code is fine, it'll produce unexpected results because at the time it fires off, you own ship has yet to spawn and you haven't quite received all the data from the other players in the sector. Once this code is fired off, depending on your sector load speed, net latency, and a couple other factors, you definitely may not be able to get information from GetPlayerName, GetPrimaryShipNameOfPlayer, or GetPlayerDistance since your ship has yet to finish entering the sector.

I would suggest you wait until you receive the SHIP_SPAWNED event. In that case, though, you'd also receive the event when your ship spawned in a station exit; if you'd like it to be otherwise, maybe code something like a switch that only processes the SHIP_SPAWNED events if they come immediately after the PLAYER_ENTERED_SECTOR event.
Jul 11, 2008 maq link
Or check for name being the 'reading transponder' thing and make it recheck that id until it can get the data
Jul 11, 2008 Scuba Steve 9.0 link
Ah, but that data is filled in before your ship is spawned, so some of the functions still won't work as expected.