'Self' returns a Nil value - basic lua question

'Self' returns a Nil value - basic lua question

Anonymous
Not applicable
1,450 Views
4 Replies
Message 1 of 5

'Self' returns a Nil value - basic lua question

Anonymous
Not applicable

Hi guys,

 

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. 

I managed to write a function that will spawn a unit on a keyboard input using this code and script call global in flow:

 

function SphereSpawn()
unit_sphere = World.spawn_unit(SimpleProject.world, "content/models/BUILDING B1/Sphere")
end

 

now i want to be able to turn the visibility of this object off. So using the API reference i found "set_unit_visibility".

 

I then wrote this function:

 

function SphereVisibility(self)
unit_sphere = self.unit
stingray.unit.set_unit_visiblity(unit_sphere, false)
end

 

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. 

 

Can someone give me some more information about the 'self' side of things and how that is declared? 

 

Thanks!

 

 

Reply
Reply
0 Likes
1,451 Views
4 Replies
Replies (4)
Message 2 of 5

__ben__
Alumni
Alumni

'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:

 


print("Value of self is now:" .. self)

 

Keep adding print() calls to your code until you find the point where self ends up nil.

Reply
Reply
0 Likes
Message 3 of 5

_robbs_
Alumni
Alumni

when you call the SphereVisibility(self) function, what do you pass it as an argument? Maybe a longer snippet would help.

Reply
Reply
0 Likes
Message 4 of 5

Anonymous
Not applicable

Thanks for the replies guys.

 

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. 

 

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)?

 

I'm sure all of this is easy, but im just stuck understanding it still. 

 

Thanks

Reply
Reply
0 Likes
Message 5 of 5

t_livee
Alumni
Alumni

`stingray.Unit.set_unit_visibility` takes a `Unit` as parameter 1 and then a boolean.

 

 

local unit = stingray.World.spawn_unit(SimpleProject.world, "content/models/BUILDING B1/Sphere")
stingray.Unit.set_unit_visibility(unit, false)

Since `unit` is a lua `Unit` object, the `self` lua machinery can be used to call it this way instead:

 

 

unit:set_unit_visibility(false)

or

 

unit.set_unit_visibility(unit, false)

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:
https://www.lua.org/pil/16.html


Also note that the functions you wrote are global functions, and the `self` logic is used by table functions, e.g.:

 

 

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

 

 

Reply
Reply
0 Likes