I am creating a social app and when the users login I want to show their username . How can I do this ?
Really nowhere near enough info in your question.
Assuming you’ve logged the user in and can get their user name from whatever social media provider you mean…
https://docs.coronalabs.com/api/library/display/newText.html
-- Assuming you put the user's name in the variable userName display.newText( userName, display.contentCenterX, display.contentCenterY, native.systemFont, 20 )
I’m getting this error :
local username = display.newText( username, display.contentCenterX, display.contentCenterY, native.systemFont, 20 ) screenGroup:insert(username) bad argument #1 to 'newText' (string expected, got nil)
Let me ask you to answer some basic questions. (Please answer them all)
-
What debug steps did you take on your own before asking your follow-up question above?
-
What is argument #1? That is, when you read that error message, what do you think argument #1 is refefring to?
-
What did you put in the variable ‘userName’?
-
Did you verify that there is something in ‘userName’?
- When I first got the error , I thought I was missing this ‘screenGroup:insert(username)’ so when I put that line I still got the error . I checked over and over that line of code and nothing changed so I posted here .
- What I think argument #1 is referring to is possibly a typo .
- This ‘local username’ is at the top of my code
- I don’t think so
The Error Message
The error message is referring to this code:
display.newText( username, display.contentCenterX, display.contentCenterY, native.systemFont, 20 )
Not any other line of code in that file. In fact, I’m 99% sure you get a line number with the error message.
Argument #1 is the first argument passed to the function display.newText() i.e. The variable ‘userName’
That error message means …
“Hey! You were supposed to pass a string as argument #1 when calling diplay.newText(), but you didn’t. You passed nil instead.”
username versus userName
I noticed you used the spelling ‘username’ while I used the spelling ‘userName’. This is fine as long as your code is consistent. Remember, Corona is case sensitive.
username Variable and Object?
I noticed you passed a varaible called ‘username’ to display.newText() and you created a new variable to store the display object in, called ‘username’. This is just plain wrong…
Debugging This
First, you need to know how to read error messages. Please see ‘The Error Message’ above.
Second, once you realize the the issue is Corona complaining about the variable ‘username’ you would debug this as follows:
print("my user name is", username) display.newText( username, display.contentCenterX, display.contentCenterY, native.systemFont, 20 )
Now, run it and look at the console. If it prints
my username is nil
Then the variable ‘username’ has nothing in it. That is wrong.
Try This Last
I MEAN IT! DO THIS LAST! Read everything above first or you’re wasting my time.
local userName = "comeUp264" local obj = display.newText userName, display.contentCenterX, display.contentCenterY, native.systemFont, 20)
I tried :
print("my user name is", username) display.newText( username, display.contentCenterX, display.contentCenterY, native.systemFont, 20 )
and got the same error . I tried this :
local userName = "comeUp264" local obj = display.newText( userName, display.contentCenterX, display.contentCenterY, native.systemFont, 20 )
and got this error :
error loading module 'profile' from file 'C:\Users\user\Documents\Corona Projects\app\profile.lua': C:\Users\user\Documents\Corona Projects\app\profile.lua:29: unexpected symbol near '.' stack traceback: [C]: in function 'error' ?: in function 'gotoScene' C:\Users\user\Documents\Corona Projects\app\newsfeed.lua:42: in function '\_onPress' ?: in function '\_setSelected' ?: in function '?' ?: in function \<?:182\>
That is a problem with your code.
Put my example in an empty main.lua file and it will work fine.
Other issues with your app are out of the scope of this discussion.
I didn’t expect your error to go away. What did you get in the console? Did you get the output I mentioned about a nil.
I can’t help more on this.
why should I empty main.lua ?
@Ed I have stopped helping this guy as he doesn’t listen or act on advice. I suggest you do the same mate.
600+ posts and he can’t lookup the correct syntax for a newText ?
actually I do listen
@sgs - Tsk tsk. Public beatings are not required. I understand the frustration, but I come here to help, so I’ll continue to try.
PS - I don’t measure folks by the number of posts they make, but their progress in those posts. comeUp264 is struggling. Part of that is on him, but part of that is on those of us who help.
It’s funny. Today, it is both easier and harder to make a game than when I first started learning about programming and game making.
- It is easier because you can do a lot and there is all kinds of information to learn from.
- It is harder because you can do a lot and there is all kinds of information to learn from.
I started programming back in the 70s. No internet, no real educational resources to call on, few books on the topic, and basically esoteric manuals. However, I only had to learn a small set of concepts and command and I was on my way.
My games were text based and black and white 2D.
Today’s users are faced with a massive slope to climb and the sense that their game must be huge and complicated. i.e. It is somehow not cool to make a text game or a simple two-color 2D game.
I feel bad for and envy today’s newbies at the same time.
@comeUp264
First, I really want to help and see you succeed. The more successful users there are here the better.
Second, I think there are a few issues here:
-
It does in fact often seem you’re not listening to what we (those who help) are telling you, BUT… I think the real problem is we are not clearly communicating. More on this below.
-
I get the distinct feeling you are new to programming, but you’re trying to make things that would give old an experienced developer pause. i.e. You’re trying to make apps and games that are beyond your current skill set and experience. So, it is going to be really hard to succeed.
-
You demonstrate inexperience with Corona itself. Specifically, I can tell you haven’t done lots and lots of small projects to explore the features and that you have not learned yet how to read error messages and debug them.
The only advice I can give you here is that you make lots and lots of micro projects, tests, and mechanics explorations. Just work in in main.lua to start. Don’t make multi file projects till later. When you’ve got your chops, then move on to modules, and much after that move on to using composer.
On Communication
I honestly think that when folks give you an answer, you may read it, but you get something completely different out of it.
I think you effectively latch onto the parts that make sense to you and that you are interested in while the rest drops by the wayside.
This sounds terrible, but it is natural and what typically happens when the two parties communicating are at different levels.
I don’t know a lot about you, so I may assume you know things you do not not.
There may be a deeper problem however. I sense a certain lack of curiosity and the absence of due diligence.
Why do I say this?
Well, I often answer questions with a few paragraphs discussing the issue and my answer. Then I provide some generic code to go with said discussion.
My expectation is that you should read everything I say, and then look at the code.
I don’t expect you to paste it verbatim into your app and then just run it. Unfortunately, you often do just that.
It is clear you are doing this because I will post a response and minutes later you will post back with a response:
- it didn’t work
- I tried that and got this error
- Some other short answer
This is both insulting and frustrating.
It is insulting, because I put real effort into my answers and take the time to make them legible and clear. However, you don’t reciprocate on the effort side. i.e. The effort and energy that goes into these discussions seems very one-sided.
Final Suggestion
It might be a good idea for you to put your current project on hold and return to basics for a bit.
Like I said above, make short examples and explore basic concepts. Learn to debug small projects first. Then consider something bigger.
Anyways, best of luck to you in the future.
@comeUp264, @roaminggamer wrote the best possible answer in the case scenario. I won’t be of much help, because I am a newbie too. But, @roaminggamer is definitely right, start with smaller projects first. I am struggling with my game, because I jumped on it too quickly. All I can say is this. Weeks before I even came up with the idea for my game, I worked on a little project. I wanted to add a text box that allowed a user to type in a number, and it would set it as the x-coordinate of a crate, each crate would fall to the ground with a number (its position in the table), and long story short, the knowledge I achieved about tables was invaluable and helped me a lot when starting on the basics of my new project.
Also, I have to agree with @roaminggamer here on a second topic. It seems like you have been somewhat ignoring the explanations, I am not accusing you of doing this on purpose. But, the first times I would write questions on the Corona Forums, I almost never bothered to read explanations, I would always seek out the code box in people’s responses. (to be honest, I still kind of do)
Good luck on your project. I hope the posts here inspired you.
PS: @roaminggamer, I hope one of the two-color games you made included green, (the physical manifestation of envy toward newbies
I noticed here you set the text object as username, and put the text object’s text as username too. This does not make sense, you should try this: (Note: @roaminggamer did mention this, if you did not read it, look at the 6th post in this thread.
local username = display.newText(userName, etc.) --notice the capital 'N' --Do not copy and paste directly, you still need to fill in the display.newText parameters
Also, if the value is not predefined as a string, you should use the ‘tostring’ function:
local userName = tostring(?) local userNameText = display.newText(userName, ...) --changed text variable name to avoid confusion
Hope this helps.
@Ed if I was beating then my response would of been completely different. I have no time for people taking up my time and not listening. I’m done with people that ask for help but are 1) not willing to accept and 2) don’t take the time to work through the help we have give and apply it to their work.
We don’t exist to write stuff for them and that is something they really need to understand! I am sure @sdktester15 knows my frustrations.
Lol! Sorry, for the recent flurry of questions!
Nah you are cool dude as long as you learn and grow (and accept your limitations)
You simply cannot think “oh will make Clash of Clans”
why do I have to use a capital N in userName ?