Call a function

Avantar
I hope you guys can help me out:

I would like to call a function that is made up of a string and a number:
Say I have functions draw1, draw2, draw3....
I would randomly like to call one of the 'draw' functions.
So now I have GetRandomInt(1,3) giving me a number from 1 to 3
Now I would like to call the function: "draw"+(number generated)

Is that possible or is this approach just stupid?

The Pixie
Try:

result = Eval("draw" + GetRandomInt(1,3))

I have a feeling that Quest will get upset if you do not assign the result of Eval to something, or if the expression does not return a value (so you may need to have the draw functions return something), so some playing around may be required.

Alex
I would hard-code the 3 cases using an "if" or "switch". Using an eval here is icky.

Avantar
To Alex:

I was afraid the suggestion might be using 'case'. The random number is not just for functions starting with draw, but also a lot of other functions.
draw1 - draw100 will be representing cards with specific events that happen when drawn, but a total different deck might be drawn depended on location ans/or gender.

I can not imagine doing 2200 + case statements across 22 locations? But if that is the way to go - I guess I will just need to do it. :?

This brings me to another question - Will quest be OK with about 1000+ functions hanging about?

To Pixie:
I tried something similar, but I am sure how to call the 'result' - I am a noob when it comes to parameters and coding in general.

Thank you for the quick replies Alex and Pixie!

The Pixie
Try something like this:

game.parameter1 = "some value"
game.parameter2 = 42
result = Eval("draw" + GetRandomInt(1,3) + "(game.parameter1, game.parameter2)")

Alex
Do you really need thousands of functions - surely there's a lot of commonality between them? (If you're copying and pasting code between all these functions, you're doing something wrong)

I'm sure there's a way of rearranging what you're doing so you can just pass a random number into one function (or one of a small handful).

Avantar
Thank you for the reply Pixie and Alex.

To Alex:
So here it is in full -
I have 22 rooms and 1 to 4 players that can each be male or female. (This will be a board game see?)
Each player have a dice they can roll - that will move them to the various rooms. They will encounter random things landing on these rooms. Each room has got its own two deck of cards consisting of a 100 cards each. If it comes to being an 'adventure card' or 'chance' card is dependent on a few factors - this is all sorted.

My problem lies in drawing random cards for each room. Each card will represent something like: Archery competition, making it through a gauntlet, monsters, a game of chance...blah, blah
Therefore the cards will be functions having you do this stuff....unless there is another way. But yes, there will be a lot of stuff happening.
Doing a library...even if I wanted too - is above me.

Not sure if there is another way to approach this.

Avantar
The Pixie wrote:Try something like this:

game.parameter1 = "some value"
game.parameter2 = 42
result = Eval("draw" + GetRandomInt(1,3) + "(game.parameter1, game.parameter2)")


I have done a test module attached below, just to test drawing a random card.
The aim would be to get either of these messages: "You have drawn card 1/2/3"

So I have have put my understanding of what you wrote in this little module and as you can see my level of understanding is not a lot.
I've used a command 'draw card' to try and implement what you gave me - lol Just to try and figure how Eval works and of course the error:
Error running script: Error evaluating expression 'Eval("Card" + GetRandomInt(1,3) + "(game.parameter1, game.parameter2)")': Eval function expected dictionary parameter but was passed 'null'

HegemonKhan
HK edit: sorry, I was in a hurry when I rush-posted this post... the 'random_value_x = GetRandomInt (0,100)' code line would actually be outside of the 'foreach' scripting, and how to actually get the 'connect comparison' would be done a bit differently, I'd need to look up at how to match it properly with object list order item (it's been awhile since I've done this, so I forgotten how).

-----------------

you can do a 'foreach' of an Object List of your rooms, and then for its scripting:

// random_value_x = GetRandomInt (0,100)
// if (random_value_x = your list order item (room)
// player.parent = your list item (room)

----------

HK edit:

Pixie's Magic Code~Library (I guess you can just find it's scripting as part of his~her Simple Combat Library now with the moving of references to their new location, docs, as I couldn't find Pixie's Demo and Library files of this magic coding anywhere now in the new resources location) does this same code method for applying (for example) double damage with using opposing spell elementals (fire vs water, air vs earth, etc).

Avantar
Thank you HK!

That sounds a bit more to my understanding.
From all these replies, I will sort something.
Thanks again.

jaynabonne
To add my two cents: I wouldn't use functions. I'd use script attributes on the various rooms. You can easily call a script by expression using "do". For example, if you have a room like this:

<object name="SomeRoom">
<draw1 type="script">
// some script
</draw1>
<draw2 type="script">
// some script
</draw2>
<draw3 type="script">
// some script
</draw3>
</object>

then you can invoke a random function as:

do(SomeRoom, "draw" + GetRandomInt(1,3))

This even allows you to have commonality among the rooms, as you can then key it off the current room:

do(game.pov.parent, "draw" + GetRandomInt(1,3))

will invoke the function in the room where the player currently is.

If you need to pass parameters, that takes a small bit more coding, but it's not too bad.

jaynabonne
And just for completeness, here's your game redone with script attributes:

<!--Saved by Quest 5.4.4873.16527-->
<asl version="540">
<include ref="English.aslx" />
<include ref="Core.aslx" />
<game name="Card Draw Test">
<gameid>64becbd5-67f2-483c-b488-a21a16420910</gameid>
<version>1.0</version>
<firstpublished>2014</firstpublished>
</game>
<object name="room">
<inherit name="editor_room" />
<object name="player">
<inherit name="editor_object" />
<inherit name="editor_player" />
</object>
<Card1 type="script">
msg ("You have drawn card 1")
</Card1>
<Card2 type="script">
msg ("You have drawn card 2")
</Card2>
<Card3 type="script">
msg ("You have drawn card 3")
</Card3>
</object>
<command name="draw card">
<pattern>draw card</pattern>
<script>
do(game.pov.parent, "Card" + GetRandomInt(1,3))
</script>
</command>
</asl>

Avantar
Now there is an idea! Thank you Jay!

I wish it was sooner; since I've done a lot of work on it last night. I have basically went and decided on two global packs - had two random numbers generated dependent on your level and so forth, store the two variables and then always draw card 1 from the pack, matched the card with the stored variable and if it differs go to the next card until it finds the correct one.

This is limiting me in what I wanted to achieve per room. I am surely going to ditch all my hard work for your suggestion.
Thank you again.

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums