Advertising (This ad goes away for registered users. You can Login or Register)

Question about LUA coding and C/C++?

Programming on your favorite platform, for your favorite platform? Post here
Locked
Zasisem
Posts: 200
Joined: Mon Sep 27, 2010 3:49 pm

Question about LUA coding and C/C++?

Post 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
Advertising
codestation
Big Beholder
Posts: 1660
Joined: Wed Jan 19, 2011 3:45 pm
Location: /dev/negi

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

Post 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.
Advertising
Plugin list
Working on: QPSNProxy, QCMA - Open source content manager for the PS Vita
Playing: Error: ENOTIME
Repositories: github, google code
Just feel the code..
GBOT
Developer
Posts: 321
Joined: Wed Apr 25, 2012 8:00 pm
Contact:

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

Post by GBOT »

Code: Select all

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

//..

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

for example.
Github
Twitter

Can't give enough crepes
Acid_Snake
Retired Mod
Posts: 3100
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

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

Post 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};
Infinite Chalupas
Posts: 92
Joined: Thu Feb 20, 2014 9:00 pm

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

Post 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;
};
Locked

Return to “Programming”