Jump to content

Photo

The NJF Script Learning Topic


  • Please log in to reply
92 replies to this topic

#16 Russ

Russ

    Caelan, the Encouraging

  • Administrators
  • Location:Washington

Posted 30 March 2014 - 10:32 PM

Yup, you sure did. A simple script that full heals your health and magic when you enter the screen. See, scripting's not that hard when you start simple and work your way up from there.



#17 Mero

Mero

    Touch Fluffy Tail

  • Banned
  • Real Name:Tamamo No Mae
  • Location:Rainbow Factory

Posted 30 March 2014 - 11:29 PM

read zscript.txt std.txt and eventually string.txt. Go through saffith's tutorials. Slowly and don't advance until you have a firm understanding of the previous topics. Also bit mask are fun. :tard:



#18 TheLegend_njf

TheLegend_njf

    Deified

  • Members
  • Real Name:Grant

Posted 31 March 2014 - 07:46 AM

In response to Russ's answer that "waitframe is a function", I am curious, what function does it serve?

#19 Moosh

Moosh

    Tiny Little Questmaker

  • ZC Developers

Posted 31 March 2014 - 08:06 AM

Waitframe() waits for 1 frame or 1/60th of a second. You typically put it at the end of a loop or any place you want a pause in the script. There's also Waitframes() which does have an argument and waits for multiple frames.

 

If you have an infinite loop without a Waitframe() anywhere in your script, it'll cause ZC to freeze, so it's pretty important to remember.

 

Edit: I should also mention that frames are basically ZC's smallest measurement of time, so if you want something to always be true, like a script that keeps Link's HP at max, you set it every frame.


Edited by Moosh, 31 March 2014 - 08:10 AM.


#20 TheLegend_njf

TheLegend_njf

    Deified

  • Members
  • Real Name:Grant

Posted 31 March 2014 - 08:19 AM

So it's very necessary, but outside of preventing ZC from freezing from infinite loops, it essentially does nothing that the player would see himself?

#21 David

David

    Fallen leaves... adorn my night.

  • Administrators
  • Real Name:David
  • Pronouns:He / Him

Posted 31 March 2014 - 08:27 AM

No, it doesn't do anything that the player would see.

#22 TheLegend_njf

TheLegend_njf

    Deified

  • Members
  • Real Name:Grant

Posted 31 March 2014 - 08:28 AM

I'm also taking it that as long as a while(true) exists in a script, waitframe() must also exist to keep ZC from freezing?

#23 David

David

    Fallen leaves... adorn my night.

  • Administrators
  • Real Name:David
  • Pronouns:He / Him

Posted 31 March 2014 - 08:30 AM

Correct. Like you said, otherwise the game will freeze forever.

Edited by DaviAwesome, 31 March 2014 - 08:31 AM.


#24 TheLegend_njf

TheLegend_njf

    Deified

  • Members
  • Real Name:Grant

Posted 31 March 2014 - 08:37 AM

Are there examples on what these loops would be useful for? I know they keep a script working. But what are some examples of scripts that need loops and scripts that don't?

#25 David

David

    Fallen leaves... adorn my night.

  • Administrators
  • Real Name:David
  • Pronouns:He / Him

Posted 31 March 2014 - 08:41 AM

Well, ghost.zh enemies would need a loop so that they keep doing their attack pattern. That's why they usually use a while(true) loop somewhere in the script.

Scripts that wouldn't need a loop would be a script that only happens once or one time per 'click'; an example would be an item script that restores HP and MP, kind of like the one you made.

I could be more descriptive but I am sort of limited as to how much I type when I'm using a phone, xD. Hope this helps, and keep asking away! :)

Edited by DaviAwesome, 31 March 2014 - 08:42 AM.


#26 TheLegend_njf

TheLegend_njf

    Deified

  • Members
  • Real Name:Grant

Posted 31 March 2014 - 08:46 AM

It helps me understand a lot, and I hope that it helps other people who doesn't know much either by following this topic.

