Hello guys i am trying to add Divisions to a game but there is a problem with tables, do you guys know how to solve this?
local divisonName = result3[i].name
print(divisonName)
ESX.Divisions[result3[i].owner].divisonName = {}
it's the code it should get division name and create a table with that like this (we assume divisonName gonna return swat for example):
["police"] = {
["swat"] = {
},
},
but instead of putting division name as SWAT it will put divisionName variable
i already print that divisionName variable and console return me SWAT so everything is fine with logic and value of variable but i guess there it's syntax issue i am not sure!
Console Debug image
Note that in Lua the construct some_table.field is a syntactic sugar for some_table["field"]. Whatever is written after the dot will be treated as a string key.
In your case, to index by the value stored in the variable, you need to write ESX.Divisions[result3[i].owner][divisonName], and not as .divisionName.
Related
I want to pass a lua reference to another function without actually using assignment = but something like loadstring.
local myTable = { test="Hello" }
local myTableStringified = tostring(myTable) -- table: 0xref
print(myTableStringified)
local myTableUnstringified = loadstring(myTableStringified)
print(myTableUnstringified) -- nil but should show table: 0xref
As seen above, this won't do.
You'll have to use one of the modules that provide serialization.
Keep in mind that loadstring returns a function that needs to be called, so to get the table back you need to use loadstring(myTableStringified)().
I'm very new to Lua and I'm trying to globally initialise a table at the very start of my program. At the top, I have:
storage = {}
Then, I want to iterate over elements in this table inside functions in the same file. One example is:
local output
for item in storage do
output = output .. item
end
return output
In this case, I get:
attempt to call a nil value
On the line beginning with for.
I have also tried printing out storage[1]. In this case I get:
attempt to index local 'storage' (a nil value)
Could someone please explain in simple terms what could be wrong here?
You are not showing the entire script, but it's clear that storage value gets reset somewhere between your initialization and using in for item in storage do, because if it keeps the value, you'd get a different error: attempt to call a table value.
You need to use ipairs or pairs function in the loop -- for key, item in pairs(storage) do -- but you first need to fix whatever resets the value of storage.
Is there an easy way to get a default value from an Erlang record definition? Suppose I have something like this:
-record(specialfield, {
raw = <<"default">> :: string()
}).
I would like to have some way to retrieve the default value of the raw field. Something like this would be very simple:
#specialfield.raw % => <<"default">>
This is not possible. I would need to instantiate a record in order to get the default value:
Afield = #specialfield{}
DefaultValue = Afeild#specialfield.raw
DefaultValue % => <<"default">>
Is there an easier way of doing this? I seems like there should be some way to retrieve the default value without having to create an instance of the record.
How about:
raw_default() -> <<"default">>.
-record(specialfield, { raw = raw_default() }).
And now you have a function with the default in it. This will be extremely fast since it is a function call to a constant value. If this is also too slow, enable inlining.
Constructing an empty record and accessing one field can be done on one line:
(#specialfield{})#specialfield.raw.
Take a look at erlang - records, search section "11.8".
There's not much special about records - they're just a tuple at runtime. So to get the field raw from the tuple of default values that is the internal representation of #specialfield{} you would use:
element(#specialfield.raw, #specialfield{}).
In this case, #specialfield.raw is the index of the value for raw in the #specialfield tuple. When you pass in specialfield that resolves to a tuple in the form {specialfield, <<"default">>}.
I've created a lot of string variable names and I would like to use the names as table names ie:
sName1 = "test"
sName2 = "test2"
tsName1 ={} -- would like this to be ttest ={}
tsName2 ={} -- ttest2 = {}
I can't figure out how to get this to work, have gone through various combinations of [] and .'s but on running I always get an indexing error, any help would be appreciated.
In addition to using _G, as Mike suggested, you can simply put all of these tables in another table:
tables = { }
tables[sName1] = { }
While _G works the same way pretty much every table does, polluting global "namespace" isn't much useful except for rare cases, and you'll be much better off with a regular table.
your question is sort of vague, but i'm assuming you want to make tables that are named based off of string variables. one way would be to dynamically create them as global objects like this:
local sName1 = "test"
-- this creates a name for the new table, and creates it as a global object
local tblName = "t".. sName1
_G[tblName] = {}
-- get a reference to the table and put something in it.
local ttest = _G[tblName]
table.insert(ttest, "asdf")
-- this just shows that you can modify the global object using just the reference
print(_G[tblName][1])
Lets say I have i variable var witch is a pointer to another variable named user_id.
How do I puts var such that I can see in the console user_id = (whatever the value is)
Reason why I want to do this, is because I want to write a method called print_debug_block, where you give it an array of variables, and it prints in the following format:
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var1 = valOfVar1
var2 = valOfVar2
var3 = valOfVar3
etc
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger.debug() outputs to the log.
See http://guides.rubyonrails.org/debugging_rails_applications.html#the-logger