Jump to content

Photo

Advanced scripting tutorial (WIP)


  • Please log in to reply
44 replies to this topic

#16 lucas92

lucas92

    Defender

  • Members

Posted 21 January 2010 - 04:57 PM

I have 1 quick question:
Is this legal in zscript?

CODE
void draw_cube(int cube_x[8],int cube_y[8], int cube_z[8])
    {}


I haven't seen any script passing an array in the parameters....

#17 Joe123

Joe123

    Retired

  • Members

Posted 21 January 2010 - 05:06 PM

No it's not.
You can index any integer, so you can pass an array a bit like this:
CODE
{
int a[5];
passarray(a);
}
void passarray(int array){
    array[whatever] = blah;
}


But it's not guaranteed to work.

#18 lucas92

lucas92

    Defender

  • Members

Posted 21 January 2010 - 07:05 PM

... Are you sure this would work? I have the feeling that "a" won't take the the value of array at all after passarray. At least, in C it doesn't work that way. You would have to put a pointer in the function parameter to be able to modify it...

#19 Joe123

Joe123

    Retired

  • Members

Posted 21 January 2010 - 07:14 PM

Yes I'm very sure that you can do that, you might recall that I'm a developer and I have access to the source code. I'm well aware of how arrays work in C.
The way it works is that the array pointer is just an integer which is passed through an array via a (pretty horrible) linked-list algorithm to access the data. The only issue is whether the compiler would know if it's meant to be accessing global or script RAM, which is very unlikely.

#20 Banjo7J

Banjo7J

    Defender

  • Members
  • Real Name:John
  • Location:Scotland

Posted 23 January 2010 - 06:03 AM

OK, I just read most of this (I skipped the bitwise operators) and while it's kinda confusing, I get most of it. The thing I don't get is user-defined functions. How exactly does it work? Is it like, the first time you use it you write the function name and what it does in the brackets then next time you just write the name or what? I know it's barely if ever needed but it's still bugging me nonetheless.

#21 Joe123

Joe123

    Retired

  • Members

Posted 23 January 2010 - 06:10 AM

User defined functions definitely isn't barely ever needed, where did you get that idea from? =S
Look in the database for some examples of scripts with user-defined functions.

#22 Banjo7J

Banjo7J

    Defender

  • Members
  • Real Name:John
  • Location:Scotland

Posted 23 January 2010 - 06:22 AM

QUOTE(Saffith @ Mar 28 2009, 09:18 PM) View Post

While it's rarely if ever strictly necessary,

There. Although, then again, now that I've reread my post, I see that it was bad wording.

#23 Joe123

Joe123

    Retired

  • Members

Posted 23 January 2010 - 06:29 AM

Oh, right.
It's often that you don't need to use them, but if you don't scripts larger than a certain size become more and more difficult to read very quickly.

#24 Banjo7J

Banjo7J

    Defender

  • Members
  • Real Name:John
  • Location:Scotland

Posted 23 January 2010 - 06:44 AM

Right. It's not needed but it's very useful. And you still haven't actually answered my question or was
QUOTE
Look in the database for some examples of scripts with user-defined functions.
your answer?

#25 Joe123

Joe123

    Retired

  • Members

Posted 23 January 2010 - 06:49 AM

Yeah, that was my answer.
I'll give you another quick one here, then the best way to see how they work is to look at them in action really.
CODE
ffc script blah{
    void run(){ //this is a function declaration, run() must be in all scripts
    }
    void myfunction(int parameters, int moreparameters){//this is another function declaration, return type is 'void' and it has two parameters of type 'int'
        paramaters = 5; //whatever you want to do inside the function
    }//close the funciotn
    int myotherfunction(){//this is another function declaration, returning type 'int'
        myfunction(0,0); //this shows how you return a value, and how to use the previously declared 'myfunction'
        return 89;
    }
}


#26 Banjo7J

Banjo7J

    Defender

  • Members
  • Real Name:John
  • Location:Scotland

Posted 23 January 2010 - 07:07 AM

I think I get it but I'll look around in the database a bit.

EDIT:Ah, yeah, now I see. I looked in your's and AgentLym's Dungeon Entrance script.

Edited by Banjo7J, 23 January 2010 - 07:09 AM.


#27 Gleeok

Gleeok

    It's dangerous to dough alone, bake this.

  • Members
  • Real Name:Pillsbury
  • Location:Magical Land of Dough

Posted 23 January 2010 - 08:36 AM

QUOTE(Joe123 @ Jan 21 2010, 02:06 PM) View Post

No it's not.
You can index any integer, so you can pass an array a bit like this:
CODE
{
int a[5];
passarray(a);
}
void passarray(int array){
    array[whatever] = blah;
}


But it's not guaranteed to work.


You know if you could do that, and since every zscript variable is the same size, and if the array offset doesn't check the size but it still points at some data..you might be able to do something like this then:
CODE

int a[2] = { 1,2 };
int b = 3;
passA( a );

void passA( int array )
{
  if(array[2] == 3)
    //holy crap, it's b!
}


Of course it won't work, but it could work. :o


#28 Schwa

Schwa

    Enjoy the Moment more. This strengthens Imagination.

  • Members
  • Real Name:Hunter S.
  • Location:Redmond Subspace (I had a Potion)

Posted 23 January 2010 - 10:26 AM

As far as user-defined functions go, I use them all the time, but all mine are simply an "extension", so to speak, of Global Script Slot 2. It's almost no different than just defining them in the Global Script itself, except-- just as Joe said-- it's better organized for me, AND using this way makes it very easy to re-order the priority of each routine, in case I need certain code called before others or I change my mind about it later.

This has been Schwa, your guide to the world of awesome. Back to adventure!

#29 Joe123

Joe123

    Retired

  • Members

Posted 23 January 2010 - 12:41 PM

Well, arrays and normal variables are stored in different data structures, but you actually can do what you just said.
CODE
void ohdear(){
     int a[3] = {1,2,3};
     int b[2] = {4,5};
     Trace(a[3]);
}

I'm fairly certain that function would trace 4.

My re-implementation of arrays that's actually never going to get used 'cause it's a bit of a mess wouldn't allow that, but nevermind.


#30 Schwa

Schwa

    Enjoy the Moment more. This strengthens Imagination.

  • Members
  • Real Name:Hunter S.
  • Location:Redmond Subspace (I had a Potion)

Posted 23 January 2010 - 02:11 PM

I have a question on the subject of user-defined functions.

Would it be possible to assign a set of functions to an array, so a variable can index which one to use? Basically you could check a variable, and run one Function depending on that variable without having to do the If-Then-Else-If method, which I've always thought of as choppy and unrefined. I'd have no idea how to set something like that up, though... Maybe "{variable} = {function name};", like it would trick the variable into setting itself to whatever the function equates to in terms of numerical values (I hope I explained that right icon_heh.gif ).

It's not the end of the world if this can't be done, but if it can, I'll definitely use that over If-Then-Else-If. icon_king.gif


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users