A quick question about my own functions/scripts

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
DarksideX
Posts: 22
Joined: Wed Dec 10, 2008 7:42 am

A quick question about my own functions/scripts

Post by DarksideX »

Hello.

How do i make my own script that i will use later on, and make it not complain about undeclared variables?

Example.
File Name : LoadLevel1.h

Code: Select all

void loadLevel1()
{
LevelGravity=0.6;
}
File Name : main.cpp

Code: Select all

#include "LoadLevel1.h"

int LevelGravity;
loadLevel1();

That is how i want to do it. Make a file where i load my script/function (dont now what its called here) and then call them from anywhere.
I dont want the program to take a look at the script/function UNTIL i use it, so it doesnt complain about undeclared variables.

So.. How do i do it?

Thanks.
-Ali
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Post by Ion Dune »

Try this:

Code: Select all

void loadLevel1( int & LevelGravity )
{
LevelGravity=0.6;
} 

Code: Select all

#include "LoadLevel1.h" 

int LevelGravity;
loadLevel1( LevelGravity ); 
Also, C++ is not a scripting language.
DarksideX
Posts: 22
Joined: Wed Dec 10, 2008 7:42 am

Post by DarksideX »

Thanks alot Ion Dune but what if i have a function that has alot of variables. Do i have to declare all of them between the parantheses?

Thanks again.

-Ali

Edit: Also, what does & stand for?
Dark_Kilauea
Posts: 368
Joined: Tue Aug 21, 2007 1:43 am
Location: The Middle of Nowhere

Post by Dark_Kilauea »

You really really really need to read on using C++. Go find a tutorial or book and get started.
rogerborg wrote:Every time someone learns to use a debugger, an angel gets their wings.
DarksideX
Posts: 22
Joined: Wed Dec 10, 2008 7:42 am

Post by DarksideX »

Ive found it easier for me to jump right in programming and then experiment with codes. I started programmign i GML "gamemaker language" and mastered the language in one month. I think experimentation is the best way. Maybe not for anyone, but i feel so for me :) I aldready got a halfstable engine running, started just a few days ago. Knowing 0 about c++.

But.. I know im doing wrong :P I just like to play around with codes. And if it gets too much then maybe grab a tut, or two. Or maybe drop a question here.

Thanks though, i really should listen to your advice and shut up XD

-Ali
DarksideX
Posts: 22
Joined: Wed Dec 10, 2008 7:42 am

Post by DarksideX »

Now i have another problem.

I have a code with the device inside a funtion and now its complaining about the device not being declared.

Is there no way to make it so that c++ ignores the script until i call it?

-Ali
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Show us your code and you should probably refrain from using the word script when talking about code as they're different things ;)
Image Image Image
geckoman
Posts: 143
Joined: Thu Nov 27, 2008 11:05 am
Location: Germany
Contact:

Post by geckoman »

Did you read what Ion Dune wrote? C++ is NOT NOT NOT a scripting language, so no scripts.
And if you do not declare some variable the compiler will not stop complaining, because your code is getting COMPILED and the compiler MUST NOT ignore your functions/methods whatever.

Btw. your GML is NOT compiled, thats the difference.

If you don't understand what I wrote, then it's one more sign that you should read a book or at least tutorial. C++ is not easy!!:!:

And please next time post some code, so everybody can see what exact problem you have, you will get more response.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

OK, C++ is a compiled language, not a script. This does matter, since the compiler does quite a lot of checking of your source as it's compiling it. If there's something wrong, then you want it to fail during compilation. Better that than to have it crash during execution.

What you are asking about is utterly fundamental C/C++. I suggest that you read about:

Variable scope.

Global vs local variables.

Variable declaration and definition.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Pie21
Posts: 13
Joined: Mon Oct 06, 2008 11:21 am
Location: Melbourne, Australia
Contact:

Post by Pie21 »

This is possibly the first post on this forum I feel I have any semblance of authority discussing :P

I'm kind of starting out with C++ too, but picked up C-Script a few years ago and have been using Python at Uni for all this year. I'm like you, in that I like to get out of depth to learn things (which Irrlicht has been a lot of fun for so far), but if there's one book I recommend, it's C++ For Dummies. I'm not a huge fan of the For Dummies books, as they seem a little hit and miss of how well they're written (since they're by different folks), but I found the C++ one absolutely perfect for me to learn it.

Just a suggestion. Covers everything from ints to polymorphism and templates. Does pointers and classes really well. Read through it once and THEN do the experimenting, and it'll save you buckets of tears.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

I agree to all other repliers... ;)
http://www.cplusplus.com/doc/tutorial

well, for your variable problem:
you can also declare all vars as globals and externs, so you don't need to pass any var around...

or you can pack the vars all together in one (or more) structs, so you just need to pass this struct to the function(s)...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
DarksideX
Posts: 22
Joined: Wed Dec 10, 2008 7:42 am

Post by DarksideX »

Thanks alot guys =D

I will take my time to read a tutorial, or this book and then continue with the experimentation.

I just wish i was not an employee at mcdonalds! XD
Takes so much time!

Thanks again

-Ali
Post Reply