Genebris Posted February 19, 2015 Share Posted February 19, 2015 Something weird is happening. I have two objects with the scame script on them. Here is the code: Script.enabled=0 --bool "enabled" Script.int=0 Script.tab={1} function Script:UpdateWorld() System:Print("------------starts "..self.entity:GetKeyValue("name")) System:Print("int="..self.int) System:Print("#="..#self.tab) System:Print("[#]="..self.tab[#self.tab]) if not self.enabled then return end self.int=self.int+1 self.tab[#self.tab+1]=math.random(1,9) System:Print("-----------END "..self.entity:GetKeyValue("name")) end It has boolean variable which is set to true for one entity and to false for another entity in the editor. it also has one number variable (int) and one table (tab). Script with "enabled" entity ads +1 to int variable and one extra table element with random value each update. And here is what I get in output: ------------starts Pivot 1 (enabled) int=0 #=1 [#]=1 -----------END Pivot 1 (enabled) ------------starts Pivot 2 (disabled) int=0 #=2 [#]=4 ------------starts Pivot 1 (enabled) int=1 #=2 [#]=4 -----------END Pivot 1 (enabled) ------------starts Pivot 2 (disabled) int=0 #=3 [#]=8 ------------starts Pivot 1 (enabled) int=2 #=3 [#]=8 -----------END Pivot 1 (enabled) ------------starts Pivot 2 (disabled) int=0 #=4 [#]=7 ------------starts Pivot 1 (enabled) int=3 #=4 [#]=7 -----------END Pivot 1 (enabled) ------------starts Pivot 2 (disabled) int=0 #=5 [#]=9 ... (Added space after each update for easier reading) As you can see int variable increases in "enabled" entity and doesn't change in "disabled". Also disabled entity doesn't print last ("---END") line, so it is really disabled. But for some reason the table changes for both of those entities. It has same amount of elements and the last element is the same (it was generated randomly) for both scripts. Can you please explain me what is going on here? Quote Link to comment Share on other sites More sharing options...
Rick Posted February 19, 2015 Share Posted February 19, 2015 They are shared yes. I don't know if it's global or I think it might be just shared between any instance of that script. If you know C++ you can think of them as static variables inside a class. You'd get the same effect if you made a local at the top of the script I believe. 1 Quote Link to comment Share on other sites More sharing options...
Genebris Posted February 19, 2015 Author Share Posted February 19, 2015 That's unexpected. What if I want to make an inventory for NPCs? And what else is shared besides tables? Quote Link to comment Share on other sites More sharing options...
Josh Posted February 19, 2015 Share Posted February 19, 2015 Create the table in the Start() function and it won't be shared. 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Genebris Posted February 19, 2015 Author Share Posted February 19, 2015 Nice, thanks! I already was afraid there is no solution for this ) But is there something else I should create in start? Other types of variables are fine? Quote Link to comment Share on other sites More sharing options...
Josh Posted February 19, 2015 Share Posted February 19, 2015 Anything you want to only apply to that specific object should be in the Start() function. 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. 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.