<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic 'Self' returns a Nil value - basic lua question in Stingray Forum (Read Only)</title>
    <link>https://forums.autodesk.com/t5/stingray-forum-read-only/self-returns-a-nil-value-basic-lua-question/m-p/6660183#M6353</link>
    <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to learn a bit more about lua and using the API reference to create some functions in lua. My lua is very basic.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I managed to write a function that will spawn a unit on a keyboard input using this code and script call global in flow:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;function SphereSpawn()&lt;BR /&gt;unit_sphere = World.spawn_unit(SimpleProject.world, "content/models/BUILDING B1/Sphere")&lt;BR /&gt;end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;now i want to be able to turn the visibility of this object off. So using the API reference i found "set_unit_visibility".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I then wrote this function:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;function SphereVisibility(self)&lt;BR /&gt;unit_sphere = self.unit&lt;BR /&gt;stingray.unit.set_unit_visiblity(unit_sphere, false)&lt;BR /&gt;end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but i keep getting an error saying that 'self' is a nil value. I think i can see why, but just can't get my head around it yet.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone give me some more information about the 'self' side of things and how that is declared?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 01 Nov 2016 16:16:21 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2016-11-01T16:16:21Z</dc:date>
    <item>
      <title>'Self' returns a Nil value - basic lua question</title>
      <link>https://forums.autodesk.com/t5/stingray-forum-read-only/self-returns-a-nil-value-basic-lua-question/m-p/6660183#M6353</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to learn a bit more about lua and using the API reference to create some functions in lua. My lua is very basic.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I managed to write a function that will spawn a unit on a keyboard input using this code and script call global in flow:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;function SphereSpawn()&lt;BR /&gt;unit_sphere = World.spawn_unit(SimpleProject.world, "content/models/BUILDING B1/Sphere")&lt;BR /&gt;end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;now i want to be able to turn the visibility of this object off. So using the API reference i found "set_unit_visibility".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I then wrote this function:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;function SphereVisibility(self)&lt;BR /&gt;unit_sphere = self.unit&lt;BR /&gt;stingray.unit.set_unit_visiblity(unit_sphere, false)&lt;BR /&gt;end&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but i keep getting an error saying that 'self' is a nil value. I think i can see why, but just can't get my head around it yet.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone give me some more information about the 'self' side of things and how that is declared?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Nov 2016 16:16:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/stingray-forum-read-only/self-returns-a-nil-value-basic-lua-question/m-p/6660183#M6353</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-11-01T16:16:21Z</dc:date>
    </item>
    <item>
      <title>Re: 'Self' returns a Nil value - basic lua question</title>
      <link>https://forums.autodesk.com/t5/stingray-forum-read-only/self-returns-a-nil-value-basic-lua-question/m-p/6660295#M6354</link>
      <description>&lt;P&gt;'self' is the reference to the current object. As for why it's nil, you can either trace through the code in the debugger line by line or add print() calls:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt; print("Value of self is now:" .. self)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Keep adding print() calls to your code until you find the point where self ends up nil.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Nov 2016 16:57:12 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/stingray-forum-read-only/self-returns-a-nil-value-basic-lua-question/m-p/6660295#M6354</guid>
      <dc:creator>__ben__</dc:creator>
      <dc:date>2016-11-01T16:57:12Z</dc:date>
    </item>
    <item>
      <title>Re: 'Self' returns a Nil value - basic lua question</title>
      <link>https://forums.autodesk.com/t5/stingray-forum-read-only/self-returns-a-nil-value-basic-lua-question/m-p/6661211#M6357</link>
      <description>&lt;P&gt;when you call the SphereVisibility(self) function, what do you pass it as an argument? Maybe a longer snippet would help.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Nov 2016 23:02:19 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/stingray-forum-read-only/self-returns-a-nil-value-basic-lua-question/m-p/6661211#M6357</guid>
      <dc:creator>_robbs_</dc:creator>
      <dc:date>2016-11-01T23:02:19Z</dc:date>
    </item>
    <item>
      <title>Re: 'Self' returns a Nil value - basic lua question</title>
      <link>https://forums.autodesk.com/t5/stingray-forum-read-only/self-returns-a-nil-value-basic-lua-question/m-p/6667015#M6391</link>
      <description>&lt;P&gt;Thanks for the replies guys.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think i'm just too new to lua's syntax to fully understand what i'm doing. I've done a fair amount of reading but havent had that moment where it all comes together and i understand what is going on. I know it has something to do with object orientated programming and passing arguments for the function etc.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The API states that set_unit_visibility requires (self, visibility), and that self is stingray.unit. What exactly does that mean? If i want to set the visibility of a unit named Box001, is that asking for (Box001.unit, false)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm sure all of this is easy, but im just stuck understanding it still.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 04 Nov 2016 08:25:07 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/stingray-forum-read-only/self-returns-a-nil-value-basic-lua-question/m-p/6667015#M6391</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-11-04T08:25:07Z</dc:date>
    </item>
    <item>
      <title>Re: 'Self' returns a Nil value - basic lua question</title>
      <link>https://forums.autodesk.com/t5/stingray-forum-read-only/self-returns-a-nil-value-basic-lua-question/m-p/6667161#M6392</link>
      <description>&lt;P&gt;`stingray.Unit.set_unit_visibility` takes a `Unit` as parameter 1 and then a boolean.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;local unit = stingray.World.spawn_unit(SimpleProject.world, "content/models/BUILDING B1/Sphere")
stingray.Unit.set_unit_visibility(unit, false)
&lt;/PRE&gt;
&lt;P&gt;Since `unit` is a lua `Unit` object, the `self` lua machinery can be used to call it this way instead:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;unit:set_unit_visibility(false)
&lt;/PRE&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;unit.set_unit_visibility(unit, false)&lt;/PRE&gt;
&lt;P&gt;The `self` functionality is just a helper to automatically pass a variable to an object function. I suggest reading over the object-oriented docs at the Lua site:&lt;BR /&gt;&lt;A href="https://www.lua.org/pil/16.html" target="_blank"&gt;https://www.lua.org/pil/16.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Also note that the functions you wrote are global functions, and the `self` logic is used by table functions, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;-- Global function
function some_global_function(x, y)
end

-- Table functions

-- a table
local some_table = {}

-- This object function doesn't have a self parameter
function some_table.do_something_without_self(x, y)
end

-- This function has a hidden `self` first parameter because of the `:`
function some_table:do_something(x, y) -- actually takes (self, x, y)
end

-- This function signature is equivalent to the previous one: (self, x, y)
function some_table.do_something_else(self, x, y)
end

some_table.do_something_without_self(1, 2) -- ok
some_table:do_something(1, 2) -- ok. passes `some_table` as `self` implicitly because of the `:`
some_table.do_something(some_table, 1, 2) -- ok. we are calling the function with `.` instead of `:` so we must pass `some_table` as self
some_table:do_something_else(1, 2) -- this version acts just like the other
some_table.do_something_else(some_table, 1, 2)
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Nov 2016 09:55:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/stingray-forum-read-only/self-returns-a-nil-value-basic-lua-question/m-p/6667161#M6392</guid>
      <dc:creator>t_livee</dc:creator>
      <dc:date>2016-11-04T09:55:29Z</dc:date>
    </item>
  </channel>
</rss>

