Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Objects inside functions
Started by iphone_2010 Nov 22 2010 05:06 AM

4 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

iphone_2010

[GLOBAL: userInfoPane.html]
iphone_2010
  • Enthusiast

  • 92 posts
  • Guests

I have an object defined as global in my main.lua, in my library.lua I have defined a global function called doSomthing(). In the main.lua I'm trying to change the property of that object but for some reason I always get the following error:-
Attempt to set the value of 'myobject' a nil value.

I would appreciate your help in this matter.

uid: 11038 topic_id: 3866 reply_id: 303866


[TOPIC: post.html]
#2

jmp909

[GLOBAL: userInfoPane.html]
jmp909
  • Corona Geek

  • 1,328 posts
  • Corona SDK

I don't think you've explained that right. You've said your object is in main.lua and you're trying to change it in main.lua

Anyway it's only global if you use the global _G

ie _G.something = "whatever"

Otherwise you'll probably need to pass a reference to main into library eg library.root = self (in main, if this doesn't work use a setter function eg setRoot), then root.myMainObject = "whatever" in your library function
uid: 6645 topic_id: 3866 reply_id: 12049


[TOPIC: post.html]
#3

iphone_2010

[GLOBAL: userInfoPane.html]
iphone_2010
  • Enthusiast

  • 92 posts
  • Guests

Im sorry, but what i meant is the variable i want to access is inside my library.lua.
I didnt understand what u've explained with regard to a setter function. Would u give me a brief example so i can understand properly

Thanks
uid: 11038 topic_id: 3866 reply_id: 12053


[TOPIC: post.html]
#4

jmp909

[GLOBAL: userInfoPane.html]
jmp909
  • Corona Geek

  • 1,328 posts
  • Corona SDK

main.lua
local library = require("library")library.setX(50)print(library.getX()); -- => 50


library.lua
......local _x = 0function setX(n)_x = nendfunction getX()return _xend
uid: 6645 topic_id: 3866 reply_id: 12132


[TOPIC: post.html]
#5

jmp909

[GLOBAL: userInfoPane.html]
jmp909
  • Corona Geek

  • 1,328 posts
  • Corona SDK

also you need to define your problem more clearly. where is your object defined. where are you trying to access it and what are you trying to do to it?

uid: 6645 topic_id: 3866 reply_id: 12133



[topic_controls]
[/topic_controls]