Jump to content

Tables Question 2.5 in Manual


Snoozy

Recommended Posts

Okay so I'm rather confused on this, I've now finished my loading system, but after that I just discovered that the stuff that I load into:

VehicleInfo = {}; 
AND 
VehicleInfo[idx] = {}; 

Now I'm confused cause I just discovered that this "VehicleInfo" is limited to the function is this cause I placed them inside the function?

I'm wondering how to make it like "global" not kinda "local"(Limited to function)

Link to comment

Already done that but the problem is that the table VehicleInfo[idx] is required to be inside the loading, else it wouldn't work just tried, the other part is outside of the function but the VehicleInfo[idx] = {};

is inside of the

for idx=i,maxve do 

right after that the

VehicleInfo[idx] = {}; 

is located.

Therefore it seems to not work in a function I just added to test which is supposed to output some information when a player enters a vehicle.

Link to comment
  • Discord Moderators

If you define VehicleInfo outside any function scope, and afterwards insert entries they won't be "hidden" or only available in the for loop.

I might misunderstand you though, please post some code if possible

Link to comment

Posting only some of the code but this should be enough to get the idea.

At start of the lua file:

VehicleInfo = {}; 

Now after that the function starts for loading vehicles

Then later up after some queries and some if statements to check if query was successfully this comes:

for idx=1, maxve do 

Then inside the code above there

VehicleInfo[idx] = {}; 

Is located.

Means its running through that like 5 times as that's what I got in the database right now creating

VehicleInfo[1,2,3,4,5] = {};

However it's located in the for idx=1,maxve do

so it seems to be limited to the function

Now to test it I made:

function Test(theVehicle, seat, jacked) 
    idx = theVehicle 
    VehicleInfo[idx] = {}; 
    outputChatBox("Vehicle: "..theVehicle.."Information:"..VehicleInfo[idx].VehID,source) 
end 
addEventHandler("onPlayerVehicleEnter", getRootElement(), Test) 

This is a new function so it seems that the VehicleInfo doesn't work here.

I do get an error but I don't really think it's that important here.

Link to comment
    for idx=1, maxve do 

You can not use this to identify all vehicles! The type of idx is a number, vehicles are stored as an element! And you're probably setting elementdata on the number too (which isn't possible since idx is no element).

What exactly are you trying to do?

Link to comment

What I'm trying to do is to at server start, I have a vehicle system made that I'm attempting to load every vehicle into it's very own string/variable/whatever (Vehicles have NOT been spawned).

So I'm able to store every vehicle that I'm loading over a MySQL database into a variable each like for example

vehicleInfo[VEHICLEID][owner]

In the kinda same way as that, but I need the variable to be accessible outside of that function which the VehicleInfo[idx] = {};

Wouldn't do.

Link to comment

I see, try it this way:

VehicleInfo = {} 
... 
    for idx=1, maxve do 
        ... 
        local vehicleElement = createVehicle(id,x,y,z) 
        setElementData( vehicleElement, "vehicle_id", idx)     -- attach your ID 
        VehicleInfo[idx] = {} 
        VehicleInfo[idx].VehID = getElementModel(vehicleElement) 
        ... 
    end 

and

function Test(theVehicle, seat, jacked) 
    local idx = getElementData(theVehicle,"vehicle_id")  -- get the attached ID 
    if type(idx) == "number" then     -- check if there actually was an ID 
        local data = VehicleInfo[idx] 
        outputChatBox("Vehicle: "..idx.."Information:"..data.VehID,source) 
    end 
end 
addEventHandler("onPlayerVehicleEnter", getRootElement(), Test) 

setElementData allows to attach any variable to your vehicle, so you could use your own ID's

Link to comment

Again I'm not trying to spawn vehicles at start of the server load, therefore I don't think that would work.

All I'm trying to do is to load basically a mysql query (multiple) and save every single one of them into each own variable so I can use the information ingame later on without having to run a query at that time.

Link to comment

The VehicleInfo with your query's IS available from everywhere, you can try a simple for outputChatBox loop to test it.

Your problem is to get for the correct vehicle the matching info from the table. But vehicles don't get ID's when created, so you'll have to link the database info with the vehicle when you spawn it.

I hope I explained it good enough

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...