Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Variable disappearing after function end

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
ZacTheSin
I post too much
Reputation: 6

Joined: 09 May 2006
Posts: 2657

PostPosted: Sun Apr 12, 2015 3:38 am    Post subject: Variable disappearing after function end Reply with quote

After the first time, which seems to run fine - the script seems to never pick up that windowPreviousInteger has been created as a variable down the line.

Do I need to be defining it before the script starts (which would kind of defeat the purpose of the script in the first place)

Also, I do understand that this needs some major rewriting, which I plan on doing once I know that the variables are actually being seen.

Code:

function loadWindow() -- Window selection (executing function after first time connects gameWindow variable to find_window_match('Fushbot' .. windowPreviousInteger) - or it should)
  if(windowPreviousInteger == nil) then
    print('New Instance found')
    windowSharedInteger = allocateSharedMemory("windowInteger", 4096)
    gameWindow = winapi.find_window_match('game')
    if(windowSharedInteger == nil) then
      print('First instance found - creating windowSharedInteger')
      writeInteger(windowSharedInteger, 1)
      gameWindow:set_text('FushBot' .. readInteger(windowSharedInteger))
      gameWindow = winapi.find_window_match('FushBot' .. readInteger(windowSharedInteger))
      windowPreviousInteger = 0
      writeInteger(windowPreviousInteger, 1)
      print('windowPreviousInteger set as 1')
    else
      print('Connecting to client ' .. readInteger(windowSharedInteger) .. ', like a boa5.')
      writeInteger(windowSharedInteger, (readInteger(windowSharedInteger)+1))
      gameWindow:set_text('FushBot' .. readInteger(windowSharedInteger))
      gameWindow = winapi.find_window_match('FushBot' .. readInteger(windowSharedInteger))
      writeInteger(windowPreviousInteger, readInteger(windowSharedInteger))
      windowPreviousInteger = readInteger(windowPreviousInteger) -- Tried this to see if that would fix the creation of the variable - it should be global right?
    end
  else
    print('Existing client found, connecting to client ' .. readInteger(windowPreviousInteger))
    gameWindow = winapi.find_window_match('FushBot' .. readInteger(windowPreviousInteger))
  end
end

_________________
If someone helps you, why not Rep them?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25287
Location: The netherlands

PostPosted: Sun Apr 12, 2015 6:23 am    Post subject: This post has 1 review(s) Reply with quote

Code:
 
windowPreviousInteger = readInteger(windowPreviousInteger)

only gets executed when windowPreviousInteger is nil

when readInteger fails, it returns nil

also, windowSharedInteger should never be nil

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
ZacTheSin
I post too much
Reputation: 6

Joined: 09 May 2006
Posts: 2657

PostPosted: Sun Apr 12, 2015 8:08 am    Post subject: Reply with quote

Thanks DB, you the real MVP.
I ended up rewriting it and came out with this:

Code:
function windowSelect(fushBotSessionInt)
  if(fushBotSessionInt == 0) then
    print('New FushBot session') -- no previous Fushbot session found
    windowSharedInteger = allocateSharedMemory("windowInteger", 4096)
    gameWindow = winapi.find_window_match('game')
    if (windowSharedInteger == 0) then
      print('First instance found - creating windowSharedInteger')
      writeInteger(windowSharedInteger, 1)
      local gameWindowName = 'FushBot' .. readInteger(windowSharedInteger)
      gameWindow:set_text(gameWindowName)
      gameWindow = winapi.find_window_match(gameWindowName)
      print('fushBotSession:\t', readInteger(windowSharedInteger))
      return(readInteger(windowSharedInteger))
    elseif (windowSharedInteger > 0) then
      writeInteger(windowSharedInteger, (readInteger(windowSharedInteger)+1))
      local gameWindowName = 'FushBot' .. readInteger(windowSharedInteger)
      gameWindow:set_text(gameWindowName)
      gameWindow = winapi.find_window_match(gameWindowName)
      return(readInteger(windowSharedInteger))
    else
      print("Failed to find game client to connect new Fushbot session")
    end
  elseif(fushBotSessionInt > 0) then
    print('Existing Fushbot session found -- connecting ' .. fushBotSessionInt)
    gameWindow = winapi.find_window_match('FushBot' .. fushBotSessionInt)
    return fushBotSessionInt
  else
    print('No session found? Exiting.')
    return nil
  end
end


sitting inside of:
Code:
 if(fushBotSession == nil) or (fushBotSession == 0) then
    fushBotSession = windowSelect(0)
  elseif(fushBotSession > 0) then
    fushBotSession = windowSelect(fushBotSession)
  end


Which flows on from the scriptRun() function.

_________________
If someone helps you, why not Rep them?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25287
Location: The netherlands

PostPosted: Sun Apr 12, 2015 9:28 am    Post subject: Reply with quote

what i meant with "also, windowSharedInteger should never be nil" is that allocateSharedMemory("windowInteger", 4096) never fails (assuming it's attached to a process) it will always allocate a block referencing that shared memory block (creating it when neededl
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites