Jump to content

Script and local usage


AggrorJorn
 Share

Recommended Posts

So I have been working on a flowgraph GUI and I am running in to an issue. When you click a button you can connect to a gui collection that hides all gui elements (in this example, 3 buttons) of a certain category. Everything works fine except when I add another gui collection to the scene.

 

Works fine..

post-45-0-38886600-1372805611_thumb.jpg

 

Once I add a new gui collection to the scene (also 3 buttons in the image), all 6 buttons hide the second gui collection, although the first 3 buttons are not connected to the second GUI collection.

post-45-0-85633900-1372805620_thumb.jpg

 

The problem must lie somewhere in the buttons array that I use to store the buttons. Buttons are added to this array on startup by looking through a gui collections children. I tried using both

 

Script.buttons = {}

--in start
--store children as buttons
self.childrenCount = self.entity:CountChildren()
for i=1, self.childrenCount, 1 do
self.buttons[i] = self.entity:GetChild(i-1)
end

 

as well as:

 

local buttons = {}

--in start
--store children as buttons
self.childrenCount = self.entity:CountChildren()
for i=1, self.childrenCount, 1 do
buttons[i] = self.entity:GetChild(i-1)
end

 

 

Any ideas?

Link to comment
Share on other sites

Rick found the solution to this problem. Declaring lua tables like this:

Script.myTable = {}
--or
local myTable = {}

 

somehow makes them global and thus they will overwrite each other when the script is attached to multiple objects. Rick found out that you can solve this as follow:

 

Script.myTable = nil

function Script:Start()
self.myTable = {}
end

Link to comment
Share on other sites

Generally, I find I need script values that are shared among the class, and others that are unique to each class. For example, you might have some object initialization stuff you only want to perform once, not for every single instance of the script.

 

The flowgraph stuff you're doing looks interesting.

 

 

Link to comment
Share on other sites

So like static/shared variables? That makes sense, but why is it only tables created outside any function act like this and not all Script. variables?

 

[edit]

I tested an exposed variable like Script.health and set it's value differently between 4 different entities, but now that I say this when the game loads you must be making the same named variable behind the scenes for self so that self.health is unique now per instance.

Link to comment
Share on other sites

Josh posted the following in another topic, but it is relevant to this topic.

Shared across all instances of the script:

Script.mytable = {}

function Script:Start()
end

 

Unique for each instance:

function Script:Start()
self.mytable = {}
end

 

Global variable:

mytable = {}

function Script:Start()
end

 

Global variable:

function Script:Start()
mytable = {}
end

 

Local variable:

local mytable = {}

function Script:Start()
end

 

Local variable:

function Script:Start()
local mytable = {}
end

Link to comment
Share on other sites

That is a bit odd.

so you define the lua parameters like these:

 

Script.myfloat = 12--float "myfloat"

and they are defined for all instances?

why can i change the parameters in the script-tab differently for each instance in the Editor?

Link to comment
Share on other sites

@beo6 The only thing I can think of is when the variable is exposed and given a value (at startup) Josh is making the exact same variable name but for self (or the actual entity instead of Script). That would make it unique per entity instance then. Some magic behind the doors.

 

I haven't tested but when accessing from another script I'm curious what entity.script.myfloat would return. Would that return the global script variable instead of the unique instance variable? I would think so. In which case making getters to return self.myfloat would be what is needed to get the unique instanced value so you could call entity.script:GetMyFloat(). Maybe I'll test later today.

Link to comment
Share on other sites

I should have explained that better...when you have exposed variables like that, they are initially set all the same, but the editor will adjust them post-loading.

 

Floats and integers are never shared, because they are just variables:

local a = 1
local b = a
a=2
print( b ) --Prints "1"

 

However, tables do not get copied when they are assigned:

local a = {}
a.value=1
local b = a
a.value=2
print( b.value ) --Prints "2"

 

 

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