Jump to content
  • 0

function parameters and tables


Platonic

Question

Hi, i stumbled on a new issue while learning. Or i would say that i am not understanding the mechanic i just did because its not how i readed it from docs.

instructionSet = gg.getValues(values) -- create new table with address, flags, value

-- instructionSet 
function instructions(instructionSet) -- function parameters (instructionSets)
if (instructionSet < 0x40) then
print(type(instructionSet))
print("this is 32 bits")
else
print("this is 64 bits")
end
return instructionSet
end

print("instructionset", instructions(instructionSet[1].value)) -- function call (instructions(...)

function dataTypes(dword, instructions)
if (dword*10 == instructions[1].value) then
result = dword
print("dataType is size four")
else
result = qword
print("dataType is size eight")
end
return result
end

print("dataType", dataTypes(4, instructionSet))

currently in the first function, instructionSet is of type number. and compared to the hex 0x40
but if i change the name of the parameter of the function  "instructions"

function instructions(instructionSet) -- function parameters (instructionSets)

instructionSet is then not recognized anymore as type number but changes to a type table. I am then forced to let it know that i want to compare int values specifying field value, (the fact that it changes from number to table i don't understand)

if (instructionSet[1].value < 0x40) then


In some sources i understand that parameters can basically have any name. So if i use the table name as a function parameter i should only be able to use them in the function it self. Or at least, the functionality of the body in the functions should not be directly effected by some parameter change. Then there is the other part of the script which i have questions about.

In the function dataTypes

function dataTypes(dword, instructions)

i use "instructions" as a parameter which i don't think would be an issue. And as values i use "instructionSet" which is where i would as my other question.

print("dataType", dataTypes(4, instructionSet))

why is instructionSet here of type table?
during comparison with 0x40 its type was a number. Then in the other function it changed back to type table.
then when i want to compare it in function dataTypes i am forced to compare it as a table. which is for my current understanding odd.

Some insights would be great. Thank you.

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

In the first place you have declared instructionSet without 's'

instructionSet = gg.getValues(values) -- create new table with address, flags, value

 

Then when you rename your function parameter by instructionSets with 's'

function instructions(instructionSet) -- function parameters (instructionSets)

 

So within the function you need to use instructionSet with the 's' also

if (instructionSet[1].value < 0x40) then

so it will be

if (instructionSets < 0x40) then

 

why this issue ?  it is because you declared instructionSet above so it will be accessible anywhere

 

for example

local value = {10, 20, 30}

local function foo(value)
  print (value) -- value refer to the function parameter
end

foo(50) --> will print 50

-- # As soon you change the parameter name to something else
local function foo(anything)
  print (value) -- value refer to the table declared above
end

foo(50) --> will print the table value {10, 20, 30}



-- # And even a function without a parameter can access the variable value
local function bar()
  print (value)
end

bar() --> will print the table value {10, 20, 30}

 

So avoid naming your parameter with the same name as your variable

 

Link to comment
Share on other sites

you can use self as parameter for the function
instructions = function (self)
when u call the function you call it this way : 
instructionSet:instructions()
this means that instructionSet will take place as arg and the instructions fn will act on the table it self only and for return value change it to self so it will return itself
and for function dataTypes(dword, instructions) -> function dataTypes(self , dword) so the agrs don't get mixed up bc when u do instructionSet:dataType(dword)
the instructionSet take always the 1st arg for the function and in the body of the function rename instructionSet to self

+ don't forget to assign the function to the table 
instructionSet.instructions = function(self)
this method will insert a function inside a table and call it upon it self (tab)
self must be a table
-----------------------------------------------------------------------------------------------------------

 

instructionSet = gg.getValues(values)

-- instructionSet 
instructionSet.instructions = function(self)
if (self[1].value < 0x40) then
print(type(self))
print("this is 32 bits")
else
print("this is 64 bits")
end
return self
end

print("instructionset", instructionSet:instructions())

instructionSet.dataTypes = function(self,dword)
if (dword*10 == self[1].value) then
result = dword
print("dataType is size four")
else
result = qword
print("dataType is size eight")
end
return result
end

print("dataType", instructionSet:dataTypes(4))
------------
to unsign these function from the table : 
instructionSet.dataTypes = nil
------------
if u assign function to a sub table like : 
instructionSet.subclass.dataTypes = function(self)
the self(subclass) won't have access to the instructionSet but it will have acccess to the table inside it
------------
you can use cammel naming to prevent mixed up in your script
instructionset / Instructionset / InstructionSet / instructionSet /INSTRUCTIONSET--> these are diffrent var names you can use _ at the end or  at the start of the var name aswell this is up to u
------------
you can do also : 
fucntion test(...) <-- the ... dots means that the function take any argument and unlimited amount but u need to asign names in the function for them
arg1,arg2,arg3,arg4..argx = ...
--body
end

 

Link to comment
Share on other sites

instructionSet = gg.getValues(values) -- create new table with address, flags, value
print("value instructionSetTable: ", instructionSetTable) -- nil
print("value instructionSet: ", instructionSet) -- table address, flags, value

-- instructionSet 
function instructions(instructionSetTable) -- function parameters (instructionSets)

if (instructionSetTable < 0x40) then 
print("value instructionSetTable: ", instructionSetTable) -- value 40
dataType = 4
cdOffsetSmall = 0xC
cdOffsetBig =  0xB58
cbOffsetSmall = 0x1C
print("this is 32 bits")
else
dataType = 8
cdOffsetSmall = 0x18
cdOffsetBig = 0x16B0
cbOffsetSmall = 0x38
cbOffsetBig = 0x48
print("this is 64 bits")
end
return instructionSetTable
end

instructions(instructionSet[1].value)
print("value instructionSet[1].value: ", instructionSet[1].value) -- value 40

According to the prints the parameters should return 40. Error says that i am comparing nil.

Script error: luaj.o: /storage/emulated/0/Download/goodgame empire - kopie - kopie.lua:35
`if (instructionSetTable < 0x40) then
attempt to compare nil with number (local 'instructionSetTable')
level = 1, const = 18, proto = 0, upval = 1, vars = 3, code = 22
LT 0 v0 64
Just now, Platonic said:
instructionSet = gg.getValues(values) -- create new table with address, flags, value
print("value instructionSetTable: ", instructionSetTable) -- nil
print("value instructionSet: ", instructionSet) -- table address, flags, value

-- instructionSet 
function instructions(instructionSetTable) -- function parameters (instructionSets)

if (instructionSetTable < 0x40) then 
print("value instructionSetTable: ", instructionSetTable) -- value 40
dataType = 4
cdOffsetSmall = 0xC
cdOffsetBig =  0xB58
cbOffsetSmall = 0x1C
print("this is 32 bits")
else
dataType = 8
cdOffsetSmall = 0x18
cdOffsetBig = 0x16B0
cbOffsetSmall = 0x38
cbOffsetBig = 0x48
print("this is 64 bits")
end
return instructionSetTable
end

instructions(instructionSet[1].value)
print("value instructionSet[1].value: ", instructionSet[1].value) -- value 40

According to the prints the parameters should return 40. Error says that i am comparing nil.

Script error: luaj.o: /storage/emulated/0/Download/goodgame empire - kopie - kopie.lua:35
`if (instructionSetTable < 0x40) then
attempt to compare nil with number (local 'instructionSetTable')
level = 1, const = 18, proto = 0, upval = 1, vars = 3, code = 22
LT 0 v0 64

image.thumb.png.1d56a4073255076599d7abc6daecd38c.png

Link to comment
Share on other sites

On 3/6/2022 at 8:54 AM, MAARS said:
-- # And even a function without a parameter can access the variable value
local function bar()
  print (value)
end

bar() --> will print the table value {10, 20, 30}

 

So avoid naming your parameter with the same name as your variable

 

preferred to use function with parameter. After all its only for learning purposes. been using functions all the time. never used functions with parameters.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.