# Self calls don't work in custom functions (SOLVED)

**URL:** <https://forum.defold.com/t/self-calls-dont-work-in-custom-functions-solved/36196>\
**Category:** Questions\
**Created:** [November 29, 2018, 3:23am UTC](https://forum.defold.com/t/self-calls-dont-work-in-custom-functions-solved/36196 "2018-11-29T03:23:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArtemisVenatrix](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/artemisvenatrix/32/12199_2.png) [@ArtemisVenatrix](https://forum.defold.com/u/ArtemisVenatrix)\
**Post date:** [November 29, 2018, 3:23am UTC](https://forum.defold.com/t/self-calls-dont-work-in-custom-functions-solved/36196/1 "2018-11-29T03:23:09Z")

</div>

When I call self.x to reference a property in init it works just find, but in get\_story it returns an error. When I reference story in get\_story it returns and empty string, but returns what I assign to it when I reference it in the init function.

```lua
go.property("x", 0)
go.property("y", 0)
local story

function get_story(self)
	print(self.x)
	return story
end

function init(self)
	local f = io.open(sys.get_save_file("laser", "grid_data"), "r")
	local dataS = f:read("*a")
	local data = json.decode(dataS)
	--print(dataS)
	f:close()
	print(self.x, self.y)
	story = data[self.x][self.y]
	print(story)
end

```

---

<div class="post-metadata">

**Author:** ![COCO](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/coco/32/38692_2.png) [@COCO](https://forum.defold.com/u/COCO)\
**Post date:** [November 29, 2018, 3:38am UTC](https://forum.defold.com/t/self-calls-dont-work-in-custom-functions-solved/36196/2 "2018-11-29T03:38:16Z")

</div>

```
go.property("x", 0)
go.property("y", 0)
local story

function get_story(self)
	print(self.x)
	return story
end

function init(self)
	print(self.x, self.y)
	story = "something"
	print(get_story(self))
end

```

This works for me just fine.

---

<div class="post-metadata">

**Author:** ![ArtemisVenatrix](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/artemisvenatrix/32/12199_2.png) [@ArtemisVenatrix](https://forum.defold.com/u/ArtemisVenatrix)\
**Post date:** [November 29, 2018, 3:46am UTC](https://forum.defold.com/t/self-calls-dont-work-in-custom-functions-solved/36196/3 "2018-11-29T03:46:57Z")

</div>

Try calling it on a cell from the mainscript. The cell is a factory product calling the generated reference from factory.create as self. How do i reference the data of a factory product from a seperate script since the getter method doesnt work.

---

<div class="post-metadata">

**Author:** ![ross.grams](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@ross.grams](https://forum.defold.com/u/ross.grams)\
**Post date:** [November 29, 2018, 4:18am UTC](https://forum.defold.com/t/self-calls-dont-work-in-custom-functions-solved/36196/4 "2018-11-29T04:18:52Z")

</div>

What `factory.create` returns is not a reference, it is just the hashed ID of the new object. Basically just its name. You can’t use that as `self`. Are you used to using another game engine? With Defold you generally don’t call functions on game objects. If you want to communicate between objects you use [message passing](https://www.defold.com/manuals/message-passing/). Or, if you just need to transfer data you can use [`go.get`](https://www.defold.com/ref/go/#go.get:url-property) and [`go.set`](https://www.defold.com/ref/go/#go.set:url-property-value) with script properties, though the data types you can use with [script properties](https://www.defold.com/manuals/script-properties/#_defining_a_script_property) are limited. Another option is to use a lua module with a lookup table or whatever (you can use a hashed ID as a table key for example).

There was [another thread here](https://forum.defold.com/t/set-constant-on-go-created-by-factory/35850) recently about setting and getting stuff from factory-created objects.

Also, it’s generally a bad idea to define global functions (any custom function you define without the `local` keyword is global). It’s better to put things like that in a module. It’s way too easy to forget that you defined that name globally and cause yourself all sorts of weird bugs and headaches. A bit more about that in [this thread](https://forum.defold.com/t/how-to-create-and-use-global-constants-solved/34536).

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [November 29, 2018, 4:37am UTC](https://forum.defold.com/t/self-calls-dont-work-in-custom-functions-solved/36196/5 "2018-11-29T04:37:54Z")

</div>

You need to pass self to the function. It is not something the engine will do for you.

---

<div class="post-metadata">

**Author:** ![ArtemisVenatrix](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/artemisvenatrix/32/12199_2.png) [@ArtemisVenatrix](https://forum.defold.com/u/ArtemisVenatrix)\
**Post date:** [November 30, 2018, 12:40am UTC](https://forum.defold.com/t/self-calls-dont-work-in-custom-functions-solved/36196/6 "2018-11-30T00:40:32Z")

</div>

thanks I got it sorted