I'm going to continue reading, and will use this topic to share thoughts and questions I may have. I thank everybody who is answering my questions, even though are are mostly added for reference (I don't want to intrude on other people's valuable time), but seeing as people have taken their time to answer these questions means a lot to me and encourages me to learn more.


---------

I've taken notice that void run() is needed to run a script (or at least I think), but why not just run()? What is the significance of void? And also, why is the parenthesis blank? Is it always blank?

Edited by NewJourneysFire, 31 March 2014 - 08:47 AM.


#27 David

David

    Fallen leaves... adorn my night.

  • Administrators
  • Real Name:David
  • Pronouns:He / Him

Posted 31 March 2014 - 08:54 AM

I'm always happy to help answer questions! :)

Hopefully this can help others as well with scripting if they have questions too! :D

#28 Saffith

Saffith

    IPv7 user

  • Members

Posted 31 March 2014 - 09:45 AM

A bit more on Waitframe, since that's pretty important.
Everything in ZC runs frame by frame, and only one thing updates at a time. Link is updated, then enemies, one by one, then weapons, one by one... When your script is running, it holds everything else up. When a Waitframe is reached, that tells ZC the script has done all it's going to do for the frame and it should move on to other things.
 

I've taken notice that void run() is needed to run a script (or at least I think), but why not just run()? What is the significance of void?

Every function needs a return type. If the function doesn't actually return any data, the return type is void.

And also, why is the parenthesis blank? Is it always blank?

No. If your script takes arguments, that's where they'll go.
void run(int D0, int D1, int D2) // etc.


#29 Mero

Mero

    Touch Fluffy Tail

  • Banned
  • Real Name:Tamamo No Mae
  • Location:Rainbow Factory

Posted 31 March 2014 - 10:31 AM

Even more about what Saffith said, let's say you want to incorporate something that happens that frame this is where Waitdraw comes in() this let's the code for link, enemies, weapons, and so on run up to drawing. Any changes to these will have effect. Also only run Waitdraw() once per frame, and it only functions in actives slot. Item scripts only ffcs and the active script run for 1 frame. If you have any questions feel free too ask them here. Also take a peak at my AMA thread, link is in my signature.



#30 David

David

    Fallen leaves... adorn my night.

  • Administrators
  • Real Name:David
  • Pronouns:He / Him

Posted 31 March 2014 - 11:03 AM

A note on the "void": A function that doesn't return any values, like Saffith explained, would use "void" and then the function name. For example:
void FunctionName() {     
     Link->X = 250;
     Link->Y = 250;
}

FunctionName();
This script starts with defining the function called FunctionName(). This script makes Link's X and Y position to equal 250. However, then you have to call the function as I did where I wrote "FunctionName();".

You can take this a step further by having the function have something called arguments. Here is an example:
void FunctionName(int XValue, int YValue) {
     Link->X = XValue;     
     Link->Y = YValue;
}

FunctionName(250, 250);
FunctionName(79, 79);
FunctionName(45, 183);
In this script, I'm passing the arguments XValue and YValue. Inside the function, I am setting Link's X position to XValue and Link's Y position to YValue. Then where I call the function, I can set any values I want (as long as they are in the screen of ZC) and Link will go there. In the first one, I set Link's X position to 250 and Link's Y position to 250. In the second one, I am setting Link's X position to 79 and Link's Y position to 79. In the last one, I am setting Link's X position to 45 and Link's Y position to 183. This is the advantage of having arguments; when you call the function you can put any values you want even if it is the same function.

But now you may ask: What do you do when you do have a function that returns a value and you can't use void? Well, let's see an example:
int FunctionName(int Number1, int Number2) { 
     return Number1 + Number 2;
}

FunctionName(1, 1); //This would return 2
FunctionName(2, 5); //This would return 7
As you can see in the script, instead of "void" it has "int". This means that the function returns an integer. As you can see, I passed in arguments and the function returns the addition of those two numbers. I added comments (the writing after the "//") to show you how it returns the number that it is supposed to.

I hope this helps you understand better how functions with "void" and without "void" work. :)

Edited by DaviAwesome, 31 March 2014 - 12:18 PM.



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users