Jump to content

Variable changing value on loading new map!


Phodex Games
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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.

 

 

 

 

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

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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...