making an action happen at the end
Is there a way to make it so a function runs when the project is stopped? Like if the window is closed?
Is there a way to make it so a function runs when the project is stopped? Like if the window is closed?
can you explain what your needing to happen
I don't think so if the project is stopped then the code cant run
I am so confused but do you mean something like this?
Your blue text here
gameOver = false
init = function() // Initialize game state end
update = function() if gameOver then // Call a function that handles "stopping" behavior handleProjectStop() else // Game logic continues end end
draw = function() // Drawing logic end
handleProjectStop = function() // This function will be called when the 'gameOver' condition is met print("Project is logically stopped. Performing cleanup or final actions.") // Example: Save game state, display final score, etc. end
// Example of how to trigger gameOver // This could be based on player health, time limit, etc. // For demonstration, we'll just set it after a few seconds timer = 0 update = function() timer += 1 if timer > 300 and not gameOver then // After 5 seconds (300 frames at 60fps) gameOver = true end
if gameOver then handleProjectStop() else // Normal game updates end end
I am so confused but do you mean something like this?
This text will appear in blue in the microStudio community tab.
gameOver = false
init = function() // Initialize game state end
update = function() if gameOver then // Call a function that handles "stopping" behavior handleProjectStop() else // Game logic continues end end
draw = function() // Drawing logic end
handleProjectStop = function() // This function will be called when the 'gameOver' condition is met print("Project is logically stopped. Performing cleanup or final actions.") // Example: Save game state, display final score, etc. end
// Example of how to trigger gameOver // This could be based on player health, time limit, etc. // For demonstration, we'll just set it after a few seconds timer = 0 update = function() timer += 1 if timer > 300 and not gameOver then // After 5 seconds (300 frames at 60fps) gameOver = true end
if gameOver then handleProjectStop() else // Normal game updates end end
I am so confused but do you mean something like this?
gameOver = false
init = function() // Initialize game state end
update = function() if gameOver then // Call a function that handles "stopping" behavior handleProjectStop() else // Game logic continues end end
draw = function() // Drawing logic end
handleProjectStop = function() // This function will be called when the 'gameOver' condition is met print("Project is logically stopped. Performing cleanup or final actions.") // Example: Save game state, display final score, etc. end
// Example of how to trigger gameOver // This could be based on player health, time limit, etc. // For demonstration, we'll just set it after a few seconds timer = 0 update = function() timer += 1 if timer > 300 and not gameOver then // After 5 seconds (300 frames at 60fps) gameOver = true end
if gameOver then handleProjectStop() else // Normal game updates end end
I am so confused but do you mean something like this?
gameOver = false
init = function()
// Initialize game state
end
update = function()
if gameOver then
// Call a function that handles "stopping" behavior
handleProjectStop()
else
// Game logic continues
end
end
draw = function()
// Drawing logic
end
handleProjectStop = function()
// This function will be called when the 'gameOver' condition is met
print("Project is logically stopped. Performing cleanup or final actions.")
// Example: Save game state, display final score, etc.
end
// Example of how to trigger gameOver
// This could be based on player health, time limit, etc.
// For demonstration, we'll just set it after a few seconds
timer = 0
update = function()
timer += 1
if timer > 300 and not gameOver then // After 5 seconds (300 frames at 60fps)
gameOver = true
end
if gameOver then
handleProjectStop()
else
// Normal game updates
end
end
Okay, to clear up confusion I'm trying to make it so it saves variables when it stops.
ohhh like this?
// The variable we want to save
global.score = 0
// This function is called once when the project starts
init = function()
print("Initializing...")
// Load the saved data from the file
loadData()
// Example: Change the score to demonstrate saving
score += 10
print("Score is now: " + score)
// Register a function to be called when the project is closed
on_project_closing(saveData)
end
// Function to save the variable to a file
saveData = function()
print("Saving data...")
local data = object
data.score = score
// Use `fs.write` to save the variable as a JSON string
fs.write( "save.json", JSON.stringify( data ) )
print("Data saved.")
end
// Function to load the variable from a file
loadData = function()
print("Loading data...")
local data = null
if fs.exists( "save.json" ) then
// Use `fs.read` to read the file and `JSON.parse` to convert it back to an object
data = JSON.parse( fs.read( "save.json" ) )
end
if data != null then
score = data.score
print("Data loaded. Previous score was: " + score)
else
print("No save file found. Starting with default score.")
end
end
Wait how does on_project_close() work?