Ending Credits Posted September 21, 2010 Share Posted September 21, 2010 I have a line in code that goes: UTNCharacter * newplayer = world.CreatePlayer(); world.player = &newplayer->controller; (Where world.CreatePlayer(); returns a pointer to the UTNCharacter type.) However, when I use the following code: UTNCharacter newplayer = world.CreatePlayer(); world.player = &newplayer.controller; (Where world.CreatePlayer(); returns a UTNCharacter object.) The world.player pointer seems to be made null and PositionEntity(*player,...) with the world class methods produces a memory access error. Am I doing something stupid? Quote AMD Phenom 9850 (X4 2.5GHz) | 1066MHz CL5 GEIL Black Dragon (800MHz) | Powercolor ATI 4870 1GB | DFI 790FXB-M2RSH | 24" 1920x1200 | Windows Vista 64-bit Ultimate Link to comment Share on other sites More sharing options...
Canardia Posted September 21, 2010 Share Posted September 21, 2010 Use STL map instead of pointers, since you will need multiple players also. Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Laurens Posted September 21, 2010 Share Posted September 21, 2010 I am far from an expert (learning C++ as I go along with Leadwerks) but I believe your first example creates an object on the heap, whereas your second example creates an object on the stack. Characteristic of the stack is that when "newplayer" goes out of scope, it is no longer valid. Someone will probably come along to correct me Use STL map instead of pointers, since you will need multiple players also. The key in map will always be a pointer. You can't stuff references as a key in a map. Quote Link to comment Share on other sites More sharing options...
Canardia Posted September 21, 2010 Share Posted September 21, 2010 The key is just a static value, you can use a string or int (enum) as key. It could look like this: map<string,UTNCharacter> players; ... players["joe"]=world.CreatePlayer(); players["jane"]=world.CreatePlayer(); Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Ending Credits Posted September 21, 2010 Author Share Posted September 21, 2010 I am far from an expert (learning C++ as I go along with Leadwerks) but I believe your first example creates an object on the heap, whereas your second example creates an object on the stack. Characteristic of the stack is that when "newplayer" goes out of scope, it is no longer valid. Someone will probably come along to correct me That would explain it. Quote AMD Phenom 9850 (X4 2.5GHz) | 1066MHz CL5 GEIL Black Dragon (800MHz) | Powercolor ATI 4870 1GB | DFI 790FXB-M2RSH | 24" 1920x1200 | Windows Vista 64-bit Ultimate Link to comment Share on other sites More sharing options...
Ending Credits Posted September 21, 2010 Author Share Posted September 21, 2010 Use STL map instead of pointers, since you will need multiple players also. Actually, player is just a reference to the player's character so I can place the camera, all the rest will be done in methods (this is probably a really silly/weird way to do things). I'll have a look at maps though, they look quite usefull. Quote AMD Phenom 9850 (X4 2.5GHz) | 1066MHz CL5 GEIL Black Dragon (800MHz) | Powercolor ATI 4870 1GB | DFI 790FXB-M2RSH | 24" 1920x1200 | Windows Vista 64-bit Ultimate Link to comment Share on other sites More sharing options...
VeTaL Posted September 22, 2010 Share Posted September 22, 2010 I can recommend you Scott Meyers - Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library. Quote Working on LeaFAQ Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.