how do i not allow more than 1 instance of an app?

Blitz3D Forums/Blitz3D Beginners Area/how do i not allow more than 1 instance of an app?

Whats My Face(Posted 2009) [#1]
So, right now I have a game based on timing, however, to the people i've been giving this to have been getting around the timing that you need to beat a boss in the game. They've been opening multiple instances of the game and slowing it down in order to not need the skills required to beat the boss.

Is there any way to prevent multiple instances of this? I can't find a way right now, and I'm not really sure how i can use inline c++ cause I know how to do it in c++ but i'm not very familiar with DLL's if anyone can give me a small tutorial on inline c++ or show me how to do it directly in blitz.

thanks


Warner(Posted 2009) [#2]
If your app has a specific title, you can use api_FindWindow to search for the running application. If one is found, the application quits.
Sort of this:
If api_FindWindow("this_game") <> 0 Then RunTimeError "game is allready running"

AppTitle "this_game"
Repeat
  Cls
  Text 0, 0, "running.."
  Flip
Until Keyhit(1)

api_FindWindow should be declared in a .decls file. I think you can find it's definition somewhere in the code archive.


stanrol(Posted 2010) [#3]
or a mutex DLL.


xtremegamr(Posted 2010) [#4]
On a side note: If opening multiple instances of your game slows down your computer, try using a 'Delay 1' at the end of your game's main loop. This should free up the processor a little bit.

If that doesn't work, try limiting your game's frame rate:

;;some globals
Global fl_frameratecap=60 ;what you want to limit your framerate to
Global fl_loopstart ;used to calculate fl_looptime
Global fl_looptime ;the amount of time (in milliseconds) that your main loop took
Global fl_targetlooptime=1000/fl_frameratecap ;what you want fl_looptime to be

;;at the beginning of your main loop
fl_loopstart=MilliSecs()

;;at the end of your main loop
fl_looptime=MilliSecs()-fl_loopstart ;calculate the loop time
diff=fl_targetlooptime-fl_looptime ;the difference between the target and actual looptimes
Delay diff ;delay the loop




stanrol(Posted 2010) [#5]
or vwait


Adam Novagen(Posted 2010) [#6]
My personal favorite method is TCP streams sent to 127.0.0.1 (internal "home" port, stays within the computer itself). There's an example in the Command Reference - in the TCP (Network) section - that lets you create two Blitz programs that "talk" to each other.

Here's the fun part though: there's a library that takes care of all the messy TCP stuff for you in a few simple functions, and I made it. XD Go to my website, [a http://www.ghzgames.com/], go to the "GhzLib" page and download it. The APP Message function set is what you're after, look for 'em in the HelpDocs. :D


_PJ_(Posted 2010) [#7]

or vwait


Bad idea.
vwait does not relieve the processor at all.


xlsior(Posted 2010) [#8]
The problem with checking for other instances by TCP is that it won't work on many computers that are running a software firewall.

It's much better to use the windwos API to obtain the list of running processes upon app launch, and have the application terminate itself if it detects another instance (optionally change focus to the other instance first)