Thread Tools Display Modes
08-11-16, 06:18 PM   #1
candrid
Premium Member
 
candrid's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 61
print not working

Hello all.

I just started with Lua and I was wondering why the following does not work:
Code:
print("hello there");
Has this been updated to a different method?

I am trying to have a greeting send to the default chat window but the tutorials I am looking at seem to be a bit outdated.

Thank you in advance.
  Reply With Quote
08-11-16, 06:24 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,878
How/where are you using this?

If you are putting it in the main chunk of your code and there is other text printed to your chat window during login, your print statement may be at the top of the chat text listing if you scroll it up.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
08-11-16, 06:30 PM   #3
Joker119
A Flamescale Wyrmkin
 
Joker119's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2014
Posts: 113
Originally Posted by candrid View Post
Hello all.

I just started with Lua and I was wondering why the following does not work:
Code:
print("hello there");
Has this been updated to a different method?

I am trying to have a greeting send to the default chat window but the tutorials I am looking at seem to be a bit outdated.

Thank you in advance.
Are you running it completely by itself in a lua file? Because you'll probably have a bad time.
Print needs to be run as part of a defined function. Or are you trying to run it through a slash command in-game to test it out?

in-game
Code:
 /console script(print("hello"))
Make a simple addon for it:

Lua Code:
  1. ## Interface: 50001
  2. ## Title: Hello World
  3. ## Author: Joe Smith
  4. ## Version: 0.1
  5.  
  6. local f = CreateFrame("Frame")
  7. f:RegisterEvent("PLAYER_LOGIN")
  8. f:SetScript("OnEvent", function(f, event)
  9.     if event == "PLAYER_LOGIN" then
  10.       print("Hello, world!")
  11.     end
  12. end)

That should print the message "Hello, World!" into your chat frame when you login, or
Lua Code:
  1. local f = CreateFrame("Frame")
  2. f:RegisterEvent("PLAYER_LEVEL_UP")
  3. f:SetScript("OnEvent", function(f, event)
  4.     if event == "PLAYER_LEVEL_UP" then
  5.       print("Congratulations on reaching level"..UnitLevel("player").." programmer!")
  6.     end
  7. end)

Basically, print in lua has to be within a function, that function has to be called into existence somehow.

Here's a neat guide I believe is still mostly useful: http://www.dev-hq.net/posts/2--creat...warcraft-addon
  Reply With Quote
08-11-16, 07:03 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
The orginal code should work fine. This is not accurate:

Originally Posted by Galaxy119 View Post
Print needs to be run as part of a defined function.
print can be run anywhere, at any time. In fact, no function can only be called from inside another function; that's just not how Lua works, and wouldn't really make much sense -- if functions could only be called from inside other functions, how would you call those outer functions to make anything happen in the first place?

However, if you're using an addon that makes the chat frame keep more messages in its scroll-back history, then anything printed before that addon loads will be erased. You can (usually, depending on the addon) work around this by waiting until the PLAYER_LOGIN event to print your message.

Other than that, we need more context in order to figure out your problem. If you're using print in an addon, show the rest of the code.

If you're typing /run print("Hello world") the chat box and it's not working, either some other addon is interfering and/or there is something horribly wrong with your WoW installation; try deleting your Cache folder.

Some other general tips for addon development, or even troubleshooting in general:

- Make sure you have an error display running. I'm biased toward Bugger since I wrote it, but BugSack or Swatter will work too.

- When trying to diagnose problems, disable any other addons you're normally running. This will quickly rule out (or confirm) that the problem is caused by interference with another addon. You can use an addon like ACP to save sets of enabled addons, so you don't have to manually go back through and check all the ones you want to re-enable. I have several sets like this -- one "minimal" that only enables the addon manager and error display for use when debugging problems, "light" that only enables the bare minimum addons for solo play during addon development, and "standard" that enables all the addons I want during normal play.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.

Last edited by Phanx : 08-11-16 at 07:15 PM.
  Reply With Quote
08-11-16, 09:06 PM   #5
candrid
Premium Member
 
candrid's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 61
You guys are all fantastic.

The code I mentioned is all that is inside a Lua file.
So, if you were to open the file you would only see the code I posted.

I will definitely try all the things you guys suggest and get back to you. Thank you very much!
  Reply With Quote
08-11-16, 10:12 PM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,878
You also need a file with the same name as the folder your .lua file is and with an extension of .toc.

You will need to add the name of your .lua file to the .toc file.

See the .toc file for some addons for other entries for this file like:

## Interface: 70000
## Title: FauxMazzle
## Author: Fizzlemizz
## Version: 7.0.2

YourLuaFile.lua -- your code file



## Interface: = the version of the game for identifying which game version an addon was last updated for (70000 being Legion)

## Version: = version # of your addon

## Author: = you

## Title: = Name that will show in the Addons list

etc.

If the print statement is the only thing in your .lua file then it will be the first thing printed and may scroll off the top of your chat text list after login (depending on what other addons you have installed). Galaxy119 and Phanx showed how to get it to print in a more expected manner.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 08-12-16 at 01:07 AM.
  Reply With Quote
08-11-16, 10:14 PM   #7
Joker119
A Flamescale Wyrmkin
 
Joker119's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2014
Posts: 113
Originally Posted by Phanx View Post
The orginal code should work fine. This is not accurate:



print can be run anywhere, at any time. In fact, no function can only be called from inside another function; that's just not how Lua works, and wouldn't really make much sense -- if functions could only be called from inside other functions, how would you call those outer functions to make anything happen in the first place?

However, if you're using an addon that makes the chat frame keep more messages in its scroll-back history, then anything printed before that addon loads will be erased. You can (usually, depending on the addon) work around this by waiting until the PLAYER_LOGIN event to print your message.

Other than that, we need more context in order to figure out your problem. If you're using print in an addon, show the rest of the code.

If you're typing /run print("Hello world") the chat box and it's not working, either some other addon is interfering and/or there is something horribly wrong with your WoW installation; try deleting your Cache folder.

Some other general tips for addon development, or even troubleshooting in general:

- Make sure you have an error display running. I'm biased toward Bugger since I wrote it, but BugSack or Swatter will work too.

- When trying to diagnose problems, disable any other addons you're normally running. This will quickly rule out (or confirm) that the problem is caused by interference with another addon. You can use an addon like ACP to save sets of enabled addons, so you don't have to manually go back through and check all the ones you want to re-enable. I have several sets like this -- one "minimal" that only enables the addon manager and error display for use when debugging problems, "light" that only enables the bare minimum addons for solo play during addon development, and "standard" that enables all the addons I want during normal play.

You're right.
I'm sorry I'm mixing code languages again, in BASH print isn't a function the way it is in Lua, and has to be called. My mistake.
  Reply With Quote
08-12-16, 03:51 AM   #8
candrid
Premium Member
 
candrid's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 61
Hi again!

So I ran /run print("Hello world") and it worked.
However, when I use the Lua with only print("Hello world") in it, it does nothing. That is with no other addons installed and no cache folder.

I will reinstall WoW and try again.
  Reply With Quote
08-12-16, 05:21 AM   #9
candrid
Premium Member
 
candrid's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 61
Reinstalling WoW fixed it!

You guys are the best!
  Reply With Quote
08-17-16, 12:28 PM   #10
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
That was a little overkill... o.O

It was also possible that your code was being run before the chat frame was finished being created/initialized.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » print not working


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off