Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
1 registered members (TipmyPip), 18,631 guests, and 7 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Structs and pointers #182639
02/07/08 23:44
02/07/08 23:44
Joined: Feb 2008
Posts: 39
R
RicheyMB2 Offline OP
Newbie
RicheyMB2  Offline OP
Newbie
R

Joined: Feb 2008
Posts: 39
I have two structs and would like the TEXT in the first struct to show the value of a STRING in the second struct.

Code:

typedef struct {
TEXT* txtPlayerName;
} MPLAYDEBUGTEXT;

typedef struct {
STRING* strPlayerName;
var varPlayerID;
var varLoaded;
} MPLAYEROBJ;



I would like the TEXT in the MPLAYDEBUGTEXT struct to show the value of the STRING in the MPLAYEROBJ struct.

I can do that with something like this

Code:
  
(mplayerdebugtext.txtPlayerName.pstring)[0] = mplayerobj.strPlayerName



But then if I change mplayerobj.strPlayerName the change is not reflected in the text object. I've tried all kinds of *'s and &s but no luck so far.

Re: Structs and pointers [Re: RicheyMB2] #182640
02/08/08 00:58
02/08/08 00:58
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
When you change mplayerobj.strPlayerName, do you update mplayerdebugtext as well?


Click and join the 3dgs irc community!
Room: #3dgs
Re: Structs and pointers [Re: RicheyMB2] #182641
02/08/08 06:59
02/08/08 06:59
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline
Expert
Excessus  Offline
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
Quote:

When you change mplayerobj.strPlayerName, do you update mplayerdebugtext as well?



This is what he wants to happen "automatically" by using pointers.


I think your code should work..

Maybe your text isn't visible (VISIBLE flag off), or it's off the screen? Have you tried copying some random string in it?

EDIT: how are you changing mplayerobj.strPlayerName? It should be by str_cpy, not assignment (=).

Last edited by Excessus; 02/08/08 07:03.
Re: Structs and pointers [Re: Joozey] #182642
02/08/08 09:25
02/08/08 09:25
Joined: Jan 2008
Posts: 19
G
girish Offline
Newbie
girish  Offline
Newbie
G

Joined: Jan 2008
Posts: 19
use Str_cpy

use Str_cpy(mplayerdebugtext.txtPlayerName.pstring)[0] , layerobj.strPlayerName);

Re: Structs and pointers [Re: Excessus] #182643
02/08/08 10:56
02/08/08 10:56
Joined: Feb 2008
Posts: 39
R
RicheyMB2 Offline OP
Newbie
RicheyMB2  Offline OP
Newbie
R

Joined: Feb 2008
Posts: 39
Excessus is spot on, I don't want to have to update mplayerdebugtext every time mplayerobj.strPlayerName is changed.

The TEXT is visible. If I do the following
Code:

mplayerobj.strPlayerName = "TEST";
(mplayerdebugtext.txtPlayerName.pstring)[0] = mplayerobj.strPlayerName;

mplayerobj.strPlayerName = "PLAYER NAME";
Code:


The word TEST is displayed on the screen. If I was not using structs and just a 'traditional' STRING and TEXT combination I would not have to update the TEXT when the STRING changed.

Hope this helps anyone looking.

Re: Structs and pointers [Re: RicheyMB2] #182644
02/08/08 16:23
02/08/08 16:23
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline
Expert
Excessus  Offline
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
Yes, I see the problem.

Let's look at it line by line.

mplayerobj.strPlayerName = "TEST";
mplayerobj.strPlayerName is a STRING pointer. By writing "TEST", you are putting 5 chars somewhere in memory (T, E, S, T, 0). The assignment (=) makes your STRING* (mplayerobj.strPlayerName) point to that memory location. All good so far.

(mplayerdebugtext.txtPlayerName.pstring)[0] = mplayerobj.strPlayerName;
This line copies the memory address from mplayerobj.strPlayerName to
(mplayerdebugtext.txtPlayerName.pstring)[0]. Now both pointers point to the memory address where "TEST" is stored. Still good.

mplayerobj.strPlayerName = "PLAYER NAME";
This is the problem. "PLAYER NAME" is not stored at the same location as "TEST". Now you have mplayerobj.strPlayerName pointing to the memory address where "PLAYER NAME" is stored, and (mplayerdebugtext.txtPlayerName.pstring)[0] still pointing to the memory address where "TEST" is stored.

What you want to do, is not change the memory address strPlayerName points to, but change what is at that address. You can do this with str_cpy. Here is how I'd write this:

Code:

mplayerobj.strPlayerName = str_create("TEST"); // I do this so it is actually a STRING, and not a char[].
(mplayerdebugtext.txtPlayerName.pstring)[0] = mplayerobj.strPlayerName;

str_cpy(mplayerobj.strPlayerName, "PLAYER NAME");



A nice clip about pointers in C:
http://www.youtube.com/watch?v=6pmWojisM_E

Re: Structs and pointers [Re: Excessus] #182645
02/09/08 19:36
02/09/08 19:36
Joined: Feb 2008
Posts: 39
R
RicheyMB2 Offline OP
Newbie
RicheyMB2  Offline OP
Newbie
R

Joined: Feb 2008
Posts: 39
Yea! Works now. Thanks all for your help. Excessus, you (and Binky) explained it really well.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1