Team Spartan Cookies & Milk Forums
globaltables doesn't keep data between sf instances... - Printable Version

+- Team Spartan Cookies & Milk Forums (https://teamscm.co.uk)
+-- Forum: Garry's Mod (https://teamscm.co.uk/forum-4.html)
+--- Forum: Starfall (https://teamscm.co.uk/forum-7.html)
+--- Thread: globaltables doesn't keep data between sf instances... (/thread-1437.html)



globaltables doesn't keep data between sf instances... - Yuri6037 - 04-11-2015

Hey Person,

I don't know if this is intended, but when you have a global table defined by one SF instance and then you try to read this value from another newly spawned SF instance, the vars set by the first SF instance are not kept inside the new SF instance.
That's a bit annoying, because well I plan on using those globaltables in the API I'm making for IntelliHUD. Indeed the api is intended to allow any seat entity to have special data like energy stats using my ZPM system to be renderable with the rest of the hud.
The problem is that if IntelliHUD is spawned after spawning chips using the IntelliHUD api, then data saved in globaltables before IntelliHUD spawn, are not kept inside the globals from IntelliHUD.

Note that those globals are server side only in how IntelliHUD manages things.

Is it intended to work this way, as it seams a bit weird for me ?
Can you fix it ?

Thanks by advance,
Yuri6037


RE: globaltables doesn't keep data between sf instances... - Person8880 - 04-11-2015

Global tables do persist between instances. A lot of my chips would be broken if they didn't. Are you certain you're not redefining values and overwriting what was already there?


RE: globaltables doesn't keep data between sf instances... - Yuri6037 - 05-11-2015

Here is the lib code (which adds variables to global tables) :
Code:
function intellihud.RegisterVehicle(VehicleEnt, data) if (not(IsValid(VehicleEnt))) then error("VehicleEnt must be a valid vehicle entity !") return end if (globaltables.global.IntelliHUD == nil) then globaltables.global.IntelliHUD = {} end globaltables.global.IntelliHUD[VehicleEnt:index()] = data end

And now the code that reads from it :
Code:
local function CheckPlayerSeats(p) if (globaltables.global.IntelliHUD == nil) then return end if (p:inVehicle()) then if (not(plysVeh[p] == p:vehicle())) then plysVeh[p] = p:vehicle() local e = p:vehicle() if (IsValid(e) and not(globaltables.global.IntelliHUD[e:index()] == nil)) then local data = globaltables.global.IntelliHUD[e:index()] if (IsValid(data.ZPM) and data.ZPM:class() == "starfall_processor") then plysData[p].VehData = {EnergyEntity = data.ZPM:getWirelink(), Name = data.Name, NameColor = data.NameColor} end end end if (not(plysData[p].VehData == nil)) then local en = plysData[p].VehData.EnergyEntity if (IsValid(en)) then if (not(plysData[p].VehData.PEnergy == en["P"])) then plysData[p].VehData.PEnergy = en["P"] plysForceSend[p] = true end end end elseif (IsValid(plysVeh[p])) then plysVeh[p] = nil plysData[p].VehData = nil plysForceSend[p] = true end end

The given function runs in a think hook to check if the player seats on a vehicle, and then retrieves if the vehicle exists on the defined IntelliHUD global table. However as I said, if you spawn the first function before the second one, the reader (second function) will not detect that the given vehicle is inside the table defined by first function. But if you inverse and spawn before the reader (second function), then it will detect any first function spawned.

You may also notice that the first function is not complete : indeed you need a special function that generates a table which contains an entity corresponding to ZPM starfall processor, a string name, and a color for the string name color.

To everyone, about this code, please don't copy it for your own projects, otherwise I will need to remove it from here. You can take the base idea if you want, just don't copy the code, and in plus that wouldn't help you understanding better how SF works by copying other's codes...

Thanks by advance,
Yuri6037


RE: globaltables doesn't keep data between sf instances... - TheOneAndOnlyTee - 05-11-2015

Just saying, no one's gonna copy the code since it's of no use to anyone without >basic skill and i'm pretty sure we've all got our own methods/ideas about global tables

plus 'if' it is your code that's making the problem, copying it won't work lol.


RE: globaltables doesn't keep data between sf instances... - Yuri6037 - 05-11-2015

Thanks for your post T-17, however the topic isn't on code sharing, that's just a note for everyone...

Anyways, have you any solutions ?


RE: globaltables doesn't keep data between sf instances... - Person8880 - 05-11-2015

I used your code in the following way:

1. Spawned a chip containing:
local Vehicle = ents.self():isWeldedTo()
globaltables.global.IntelliHUD = {}
globaltables.global.IntelliHUD[ Vehicle:index() ] = { ZPM = ents.self() }

This chip is welded to a chair.

2. Spawned a second chip with:
local plysVeh = {}
local plysData = { [ ents.owner() ] = {} }
local plysForceSend = {}
local function CheckPlayerSeats(p)
if (globaltables.global.IntelliHUD == nil) then
return
end

if (p:inVehicle()) then
if (not(plysVeh[p] == p:vehicle())) then
plysVeh[p] = p:vehicle()
local e = p:vehicle()
if (IsValid(e) and not(globaltables.global.IntelliHUD[e:index()] == nil)) then
local data = globaltables.global.IntelliHUD[e:index()]
print( data, data.ZPM )
if (IsValid(data.ZPM) and data.ZPM:class() == "starfall_processor") then
plysData[p].VehData = {EnergyEntity = data.ZPM:getWirelink(), Name = data.Name, NameColor = data.NameColor}
end
end
end
if (not(plysData[p].VehData == nil)) then
local en = plysData[p].VehData.EnergyEntity
if (IsValid(en)) then
if (not(plysData[p].VehData.PEnergy == en["P"])) then
plysData[p].VehData.PEnergy = en["P"]
plysForceSend[p] = true
end
end
end
elseif (IsValid(plysVeh[p])) then
plysVeh[p] = nil
plysData[p].VehData = nil
plysForceSend[p] = true
end
end

timer.create( "bleh", 1, 0, function() CheckPlayerSeats( ents.owner() ) end )

When I sat down, I got a successful print message (of the table and the first chip). Getting out of the chair and sitting back down also worked. This leaves the only logical conclusion that there must be a problem in how you are setting up your vehicles that is outside of the code you've given here.


RE: globaltables doesn't keep data between sf instances... - TheOneAndOnlyTee - 06-11-2015

Also just remebered, if you really want to get full help from Person you could use PM's instead.


RE: globaltables doesn't keep data between sf instances... - Yuri6037 - 06-11-2015

Ok thanks Person, I'll try to investigate further on the issue, but it's strange that on your side it's working when spawning the vehicle before.


RE: globaltables doesn't keep data between sf instances... - Yuri6037 - 06-11-2015

I found that realy strange, it works and sometimes not... That's weird...