0
votes

I'm pretty new to lua and already stared messing around with lua. I have written the following script to print out my distance and angle to every available object in the constructer with the help of an array but there is a big problem in my way:

me = {'["game.exe"+2A]','["game.exe"+A8]','["game"+6A8]'}

botallx = {'["game.exe"+01]','["game.exe"+02]','["game.exe"+03]',
'["game.exe"+04]','["game.exe"+05]','["game.exe"+06]','["game.exe"+07]',
'["game.exe"+08]','["game.exe"+09]','["game.exe"+10]','["game.exe"+11]',
'["game.exe"+12]'}

botally = {'["game.exe"+31]','["game.exe"+32]','["game.exe"+33]',
'["game.exe"+34]','["game.exe"+35]','["game.exe"+36]','["game.exe"+37]',
'["game.exe"+38]','["game.exe"+39]','["game.exe"+30]','["game.exe"+31]',
'["game.exe"+32]'}

botallz = {'["game.exe"+51]','["game.exe"+52]','["game.exe"+53]',
'["game.exe"+54]','["game.exe"+55]','["game.exe"+56]','["game.exe"+57]',
'["game.exe"+58]','["game.exe"+59]','["game.exe"+50]','["game.exe"+51]',
'["game.exe"+52]'}

function sight_angle()
     if (isKeyPressed(VK_E)) then

     for i=1, 12 do
     if (botallx[i] ~= nil or botally[i] ~= nil or botallz[i] ~= nil ) then
     vecX=readFloat(me[1])-readFloat(botallx[i])
     vecY=readFloat(me[2])-readFloat(botally[i])
     vecZ=readFloat(me[3])-readFloat(botallz[i])

     distance=math.sqrt(math.pow(calcZ,2)+math.pow(calcX,2))

     angleX=math.deg(math.atan2(calcZ, calcX))

     print ("current distance is " .. distance)
     print ("current angle is " .. angleX)
     else --One of the botallx, botally, botallz value was nil
     print ("Can't perform calculations due to a nil value")
     end
     end
end

theses addresses called ["game.exe"+...] contain a float value, but some of the objects in the container of botally, botallx and botallz are nil or have nil values. So when I execute the script then the array for i=1 to 12 starts calculating the first one and gives me the angle and distance, but keeps calculating until it hits an object from the container that has a nil value and then doesn't want to carry on calculating the other objects.

Example: object 1,3,6 inside botally, botallx and botallz have values, everything else is nil. it starts calculating the first one...gives out the distance and angle, but then stops at the second and starts complaining that the calculation can't be performed because of a nil value, so it jumps back to the first object and gives me the dist and angle of it again.

But I want it to jump over that second empty nil object and continue with the 3rd object, which has a value and print the distance and angle for me, then jump over the empty 4th and 5th one and calculate the 6th one and after that, it can jump back to the first one again because the rest from 7 to 12 is nil.

but it doesn't want to do this....can somebody help me?

2
Currently, there seems to be a problem with your table constructions. Check your single quotes ('), since there are some unmatched ones. The way you are showing your script here (ignoring errors in construction), there are no nil values in your tables. - pschulz
Then there is also a strong feeling that you should do this different. You said there are float values (so numbers) in your tables, but you decide to use strings to store these values. This seems very strange. - pschulz
oh i see, i fixed the single quotes. these strings inside the tables are actually pointeradresses that point to specific adresses wich contains a float value. - sasa ha
Do you use lua embedded in, say c++? In pure lua, there are no pointers, so what are you exactly doing? And even if you needed the offset for pointer addresses, it would still be easier to store them as a number, not as strings. Perhaps you can show some code. Where are you getting the offsets from? And btw, you haven't fixed the single quotes, look at the syntax highlighting. Your code beneath the tables is red (which is a string). - pschulz
Sorry I rechecked it again...now it should be fixed with the quotes. I use LUA embedded in a Programm called Cheat Engine to use pointers. I want to print out my distance and angle to the all the current bots displayed in the game - sasa ha

2 Answers

0
votes

Generally speaking, in lua, you should use ~= nil to check if a value is `nil. So you can modify your code like this:

  for i=1, 12 do
    --Do the following calculations only if botallx or botally or botallz        
    -- are not nil
    if (botallx[i] ~= nil or botally[i] ~= nil or botallz[i] ~= nil ) then
       vecX=readFloat(me[1])-readFloat(botallx[i])
       vecY=readFloat(me[2])-readFloat(botally[i])
       vecZ=readFloat(me[3])-readFloat(botallz[i])

       distance=math.sqrt(math.pow(calcZ,2)+math.pow(calcX,2))

       angleX=math.deg(math.atan2(calcZ, calcX))

       print ("current distance is " .. distance)
       print ("current angle is " .. angleX)
    else --One of the botallx, botally, botallz value was nil
       print ("Can't perform calculations due to a nil value")
 end

Alternatively the above if statement can also be written like this:

if (botallx[i] not nil or botally[i] not nil or botallz[i] not nil )
0
votes

I think you want and rather than or here. With or, you're checking that at least one of them exists, and then using the ones that don't exist anyway.

if (botallx[i] ~= nil and botally[i] ~= nil and botallz[i] ~= nil ) then

Alternatively:

if botallx[i] and botally[i] and botallz[i] then

Although those botall addresses will always be non-nil the way you've set up your tables.

What are calcX and calcZ? Did you mean vecX and vecZ there? That's your nil arithmetic exception if so. If you didn't know, an undeclared variable has a nil value instead of causing an "unknown symbol" error like in most other languages.

Assuming that calcX problem is just a copy/paste error, you might need to do some nil checks on those readFloat(...) calls.

function sight_angle()
    if (isKeyPressed(VK_E)) then
        local meX = readFloat(me[1])
        local meY = readFloat(me[2])
        local meZ = readFloat(me[3])
        if not meX or not meY or not meZ then
            print ("Can't perform calculations due to a nil value")
            return
        end
        for i=1, 12 do
            -- Ditch the botall nil checks
            local bX = readFloat(botallx[i])
            local bY = readFloat(botally[i])
            local bZ = readFloat(botallz[i])
            
            if bX and bY and bZ then   
                local vecX=meX-bX
                local vecY=meY-bY
                local vecZ=meZ-bZ

                -- Assuming calcX and calcZ meant vecX and vecZ
                local distance=math.sqrt(math.pow(vecZ,2)+math.pow(vecX,2))

                local angleX=math.deg(math.atan2(vecZ, vecX))

                print ("current distance is " .. distance)
                print ("current angle is " .. angleX)
            else
                print ("Can't perform calculations due to a nil value")
            end
        end
    end
end

Of course, I have absolutely no idea what any of these things do! Is this a Cheat Engine plugin? I'm just basing this on what I can see in your code snippet.