Jump to content

Stream:Write() only gets executed when programm closed?


Phodex Games
 Share

Recommended Posts

Hi Leadwerks fellows,

 

I run into some trouble creating an save system. I noticed that when I saved I couldnt immediatly reload my save, to load it I had to restart the "game".

 

I will make it simple. To exclude other errors, I wrote this short line of code:

 

if window:KeyHit(Key.F1) then
self.test = FileSystem:WriteFile("test.lua")
self.test:WriteLine("Test")
System:Print("executing")
end

 

And again I noticed that while I am executing the programm and hit F1 (put a print function there to double check) and alt+tab the game and check the file, its empty. I use notepad++ to watch the file, and if I hit F1 it detectes a change and after reloading the file still nothing. Even if I close the application now, still empty. I need to close the whole file and open it again then I can see the text, but its not only a matter of visibility, I also tried to read the written text after saving it in the same session, doesnt work. So I guess the stream:WriteLine() operation gets executed on exit?

 

Is there any way of how to work around this or am I missing something. Maybe its possible to refresh the scene/word/app etc? I dont know, but I remember loading and saving in the same session was possible once...

 

However I would be very glad if someone would help me out smile.png

 

P.S.: Lua sandbox is DISABLED!

Link to comment
Share on other sites

So I guess the stream:WriteLine() operation gets executed on exit?

 

Without reading to far into this, my first question would be are you running this code under your main loop? "while" loop...?

That'd explain why it'd run on program exit, when we break out of your main loop it'd finally beable to reach this code.. although I could be very wrong cause I just skimmed through your problem.

 

It is very possible to save and load in the same session, and should be very simple... if you want help with this I'd need to know more about how you're going about doing this. Feel free to send me a forum PM or through my page add me on Steam...

This shouldn't be too hard to get working.

 

Keep in mind that FileSystem::WriteFile creates a NEW file and writes to it, or OVERWRITES existing files, to add to a file that already exists use FileSystem::OpenFile and FileSystem::WriteLine, you could even do a check to see if the file exists before trying to open it, and if it doesn't, create it.

 

What you have there should work assuming the code's being executed properly, so I'm going to assume maybe I misunderstood and you're not wanting the existing contents of the file to get overwritten? here's an example of that:

 

if window:KeyHit(Key.F1) then
local path = "test.lua" --Because I'm too lazy to re-write it over and over.
if FileSystem:GetFileType(path) == 0 then
self.test = FileSystem:WriteFile(path) --create file, doesn't exist.
else
self.test = FileSystem:OpenFile(path) --Open file, because it exists and we just want to add to it.
end
self.test:WriteLine("Test")
self.test:Release()
end

 

From the looks of your code I'm assuming you don't want the stream to stay opened, so that's how I wrote the example. Also, unless you're writing code to this file it really doesn't have to be a Lua file.

Link to comment
Share on other sites

First of all, thank you for the answer smile.png

 

[...]are you running this code under your main loop?

 

I think with main loop you mean the Script:UpdateWorld() function, if so yes it is running there.

 

Keep in mind that FileSystem::WriteFile creates a NEW file and writes to it, or OVERWRITES existing files[...]

 

Yes I know I want to overwrite the savefiles every time I save, because otherwise the savedata begins to stack in the file.

 

 

[...]unless you're writing code to this file it really doesn't have to be a Lua file.

 

That is exactly what I am doing smile.png and even then it doesnt have to be a lua file, you can call the extension whatever you want, Leadwerks can read it back in.

 

P.S.: Just tried your code and it works! Well I made a very stupid mistake, but I didnt know that it is needed, I am not very common with the stream operations. Yes I needed the stream to be open, BUT I forgot to release it at the end ^^ thats why it only writes my text after I quit the programm. However thank you very much smile.png

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...