FICHEKK
FICHEKK

Reputation: 829

Function which creates objects that have global name

I had this code which simply displays a picture I need in my game.

DeckOfPlayingCards = display.newImageRect("Graphics/ImproveMenu/Wealth/Luxury/Buy/DeckOfPlayingCards.png", 96*6, 96)
DeckOfPlayingCards.x = centerX
DeckOfPlayingCards.y = -90
DeckOfPlayingCards.name = "DeckOfPlayingCards"

Now, since I have a lot of these pictures planned to be in my game menu, I don't want to copy-paste that code all over. I wanted to make a simple function that would do it for me. This is what I tried, but failed and don't know why.

function DisplayObject(object, x, y, name)
        object = display.newImageRect("Graphics/ImproveMenu/Wealth/Luxury/Buy/" ..name..".png", 96*6, 96)
        object.x = x
        object.y = y
        object.name = name

        scrollView:insert(object)
end

DisplayObject(DeckOfPlayingCards, centerX, -90, "DeckOfPlayingCards")

This function can successfully show the picture, but whenever I try to do something with it (make it a button), it says the value is nil. After doing the function, I tried to do print the name like so:

function DisplayObject(object, x, y, name)
        object = display.newImageRect("Graphics/ImproveMenu/Wealth/Luxury/Buy/" ..name..".png", 96*6, 96)
        object.x = x
        object.y = y
        object.name = name

        scrollView:insert(object)
end

DisplayObject(DeckOfPlayingCards, centerX, -90, "DeckOfPlayingCards")

print(DeckOfPlayingCards.name)

It crashes and says that the value is nil.

Now, how can I exactly make my object with function that will be as I tried to write it like the first code? I hope someone can help me out.

Upvotes: 0

Views: 44

Answers (2)

Piglet
Piglet

Reputation: 28954

The error message tells you what is wrong. You cannot index a nil value. Therefor DeckOfPlayingCards must be nil.

Where do you initialize DeckOfPlayingCards? I understand that the first block of code is not part of your current script as you moved it into the new function DisplayObject.

Assuming that DeckOfPlayingCards is nil when you call DisplayObject(object, x, y, name) object is also nil within your function. As lua only creates a local copy of the function parameters you set the local variable object to the return value of display.newImageRect...

This does not change the value of DeckOfPlayingCards!!! It is still nil.

What you do is the same as:

function DisplayObject(x, y, name)        
  local object = display.newImageRect("Graphics/ImproveMenu/Wealth/Luxury/Buy/" ..name..".png", 96*6, 96)
        object.x = x
        object.y = y
        object.name = name

        scrollView:insert(object)
end

Return object and store it in DeckOfPlayingCards to solve your problem.

Upvotes: 0

TartanLlama
TartanLlama

Reputation: 65630

DisplayObject(DeckOfPlayingCards, centerX, -90, "DeckOfPlayingCards")

I think you expect this to act as passing the name DeckOfPlayingCards to DisplayObject, but it's just passing nil because it's an undefined variable.

One option is to simply create the object in DisplayObject, return it, then assign it to DeckOfPlayingCards:

function DisplayObject(x, y, name)
   local object = display.newImageRect(
       "Graphics/ImproveMenu/Wealth/Luxury/Buy/" ..name..".png",
       96*6, 96)
   object.x = x
   object.y = y
   object.name = name

   scrollView:insert(object)
   return object
end

DeckOfPlayingCards = DisplayObject(centerX, -90, "DeckOfPlayingCards")

print(DeckOfPlayingCards.name)

Upvotes: 2

Related Questions