-1

I am a newcomer to coding in general and I want to learn basic Lua scripting for my own hobby. After working on a Lua script, the syntax all seems to be without error but I have come across one issue that I don't understand, I broke it down to this basic function:

{$lua}
 ID1 = "10"
 ID2 = "0"

if (ID1 ~= nil and ID1 == "10") then
  writeInteger(ID2,"25")
  end

print(ID2)

The issue is that the writeInteger does not seem to work at all, ID2 remains at value "0" while it should become "25". Any help would be greatly appreciated.

Marc Balmer
  • 1,780
  • 1
  • 11
  • 18
  • Nobody knows what writeInteger is supposed to do, what arguments it expects etc. – Marc Balmer Nov 27 '17 at 22:12
  • Would you please mind elaborating? I am completely clueless on it – GonthorianDX Nov 27 '17 at 22:21
  • Where are you running your Lua script? Is it for Lua embedded in a game, for one of the microcontrollers that uses Lua, for a standalone Lua script, or something else? We will need some more details to work out where the writeInteger function comes from, etc. – Jack Taylor Nov 28 '17 at 00:22
  • It is from a hex editor called Cheat Engine. You get a list of addresses in an application where you can use writeInteger(address,value) to rewrite a particular value – GonthorianDX Nov 28 '17 at 15:04

2 Answers2

0

This is not valid Lua, or at least it isn't valid vanilla (out-of-the-box) Lua, and since you have not specified anything else, there is not much we can do to help. I will assume writeInteger is a valid function (since your interpreter isn't complaining about a call to a nil value), but I don't think it works as you expect.

If you want to set the ID2 variable to 25, simply write:

ID2 = 25

Lua will convert the string type to an integer type automatically. You can run print(type(ID2)) to confirm this

If you are using cheat engine (as a quick google search suggests) the writeInteger function requires an address and a value.

function writeInteger(Address, Value): Boolean - Returns true on success. 

I am not sure if ID2, a Lua variable, is a valid address, but I am sure that "25" is not an integer. You should remove the quotation marks to start, and since the function returns a boolean you can see if the function was successful by doing:

print(writeInteger(ID2, 25))
AlgoRythm
  • 1,196
  • 10
  • 31
  • It is a Cheat Engine script, yes ID2 is a variable that changes, and when it becomes a certain number it should trigger a writeInteger function meaning it should write a value based on a specified calculation. I was not aware writeInteger was not part of vanilla Lua. I also figured out I can call a value from somewhere based on a description, but sadly I do not know if there is a way to write back to that value in the same manner, only by writing to the actual address. – GonthorianDX Nov 28 '17 at 11:53
0

Lua uses pass by value for primitive types like numbers, booleans and strings. So if you pass a number to a function like writeInteger, it creates a local copy within the function. The function can alter this copy but it will have no effect on caller (of writeInteger in this case). I don't know how writeInteger is supposed to work but if you want to call a function which alters its argument you can create a table and pass that. Tables are still passed by value but the "value" of a table is its memory address (so in effect tables are passed by reference and you can alter the contents of a table by passing it to a function).

See more here Function/variable scope (pass by value or reference?)

John Blackburn
  • 170
  • 1
  • 5