Jump to content
Genebris

Tables are global for all scripts?

Recommended Posts

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?

Share this post


Link to post

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.

Share this post


Link to post

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?

Share this post


Link to post

Anything you want to only apply to that specific object should be in the Start() function.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

×
×
  • Create New...