Can I use typedef struct in global code or not?

Non-platform specific questions.

Can I use typedef struct in global code or not?

Postby chicoscience » Sat Mar 24, 2012 3:54 pm

This works

Code: Select all
//Global code
int romesoldiers=100;
int romegold=1000;


Then I created a text actor with a draw event and script action. The script line is this:

Code: Select all
sprintf(text, "Sparta Kingdom\n--------\nSoldiers:%i\nGold:%i", romesoldiers, romegold);


And it worked like a charm. The text actor is able to get the values from the variables and display it.

Since this would give me so much headaches in the future, I started using struct.

changed to this

Code: Select all
//global code
typedef struct
{
 int soldiers;
 int gold;
} KINGDOM;

KINGDOM rome;

rome.soldiers=100;
rome.gold=1000;


And change the sprintf line in the actor to this:

Code: Select all
sprintf(text, "Sparta Kingdom\n--------\nSoldiers:%i\nGold:%i", rome.soldiers, rome.gold);


No errors but all values show zero when I run the game. Is there an error in the code?

Then I do some research and see there's an image in the website showing the global code with struct code inside it. (Sorry had to type it instead of pasting the link because "The maximum number of URLs allowed is 2."

And this text: http://game-editor.com/docs/scripting.htm
Use the Global Code Editor to freely add any 'C' code (arrays, structs, and functions). The sky is the limit when you use this Editor but you should have your feet well grounded in the C Programming Language in order to use the Global Editor effectively. Your knowledge of the 'C' language will serve you well here.
Global Code can be accessed by any script.


Which made me very happy, still can't get the simple text actor to access the struct.

I also found this http://game-editor.com/Using_Global_Code
You cannot use global code to declare complex global objects; If you try to use Struct or Actor* you will encounter problems. Game Editor handles all the interaction, listing, and running of complex Objects. It is not designed for you to add your own in this manner. Use the 'Add Actor' button instead.

You may use Actor* as a local variable for use in a script, if you like.


Ok now I'm confused. Can I use struct in global code for global acessed data or not? Is my code wrong? Am I addressing this issue wrongly?
User avatar
chicoscience
 
Posts: 19
Joined: Fri Mar 23, 2012 10:49 pm
Score: 1 Give a positive score

Re: Can I use typedef struct in global code or not?

Postby Game A Gogo » Sat Mar 24, 2012 4:57 pm

You cannot use global code to declare complex global objects; If you try to use Struct or Actor* you will encounter problems.


What this means is you can't set struct variables inside the global script. (Unless of course you set it with a function that gets called outside the global code)

A way around this is instead of using the global script you can use the Create Actor event inside the view.

So just put your
Code: Select all
rome.soldiers=100;
rome.gold=1000;

from the global script inside a Create Actor event in the view and it will work!
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Can I use typedef struct in global code or not?

Postby chicoscience » Sat Mar 24, 2012 6:14 pm

Thanks for the answer Game a Gogo. Just before I came back here to read and post my solution I managed to do it in a very similar way.

I crated the actor in view, event create actor -> script editor and clicked the variables button. Created two variables called soldiers and gold as actor variables.

Then I set their values inside the actor script without the name of the actor:

Code: Select all
soldiers = 100;
gold = 200;



And I was able to access these variables in another actor using this:

Code: Select all
rome.soldiers=100;
rome.gold=200;


So I guess there's no difference on how I create the variables right?

EDIT: Game a Gogo I tried to do what you told and no success. Aparently It's really needed to declare actor variables using the variables button.
User avatar
chicoscience
 
Posts: 19
Joined: Fri Mar 23, 2012 10:49 pm
Score: 1 Give a positive score

Re: Can I use typedef struct in global code or not?

Postby Game A Gogo » Sat Mar 24, 2012 10:32 pm

That's really strange, it worked for me S: here, I'll send you a GED

in the global code I have this:
Code: Select all
//global code
typedef struct
{
int soldiers;
int gold;
} KINGDOM;

KINGDOM rome;


in the create actor event in view:
Code: Select all
rome.soldiers=100;
rome.gold=1000;


and in the text actor I put this:
Code: Select all
sprintf(text, "Sparta Kingdom\n--------\nSoldiers:%i\nGold:%i", rome.soldiers, rome.gold);

inside the draw actor event because they're most likely will change along the game.

Also if you use actor variables instead of struct for this case, for every actor those variables will be assigned and will take place in memory. It looks to me that you don't need those variables on all actors so that means you're allocating more memory than you really need. Though it's probably not a problem with today's computer, but it's always a good practice to use only what you need, so it's up to you to choose the way you want to store them.
Attachments
Struct demo.zip
(189.48 KiB) Downloaded 83 times
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Can I use typedef struct in global code or not?

Postby chicoscience » Sat Mar 24, 2012 11:34 pm

I misunderstood what you said in your early post. I thought there was no need of struct in global code. Yeah it really works!

Ok I don't know if I'll keep using struct or actor variables now. Struct is good because I can only type stuff to create the basic structure, instead of so many mouse clicks for actor variables. Also there's the mem stuff.

Thanks a lot!

Offtopic: About this point thing. I give you a point now for helping me? How dos it work? Can you paste me a link about this? I couldn't find anything.
Edit: Forget about it, found it. You got yourself a point.
User avatar
chicoscience
 
Posts: 19
Joined: Fri Mar 23, 2012 10:49 pm
Score: 1 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest