DFsVariable *DFsScript::FindVariable(const char *name) { DFsVariable *var; DFsScript *current = this; while(current) { // check this script if ((var = current->VariableForName(name))) return var; current = current->parent; // try the parent of this one } return NULL; // no variable }
DFsVariable *DFsScript::FindVariable(const char *name, DFsScript *GlobalScript) { DFsVariable *var; DFsScript *current = this; while(current) { // check this script if ((var = current->VariableForName(name))) return var; // Since the global script cannot be serialized, we cannot store a pointer to it, because this cannot be safely restored during deserialization. // To compensate we need to check the relationship explicitly here. if (current->parent == nullptr && current != GlobalScript) current = GlobalScript; else current = current->parent; // try the parent of this one } return NULL; // no variable }
void FParser::spec_script() { int scriptnum; int datasize; DFsScript *newscript; scriptnum = 0; if(!Section) { script_error("need seperators for newscript\n"); return; } // presume that the first token is "newscript" if(NumTokens < 2) { script_error("need newscript number\n"); return; } svalue_t result; EvaluateExpression(result, 1, NumTokens-1); scriptnum = intvalue(result); if(scriptnum < 0) { script_error("invalid newscript number\n"); return; } newscript = Create<DFsScript>(); // add to scripts list of parent Script->children[scriptnum] = newscript; GC::WriteBarrier(Script, newscript); // copy newscript data // workout newscript size: -2 to ignore { and } datasize = (Section->end_index - Section->start_index - 2); // alloc extra 10 for safety newscript->data = (char *)malloc(datasize+10); // copy from parent newscript (levelscript) // ignore first char which is { memcpy(newscript->data, Script->SectionStart(Section) + 1, datasize); // tack on a 0 to end the string newscript->data[datasize] = '\0'; newscript->scriptnum = scriptnum; newscript->parent = Script; // remember parent // preprocess the newscript now newscript->Preprocess(Level); // we dont want to run the newscript, only add it // jump past the newscript in parsing Rover = Script->SectionEnd(Section) + 1; }