Phodex Games Posted June 26, 2017 Share Posted June 26, 2017 Hi, I have a general inventory script attached to every object, which should have an inventory. In the script settings of the inventory script I put an "firstSlotOffset" value, to adjust where the inventory starts to render its slots. In code this looks like that: Script.firstSlotOffset = Vec2(0, 0) --Vec2 "Slot Offs." In the start function I do some calculation: self.firstSlotOffset.x = self.firstSlotOffset.x * context:GetWidth() self.firstSlotOffset.y = self.firstSlotOffset.y * context:GetHeight() The problem is if I load to another map and back to some other map the self.firstSlotOffset value seems corrupted. This happens only after I loaded twice (so changed the map two times). I found that out by printing the self.firstSlotOffset value in the start function. What I mentioned is if you loaded twice the self.firstSlotOffset value is not what is set in the script settings but the result of the above calculation which should not be. If you change a map, all variables should be "nil"-ed shouldn't they? And by the way what do you have to take care on when loading to an new map? Do you need to clean stuff up and if yes which stuff? I guess that could be the problem that I didnt clean up. P.S.: The error only appears for the player inventory, which is the only entity staying the same on both maps I load to. Quote Link to comment Share on other sites More sharing options...
macklebee Posted June 26, 2017 Share Posted June 26, 2017 Why would the variable get set to nil unless you program it? I assume you didn't write anything in the function 'Script:Cleanup()'. https://www.leadwerks.com/docs.php?page=Tutorials_Lua-Scripting_Introduction-to-Lua# This function is called when every single instance of the script is detached, and the script is no longer in use. You can use this to delete any shared resources the script uses. Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
Phodex Games Posted June 27, 2017 Author Share Posted June 27, 2017 8 hours ago, macklebee said: Why would the variable get set to nil unless you program it? Well maybe I formulated this wrong. I mean variables don't get saved on their own if you change maps. The weird thing is it works the first time, but second time I change the map it doesn't. Even tried diffrent maps. I also tried the Cleanup function & the Detach function to set the variable to nil, but still the problem exists. Quote Link to comment Share on other sites More sharing options...
Rick Posted June 27, 2017 Share Posted June 27, 2017 Since script variables are part of an entity and when you load a new map I'm pretty sure it clears out all entities from the last load, then all those script instances should get deleted which would include your script variables. Every time you load a new map it should be starting fresh with script variables. If you are using global variables anywhere then those wouldn't get deleted. Quote Link to comment Share on other sites More sharing options...
Phodex Games Posted June 28, 2017 Author Share Posted June 28, 2017 19 hours ago, Rick said: Every time you load a new map it should be starting fresh with script variables. Hi Rick. Yes this is exactly what I meant. I will do some debugging and researching on this, but could this be a Leadwerks bug maybe? Maybe I made some mistake somewhere we will see... Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted June 28, 2017 Share Posted June 28, 2017 Just a side note that might not have something to do with this: There is an optimization by default present that looks in the new scene to see if any entities used by the old scene can be moved over instead of being deleted. This works by looking at the reference count. I doubt it would actually move the existing entity with script. Quote Link to comment Share on other sites More sharing options...
Phodex Games Posted July 3, 2017 Author Share Posted July 3, 2017 I did some debugging etc and kind of fixed the Problem better said worked around it like the following: --DOES NOT WORK: self.firstSlotOffset.x = self.firstSlotOffset.x * context:GetWidth() self.firstSlotOffset.y = self.firstSlotOffset.y * context:GetHeight() --WORKS: self.test = nil self.test = self.firstSlotOffset.x * context:GetWidth() self.test = self.firstSlotOffset.y * context:GetHeight() So I think you see the problem. Still I dont understand what the problem actually is. I encountered the same kind of problem for some other scripts as well. Would be glad if somebody could explain this. P.S.: Currently have no working internet at home so it can take a while until I reply Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.