Closed bcornw2 closed 4 years ago
Hi Benjamin,
The way to handle this is generally to pass the “_player” variable into the other classes and methods that need to use it. When you call a function in another object, that function will need to have a Player parameter to accept the _player object, and do whatever the function needs to do with that object.
Because the _player object’s datatype is “Player” (a class you created, and not a built-in class – like “string”, or “int”), the variable will be passed as a reference. This means there is really only one _player object in memory, each function will modify that one object. It’s not like a variable that is passed “by reference”. Those variables basically make a copy of the value, and do not point to one shared object.
Let me know if that doesn’t work, or if you still have questions. If you can’t get it to work, can you upload your code to GitHub, Dropbox, or some other file-sharing location? I’d need all the files in the folder where the .sln file is located, all the sub-folders underneath that location (those will probably be named FantasyRPG.Engine, FantasyRPG.Console, etc. – whatever projects you added to your solution), and files in those project sub-folders. That way I can build it and help you figure out a solution.
Scott
From: Benjamin Cornwell notifications@github.com Sent: Monday, January 6, 2020 10:03 PM To: ScottLilly/SuperAdventure SuperAdventure@noreply.github.com Cc: Subscribed subscribed@noreply.github.com Subject: [ScottLilly/SuperAdventure] Player can not be created or accessed outside of "program" class. (#1)
Hi Scott,
I know this is from a long time ago but I recently found your blog post about the adventure game and I am thoroughly enjoying it. Thanks for contributing to coding knowledge as a whole.
Anyway, I hope that if you still check this repo, you can help me get to the bottom of this issue. I followed your blog and re-created this program from scratch, in the steps that your blog outlined. I tried to add some additional features, such as character stats (Strength, Wisdom, Spell Power, etc.), but my issue is this:
Whenever I create the Player object for the player, it can only be seen in the class that created it. I tried to make everything static, call it from the Player class like how you did with the CreatDefaultPlayer() method, declare it globally (as globally as C# can get), and many other things. I'm sure its something simple. I just get a NullReferenceException no matter what I try. I do not think that the program is creating the player in a way that is reachable by the other classes. Any advice? Thanks again for making that blog, it's been really enjoyable and informative.
I just want to be able to create a Player object that can be worked on by the rest of the classes, and that player object has customizable stats.
P.S. - I've included both wrong ways - lines 94 and 97 - to show you what I've done so far. Both builds have 0 errors and 0 warnings. code:
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ScottLilly/SuperAdventure/issues/1?email_source=notifications&email_token=AARKPMQL6A63IX2LFRAUUVDQ4P5H7A5CNFSM4KDQYUZ2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IEMKACA , or unsubscribe https://github.com/notifications/unsubscribe-auth/AARKPMRLGDJL46V4IGD4TCDQ4P5H7ANCNFSM4KDQYUZQ . https://github.com/notifications/beacon/AARKPMV4ECNHGL3BDXZ5UALQ4P5H7A5CNFSM4KDQYUZ2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IEMKACA.gif
Hi Scott,
Thanks for the help. I really appreciate it. I've since added quite a bit of functionality, including stats like Strength and SpellPower, classes with different abilities like Sorcerer, Paladin, Huntsman, and class-specific spells like Smite and Heal for paladins, Fireball and frostbolt for sorcerers, and that ability to tame beast to act as a pet who attacks on your behalf (for hunters), and the ability to dual-wield daggers for rogues.
You can check it out here if you're interested. >> https://github.com/bcornw2/ScholomanceAdventure
Again, thanks for the help. You made a great blog post that enabled me to do this. It was a ton of fun! And thanks for responding to my issue too. For now, I think I've finished this project, besides adding some more rooms and some more dialog from bosses.
Thanks!
Hi Scott,
I know this is from a long time ago but I recently found your blog post about the adventure game and I am thoroughly enjoying it. Thanks for contributing to coding knowledge as a whole.
Anyway, I hope that if you still check this repo, you can help me get to the bottom of this issue. I followed your blog and re-created this program from scratch, in the steps that your blog outlined. I tried to add some additional features, such as character stats (Strength, Wisdom, Spell Power, etc.), but my issue is this:
Whenever I create the Player object for the player, it can only be seen in the class that created it. I tried to make everything static, call it from the Player class like how you did with the
CreatDefaultPlayer()
method, declare it globally (as globally as C# can get), and many other things. I'm sure its something simple. I just get aNullReferenceException
no matter what I try. I do not think that the program is creating the player in a way that is reachable by the other classes. Any advice? Thanks again for making that blog, it's been really enjoyable and informative.I just want to be able to create a Player object that can be worked on by the rest of the classes, and that player object has customizable stats.
P.S. - I've included both wrong ways - lines 94 and 97 - to show you what I've done so far. Both builds have 0 errors and 0 warnings. code:
`using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Security.Cryptography;
namespace FantasyRPG { class Program { public static Player _player;
} `