goto in Usecode C?

NOTICE: This forum is archived as read only.
Please use the Github Discussions at https://github.com/exult/exult/discussions
Forum rules
NOTICE: This forum is archived as read only.
Please use the Github Discussions at https://github.com/exult/exult/discussions
Locked
Knight Captain
Posts: 1219
Joined: Thu May 14, 2020 1:34 pm

goto in Usecode C?

Post 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?
marzo
Site Admin
Posts: 1925
Joined: Thu May 14, 2020 1:34 pm

Re: goto in Usecode C?

Post 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.
------
Marzo Sette Torres Junior
aka Geometrodynamic Dragon
[url=http://www.catb.org/~esr/faqs/smart-questions.html]How To Ask Questions The Smart Way[/url]
Knight Captain
Posts: 1219
Joined: Thu May 14, 2020 1:34 pm

Re: goto in Usecode C?

Post by Knight Captain »

Thank you Marzo. Is there a good example in any of the official mods I can look at?
marzo
Site Admin
Posts: 1925
Joined: Thu May 14, 2020 1:34 pm

Re: goto in Usecode C?

Post 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.
------
Marzo Sette Torres Junior
aka Geometrodynamic Dragon
[url=http://www.catb.org/~esr/faqs/smart-questions.html]How To Ask Questions The Smart Way[/url]
Knight Captain
Posts: 1219
Joined: Thu May 14, 2020 1:34 pm

Re: goto in Usecode C?

Post 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.
marzo
Site Admin
Posts: 1925
Joined: Thu May 14, 2020 1:34 pm

Re: goto in Usecode C?

Post 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.
------
Marzo Sette Torres Junior
aka Geometrodynamic Dragon
[url=http://www.catb.org/~esr/faqs/smart-questions.html]How To Ask Questions The Smart Way[/url]
Knight Captain
Posts: 1219
Joined: Thu May 14, 2020 1:34 pm

Re: goto in Usecode C?

Post 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.
Knight Captain
Posts: 1219
Joined: Thu May 14, 2020 1:34 pm

Re: goto in Usecode C?

Post 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. :D
Knight Captain
Posts: 1219
Joined: Thu May 14, 2020 1:34 pm

Re: goto in Usecode C?

Post 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/
marzo
Site Admin
Posts: 1925
Joined: Thu May 14, 2020 1:34 pm

Re: goto in Usecode C?

Post 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.
------
Marzo Sette Torres Junior
aka Geometrodynamic Dragon
[url=http://www.catb.org/~esr/faqs/smart-questions.html]How To Ask Questions The Smart Way[/url]
Knight Captain
Posts: 1219
Joined: Thu May 14, 2020 1:34 pm

Re: goto in Usecode C?

Post 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?
Knight Captain
Posts: 1219
Joined: Thu May 14, 2020 1:34 pm

Re: goto in Usecode C?

Post 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.
Locked