Help with function

MerryCo
06 Dec 2006, 19:06
I have the following function:

define function <RandomObjectName>
if ( $numberparameters$ > 0 ) then {
set numeric <counter; 0>
repeat until ( %counter% = 1 ) {
set numeric <num; $rand(1; 1000)$>
if exists <$parameter(1)$%num%> then set numeric <counter; 0> else set numeric <counter; 1>
}
return <$parameter(1)$%num%>
}
else {
return <ERROR>
}
end define

For some reason, it's not returning anything. Can someone please tell me where I've gone wrong? Thanks!

MaDbRiT
06 Dec 2006, 20:02
Hi MerryCo

What is your function supposed to do? I'm assuming you are passing a series of object names as parameters to the function and want a random name (from those passed) returned - have I got that right?

As it stands it doesn't make a lot of sense to me.

Al

MerryCo
06 Dec 2006, 20:14
Yes, that's correct. I want the ability to spawn objects in game. Here's a little more -- it might give you a better idea:

command <~createobject #object#> {
if exists <#object#> then {
set string <newobjectname; $RandomObjectName(#object#)$>
clone <#object#; #newobjectname#>
give <#newobjectname#>
msg <Object |b#newobjectname#|xb has been created and placed into your inventory. _
Use |b~renameobject|xb to rename it.>
}
else {
msg <That object does not exist.>
}
}


Now, running checks, etc. on the function, I see that it's generating the name just fine and dandy. Just not passing it back.

Freak
06 Dec 2006, 20:15
Quest doesn't handle returns within braces correctly.

MerryCo
06 Dec 2006, 20:41
Is there any other way to return a value from a function in anything other than braces?

MaDbRiT
06 Dec 2006, 21:01
Hi Merry

Just re-write your function a bit, like this


define function <RandomObjectName>

set string <RetVal;ERROR>
if ( $numberparameters$ > 0 ) then {
set numeric <counter; 0>
repeat until ( %counter% = 1 ) {
set numeric <num; $rand(1; 1000)$>
if exists <$parameter(1)$%num%> then set numeric <counter; 0> else set numeric <counter; 1>
}
set <RetVal;$parameter(1)$%num%>
}
return <#RetVal#>

end define


I've tried this and it works...

Be aware, your cloned item will have the original object's name as an alias - so you might want to change that - and also, although the function returns 'error' this is never used because Quest's inbuilt 'can't clone a non-existing item' will kick in.

Al

MerryCo
06 Dec 2006, 21:05
Thanks much!

And isn't it okay, if the cloned object shares the alias? This is for a multiuser game, and there can be more than one sword, for instance.

Thanks again!!!

MaDbRiT
06 Dec 2006, 21:08
Hi Merry

Sure it's OK to share the alias - but if you are going to 'rename' the object (as your code suggests) you might want to at least report the actual generated name to the creator - and avoid a lot of disambiguation of similar items.

Al

Alex
06 Dec 2006, 21:49
Hmm... returns within braces should be OK... I'll take a look into this.

Freak
06 Dec 2006, 22:09
I found this out while working on Geas. Quest will convert that code to something like:

define function <RandomObjectName>
if ( $numberparameters$ > 0 ) then do <!intproc1> else do <!intproc2>
end define

define procedure <!intproc2>
return <ERROR>
end define

So !intproc2 returns ERROR or the new object name; RandomObjectName returns nothing.

Alex
15 Dec 2006, 16:38
I've reproduced this and I've now fixed it for Quest 4.0 Beta 3 where the ASL version is set to 392 or above.

Quest was losing the context of where blocks within braces were being run, which meant that the "return" value was being lost. This would also have caused some odd problems in QuestNet Server games if you had script within nested blocks - you may end up with text being printed to the admin window instead of to the correct player.

Freak
15 Dec 2006, 19:42
What sorts of changes did you have to make?

I could modify Geas so that all procedures propogate their return values upwards, or that so just procedures with name "!intproc..." do so; which (if either) is correct?

Do the rest of the changes just affect multiplayer mode (the "with" command?) or is there any change to single player?