Jump to content

Photo

What's the script (for part of a greater script)...?


  • Please log in to reply
13 replies to this topic

#1 kmack08

kmack08

    Newbie

  • Members

Posted 07 May 2020 - 07:23 PM

I want a script that essentially says if there are no longer any instances of combo X on green (x should be the combo number, NOT a flag or type), then do "Y."  In other words, once all of {this combo} have been removed from the screen, do something.

 

 

The "Y" part could be play a sound and spawn an item (which i'm PRETTY SURE I know how to do, although i'm not sure how to make it fall from ceiling via a script) OR it could be play a sound & set room's secret state (again, I think I know how to do this).

 

I want this to be versatile so that the combo could first appear as many times as I'd like on screen but it could be removed (or replaced) by any means, whether it be they are all covered by blocks...they are bushes that have all been slashed...etc.

 

I'm a bit of a beginner with scripts.  Im pretty sure that part of this script will include:

for (int index = 0; index < 176; index += 1) { 

 

 

Thoughts?



#2 Moosh

Moosh

    Tiny Little Questmaker

  • ZC Developers

Posted 07 May 2020 - 09:36 PM

int comboCount;
for(int index=0; index<176; ++index){
	if(Screen->ComboD[index]==comboID){
		++comboCount;
	}
}

So here comboID would be the number in the combo table to check for. ++index is the same as index++ or index+=1, but is the fastest of the three. After the loop, you can check when comboCount == 0 (or !comboCount) to find when no more instances of that combo remain onscreen. If this is all you need that loop for, you could actually put a break; after ++comboCount to save on iterations.

 

For spawning an item that falls from the ceiling it's pretty simple:

if(!Screen->State[ST_ITEM]){
	Game->PlaySound(itemSFX);
	item itm = CreateItemAt(itemID, this->X, this->Y);
	itm->Z = 176;
	itm->Pickup = IP_ST_ITEM|IP_HOLDUP;
}

You first check to make sure the screen's item hasn't already been collected, then play the sound, spawn it in, and put it in the air. Items automatically obey gravity. itm->Pickup controls what happens when the item is picked up and you give it IP_ constants from std_constants.zh connected together with the | symbol.  IP_ST_ITEM makes it set the item collected screen state and IP_HOLDUP makes Link hold it above his head.


Edited by Moosh, 07 May 2020 - 09:36 PM.


#3 kmack08

kmack08

    Newbie

  • Members

Posted 07 May 2020 - 11:13 PM

ok, one thing i'm confused on with for loops...doesn't it run the loop a finite amount of times, that is 176 times in this instant, checking all 176 combo locations on this screen for this given type...and then when it's done, it stops?  Or does it repeatedly check?  i'm sensing it does NOT because the index would stop once it reaches 176 unless there's something in the script to revert it back to 0. No? 



#4 Russ

Russ

    Caelan, the Encouraging

  • Administrators
  • Location:Washington

Posted 07 May 2020 - 11:51 PM

The trick is that that for loop would be inside a while loop. If you want an ffc script to linger on the screen, doing stuff every frame, while loops are the way to go.

 

ffc script GenericScript{
    void run(){
        while(true){
            //Do something here
            Waitframe();
        }
    }
}

 

So in this case, that for loop would be inside the while loop of the above script template. That way, every frame, it would run through the for loop.



#5 Jamian

Jamian

    ZC enthusiast

  • Members

Posted 08 May 2020 - 03:25 AM

int comboCount;
for(int index=0; index<176; ++index){
	if(Screen->ComboD[index]==comboID){
		++comboCount;
	}
}

So here comboID would be the number in the combo table to check for. ++index is the same as index++ or index+=1, but is the fastest of the three. 

 

 

Just saying for those who don't know: in this instance, ++index is the same as index++, but this stops being true if the increment is part of an evaluation. ++i increments the number before the expression is evaluated, and i++ increments it after the expression is evaluated.



#6 Soga

Soga

    Secretly Alive

  • Members

Posted 08 May 2020 - 02:01 PM

Just saying for those who don't know: in this instance, ++index is the same as index++, but this stops being true if the increment is part of an evaluation. ++i increments the number before the expression is evaluated, and i++ increments it after the expression is evaluated.

 

Fun fact: prefix is computationally faster than postfix. Prefix is "fetch it, increment it, return it." Postfix is "fetch it, make a copy, increment the original, and return the copy."

Unfortunately, prefix notation is a bit troublesome when it comes to doing it on member variables, e.g.:

++object->member;

According to C++ operator precedence rules, this should work exactly as expected: increment object->member and return that. But the notation looks odd; the position of the ++ seems to imply that it is the object pointer being incremented. You could solve this with:

++(object->member);

The parentheses are unnecessary, but if you're really stuck on using prefix notation, I find that the parentheses communicate clearer meaning about what the code should be doing here. This is actually how I recommend doing it in ZScript, because I'm not sure that the ZScript compiler optimizes postfix increments/decrements like other language compliers do.



#7 Emily

Emily

    Scripter / Dev

  • ZC Developers

Posted 08 May 2020 - 05:40 PM

ZScript does not, in fact, automatically optimize if you use postfix when prefix is better; that's on you to optimize. Since it being better is context-sensitive, that would require a scanning pass that checks for context, and somehow changes that; which is a bit beyond anything set up in the parser.

 

Of note, '++object->member' should be easily readable, considering 'int->' is invalid, and '++' only works on 'int' type variables. If you tried '++npc', you'd get a type cast compile error- '++npc->X' would not, as it does in fact increment the X value.



#8 kmack08

kmack08

    Newbie

  • Members

Posted 09 May 2020 - 10:59 PM

Ok, now you guys really lost me  :unsure:



#9 James24

James24

    Adept

  • Banned
  • Real Name:James
  • Location:Australia

Posted 09 May 2020 - 11:48 PM

Yeah that's a common theme amougst people who are learning scripting.  Bottom line is that to learn how to script is going to to take a very long time and its not going to be easy.  Are you sure that you want to commit to that kind of investment?  Perhaps you should consider not scripting and saving yourself the time and hassle of learning and performing it.  Obviously the quest won't be as good, but it'll be a lot cheaper for you to make.



#10 Emily

Emily

    Scripter / Dev

  • ZC Developers

Posted 10 May 2020 - 12:26 AM

Ah, hello again Mr. Doom And Gloom 'Maybe you should just not do something because it's difficult'. 

 

@kmack word of advice, never listen to James24.

 

Anyway, Russ's answer is what you're looking for. Most scripts have a while loop in them that is 'while(true)'. That means that the loop NEVER stops running; it runs forever. Generally at the very bottom of the loop, you put 'Waitframe()'. By having an infinite loop that ends with 'Waitframe()', everything inside it will run once per frame. If you want something to run every frame, that's how you do it. Then, you'd do something like Moosh suggested to check every combo; INSIDE the 'while(true)' loop, so that it runs every frame.



#11 Evan20000

Evan20000

    P͏҉ę͟w͜� ̢͝!

  • Members
  • Real Name:B̵̴̡̕a҉̵̷ņ̢͘͢͜n̷̷ę́͢d̢̨͟͞
  • Location:B̕҉̶͘͝a̶̵҉͝ǹ̵̛͘n̵e̸͜͜͢d҉̶

Posted 10 May 2020 - 05:43 PM

Learning to script is like making food. Do so at your own pace, follow good practices and have a mentor, but too many chefs in the kitchen will spoil the dish.


  • coolgamer012345 likes this

#12 James24

James24

    Adept

  • Banned
  • Real Name:James
  • Location:Australia

Posted 10 May 2020 - 11:48 PM

Venrob I'm simply stating the reality of the situation to kmack08 so he knows what he's getting into.  The reality that few will dispute is that scripting is a difficult thing to learn and it will take a very long time.  Even when someone has learnt it its not something that's easy to pull off.  Given this situation with scripting, one has to consider the cost of learning and preforming it vs the benefits it will provide.  If the costs are too great and the benefits are too few then its clearly not something that should be attempted.



#13 coolgamer012345

coolgamer012345

    🔸

  • Members
  • Location:Indiana, USA

Posted 11 May 2020 - 12:39 AM

But learning to script teaches you to program, which is a useful general skill. The benefits are more than what meets the eye!



#14 Moosh

Moosh

    Tiny Little Questmaker

  • ZC Developers

Posted 11 May 2020 - 12:42 AM

Ok, now you guys really lost me  :unsure:

I wouldn't worry about the finer details too much. I just tried to bring up optimizations in my post because they're good habits to learn. But then things got a bit off topic with the usual programmer speak. Sorry about that.

 

Between the two posts actually addressing your questions by Russ and myself, is there anything still confusing you?


  • Russ and Aevin like this


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users