Scripts / Weird problem with 'for' and 'io.open'

Author
Message
Moshroom
9
Years of Service
User Offline
Joined: 18th Jul 2015
Location: Finland
Posted: 26th Oct 2015 12:55
Hi. I'm trying to create a graphical interface for my save/load script and I encountered pretty weird error with the following code:

The purpose of this code is to draw a list of saved games. I there is no saved game, file returns nil and the program writes "empty slot". If the save exist, the program writes the number of the slot (this way just for a proof of principle. Later I'm going to add some details about the saved game to the list).

But my problem is that occasionally (about once every 10 seconds) the used slots change to "empty slot" for a second and then turn back to SlotX. Also the code takes a LOT of memory to run. My guess is that either GameGuru somehow wastes memory when handling 'for' statement (kind of explains why almost all of the default scripts use merely if-else statements), or (which I feel is more likely) that io.open("slot" .. i .. ".dat", "r") eats unneccessarily lot of memory. Has somebody else encountered this before? Does an io command exist to just check whether a file exists without opening it?

(I know that this can be worked around by reading all the dats once, storing their data to unique local variables and building the list using if-else statements, but this takes almost 30 lines of pretty non-elegant and self-repeating code. I post this here because I'd like to know whether there is any better way to do it.)
Bored of the Rings
GameGuru Master
19
Years of Service
User Offline
Joined: 25th Feb 2005
Location: Middle Earth
Posted: 26th Oct 2015 13:14
Where are you placing the code within the function?
Professional Programmer: Languages- SAS (Statistical Analysis Software) , C++, SQL, PL-SQL, JavaScript, HTML, Darkbasic Pro (still love this language), Purebasic, others
Hardware: Dell Precision 490; AMD Radeon HD 7570; LG TFT monitor (widescreen). Wanting a new PC at some point.
Interests: Drumming, Saxophone, Art, Theatre, Music.
Moshroom
9
Years of Service
User Offline
Joined: 18th Jul 2015
Location: Finland
Posted: 26th Oct 2015 15:25
Well, the script is far from finished, but here's the context so far. ActiveMenu is set as 6 when player calls save menu (I haven't yet decided how player should do this. Probably from main menu or by pressing E near save point):
The cursor is controlled by Command() which is introduced as a separate function before main().
NUM_SLOTS is 7 and TIME_W is 145. I don't think Command() is causing problems because other menus using it are working just fine. "Top panel" is introduced if any of my menus is active (I'm planning to create various menus that work with the same logic, for example inventory and shop.)
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 26th Oct 2015 16:20
life\'s one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, AMD R9 200 series , directx 11
Moshroom
9
Years of Service
User Offline
Joined: 18th Jul 2015
Location: Finland
Posted: 26th Oct 2015 17:56 Edited at: 26th Oct 2015 18:06
Quote: "io.close(file)"

HAHAHA! I feel like an idiot now. Thanks. Sometimes the most obivious thing is the most difficult to notice.

By the way in this case 'for' and 'while' statements have at least superficially similar effects. Is there some (for example memory or performace related) reason to use 'while' instead of 'for' that I'm not aware of?
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 26th Oct 2015 18:46
'for' is generally better and easier to use in lua/GG from my experience (much less likely to cause system lock up too), i just used 'while' as an alternative while i was testing and added the 'close' later when it still didnt work.
life\'s one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, AMD R9 200 series , directx 11
3com
10
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 27th Oct 2015 17:15
Quote: "Does an io command exist to just check whether a file exists without opening it?"


This function might help:



hth

3com

Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM

Login to post a reply

Server time is: 2024-12-22 06:53:21
Your offset time is: 2024-12-22 06:53:21