Jump to content

Weird pointer issue


Ending Credits
 Share

Recommended Posts

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?

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

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 :blink:

 

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.

Link to comment
Share on other sites

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();

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

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 :blink:

 

That would explain it.

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

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.

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

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...