Page 1 of 1

Question about LUA coding and C/C++?

Posted: Thu Feb 06, 2014 6:39 pm
by Zasisem
OK so I know in LUA you can do the following:

Code: Select all

bg={x=161,y=52,w=174,h=170} //background variables
Would that be alright to do in C/C++ as well? or will it receive an error?

edit: removed some code

Re: Question about LUA coding and C/C++?

Posted: Thu Feb 06, 2014 6:47 pm
by codestation
Moved to PC->Programming since the question doesn't have anything Vita specific.

Answering your question: Lua and C or C++ are totally different languages so don't try to use the syntax of one in another (of course you are gonna get an error). Besides that, C nor C++ are interpreted languages so you have to declare a data structure to hold the data, then instantiate it and finally setting the data to their fields.

Re: Question about LUA coding and C/C++?

Posted: Thu Feb 06, 2014 7:01 pm
by GBOT

Code: Select all

typedef struct
{
u16 x, y, w, h;
} t_bg;

//..

t_bg my_background = {161, 52, 174, 170};

for example.

Re: Question about LUA coding and C/C++?

Posted: Fri Feb 07, 2014 8:43 am
by Acid_Snake
GBOT wrote:

Code: Select all

typedef struct
{
u16 x, y, w, h;
} t_bg;

//..

t_bg my_background = {161, 52, 174, 170};

for example.
too complicated for his limited knowledge, a simple array of u16 would suffice

Code: Select all

u16 my_background[] = {161, 52, 174, 170};

Re: Question about LUA coding and C/C++?

Posted: Fri Feb 21, 2014 1:24 am
by Infinite Chalupas
Or closer to the actual example:

Code: Select all

struct { int x, y, w, h } bg = { .x = 161, .y = 152, .w = 174, .h = 170 };
A typedef is not needed unless you are wanting to reuse the structure for other purposes. Structures are declared like:

Code: Select all

struct StructType
{
  StructFields
} StructObject.
Both the StructType and StructObject parts are optional. A struct without a StructType is called an anonymous structure. If a struct declaration does not have StructObject part, you can create an instance of StructType like:

Code: Select all

struct StructType myNewStructObject;
In the early days of C, this was the only way to declare structures, but people didn't like it because it was too verbose. So the typedef keyword was introduced which allowed you to do:

Code: Select all

typedef struct StructType TypeName;

TypeName myNewStructObject;
;

It can almost be thought of as a backwards #define macro. However, people decided to be lazy and combine the two, hence the

Code: Select all

typedef struct StructType
{
  StructFields
} TypeName;
where TypeName would tend to have an _t suffix, though purely optional. This usage became so popular to the point where it was the only thing taught by books and people believed it to be the only way of declaring a struct. Believe it or not, I have actually had people that tried to tell me that what I was doing, which is the original way it was done, is wrong, despite the exact opposite being the truth! Out of habbit, I declare my typedefs completely separate from my structs because it looks better with allman coding style, like:

Code: Select all

typedef struct Rect Rect;

struct Screen
{
  Rect rect;
  unsigned color;
};

struct Rect
{
  int x, y, w, h;
};