Page 1 of 1
goto in Usecode C?
Posted: Tue Jan 03, 2017 11:07 pm
by Knight Captain
Is there a way to do a goto in Usecode C? For example I'm playing with the Copy Protection function, and it loops back to the top until the number of questions answered correctly is above 3. But there's no equivalent in Usecode C?
Re: goto in Usecode C?
Posted: Wed Jan 04, 2017 6:59 pm
by marzo
You can use while or do...while for this purpose; it is better than using a goto, and you can almost always restructure the code to use one of them instead (in FoV, I think only LB's usecode needs a slight twist to rewrite without a goto).
UCC does support goto statements and labels; but here is
something to keep in mind about goto.
Re: goto in Usecode C?
Posted: Wed Jan 04, 2017 8:00 pm
by Knight Captain
Thank you Marzo. Is there a good example in any of the official mods I can look at?
Re: goto in Usecode C?
Posted: Thu Jan 05, 2017 1:59 pm
by marzo
None of them use it; but it is like ucxt output:
Code: Select all
loop_start:
//code
if (stuff) {
goto loop_break;
}
// more code
goto loop_start;
loop_break:
Which can be done as:
Code: Select all
while (true) {
//code
if (stuff) {
break;
}
// more code
}
Really, there is nothing to be gained by using goto. And using it is a guaranteed reject on any pull requests, in case you are wondering.
Re: goto in Usecode C?
Posted: Thu Jan 05, 2017 4:05 pm
by Knight Captain
I'd much rather use while or do just out of avoiding bad habits. That's what I was hoping for an example on. Looks like it is used in the Keyring mod but not SIfixes.
Re: goto in Usecode C?
Posted: Fri Jan 06, 2017 11:09 am
by marzo
Here is a bit more information on while and do...while, then:
Code: Select all
do {
// code
if (condition1) {
break; // Exits the loop
} else if (condition2) {
continue; // Goes back to the 'do'
}
// more code
} while (condition3);
while (condition1) {
// code
if (condition2) {
break; // Exits the loop
} else if (condition3) {
continue; // Goes back to the 'while'
}
// more code
}
'continue' and 'break' are also supported inside 'for', 'converse'; 'break' is also supported in 'switch' statements.
Re: goto in Usecode C?
Posted: Wed Jan 11, 2017 6:06 am
by Knight Captain
I'm stuck on fixing Pomdirgun's code, where he has an event == DEATH but it loops back to STARTED_TALKING if you haven't talked to him yet. When I try to do this as close to the original code as possible, killing him freezes the Exult engine. I want to see flag 0xCC for his death, but it's not working. I'd like to be able to run his conversation if it doesn't occur prior to his DEATH.
Otherwise I have him fully reimplemented.
Re: goto in Usecode C?
Posted: Wed Jan 11, 2017 6:17 am
by Knight Captain
I think I figured this out, I was trying to "do it right" by simplifying it but the original code works better.
Re: goto in Usecode C?
Posted: Wed Jan 11, 2017 7:11 pm
by Knight Captain
Missing quotes on a set_npc_name string got me. Added this feature request:
https://sourceforge.net/p/exult/feature-requests/187/
Re: goto in Usecode C?
Posted: Wed Jan 11, 2017 9:18 pm
by marzo
Are you sure you don't just have a variable or function with that name? UCC simply does not work like that, it would try to find a variable or function with that name (spitting out an error if there was neither) and Exult would then try to interpret what was given as a string, probably with bad results.
Re: goto in Usecode C?
Posted: Thu Jan 12, 2017 9:35 pm
by Knight Captain
You're right as usual. The name I was setting was the same as used in the function name. Would it be best practice to use funcName instead of just Name? Or npcName?
Re: goto in Usecode C?
Posted: Tue Jan 31, 2017 4:24 am
by Knight Captain
As I've been hacking my mod, I've been using these conventions:
shapeName
npcName
spellName
eggName
cutsceneName (these are sometimes in shape code but usually egg code)
The do / continue / break is handy for converting the antechamber ambush code, for example. It uses a loop between x-left and x-right to place each energy field.
The game start rabbit code also uses a loop to clear some NPC flags. That's why setting some flags in ES for SI+SS won't stay through a game start.