Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Explain me print output
Started by gudrutis1 Mar 22 2020 11:56 AM

6 replies to this topic

Best Answer XeduR @Spyric , 22 March 2020 - 12:10 PM

Yes, that is normal.

 

When you insert test() inside print() then you essentially tell the code to run test function within the print function and all that does is print the letter z.

 

Now, since function test doesn't return anything, there is nothing inside print, so the "z" gets outputted by the function and the later print(test()) doesn't output anything.

 

If you would return something from the function, then the later print would output that, e.g.

local function test()
    print("z") --> z
    return "x"
end

test()
print(test()) --> z, x (on different rows)

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

gudrutis1

[GLOBAL: userInfoPane.html]
gudrutis1
  • Enthusiast

  • 37 posts
  • Corona SDK

I just wanted to ask, is this normal?

 

local function test()
print("z") --> z
end

test()
print(test()) --> z



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,246 posts
  • Corona SDK

  Best Answer

Yes, that is normal.

 

When you insert test() inside print() then you essentially tell the code to run test function within the print function and all that does is print the letter z.

 

Now, since function test doesn't return anything, there is nothing inside print, so the "z" gets outputted by the function and the later print(test()) doesn't output anything.

 

If you would return something from the function, then the later print would output that, e.g.

local function test()
    print("z") --> z
    return "x"
end

test()
print(test()) --> z, x (on different rows)


[TOPIC: post.html]
#3

gudrutis1

[GLOBAL: userInfoPane.html]
gudrutis1
  • Enthusiast

  • 37 posts
  • Corona SDK

I was just about to expand my question on different topic before you answered. I know it's not a good practice but it's not like a problem I just can't understand why.
 

[sample='']

local wordTable = {}
local word

local function createWord()
     word = display.newText("name", 100, 100, native.systemFont, 30)

     function word:updateWord()
          --do something
     end
return word
end

for i = 1, 3 do
     wordTable[#wordTable+1] = createWord()
end

for i = 1, #wordTable do
     word = wordTable[i]
     word:updateWord()
end

[/sample]

 
Why I cant have word:updateWord() function outside createWord() function? How is the function word:updateWord() is local just inside the other function, if it is? Why I can't in "word = wordTable[i]" change "word" with another word? How is that connected?

 

For me it looks like they all are separate working functions but obviously is not.

 

Edited. I am sorry, for some reason I can't post code properly.



[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,246 posts
  • Corona SDK

That's a simple issue with scope.

 

You've created local word outside of your createWord function, so every time you run it, you update the previously created word. If you just remove the local word from the top and add "local" in front of word inside your createWord, then it'll work.



[TOPIC: post.html]
#5

gudrutis1

[GLOBAL: userInfoPane.html]
gudrutis1
  • Enthusiast

  • 37 posts
  • Corona SDK

I know what you mean but for what I am trying to accomplish it haven't made a difference, it works both ways. I just think I should be able to do like in a code below and I don't see why it doesn't connect.

 

[sample='']

local wordTable = {}

local function createWord()
     local word = display.newText("name", 100, 100, native.systemFont, 30)
     word:setFillColor(0,0,0)
return word
end

local function a:updateWord()
     --do something
end

for i = 1, 3 do
     wordTable[#wordTable+1] = createWord()
end

 

--from here I am able to reach word from wordTable and I don't need to have original "local word" in a scope anymore

for i = 1, #wordTable do
     a = wordTable[i]
     a:updateWord()
end

[/sample]

 

How you post a code, please?



[TOPIC: post.html]
#6

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,246 posts
  • Corona SDK

You post code via the code formatting tools, i.e. the ones that look like <>.

 

Technically, when you have a function that starts with a colon, ":", then you have a method. Methods are functions that pass the table that they are attached to as the first argument. This means that you must directly attach said method to the object you want to use it from.

 

You can do so via external functions, but often they'll just end up being more work.

For instance,

local function addMethod( t )
    function t:print()
        print( "Hey" )
    end
end

local function newObject()
    local object = display.newRect( 0, 0, 100, 100 )
    addMethod( object )
    return object
end

local rect = newObject()
rect:print()

Compared to:

local function newObject()
    local object = display.newRect( 0, 0, 100, 100 )
    function object:print()
        print( "Hey" )
    end
    return object
end

local rect = newObject()
rect:print()

  • gudrutis1 likes this

[TOPIC: post.html]
#7

gudrutis1

[GLOBAL: userInfoPane.html]
gudrutis1
  • Enthusiast

  • 37 posts
  • Corona SDK

Right, I see. I was looking for an error in a wrong spot in the first place :D

 

Thank you.




[topic_controls]
[/topic_controls]