Monday, October 26, 2020

#1 2020-09-05 01:39:48 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 416

Objective-C to ASOC ex4.

After checking Shane's code for the framework "GameplayKit"

I made Apple's example in Objective-C to ASOC.

Applescript:

use framework "Foundation"
use framework "GameplayKit"
use scripting additions

(** Objective-C
* Reference: https://developer.apple.com/documentation/gameplaykit/gkgaussiandistribution?language=objc
* GKRandomSource *random = [[GKRandomSource alloc] init];
* GKRandomDistribution *dice3d6;
* dice3d6 = [[GKGaussianDistribution alloc]
* initWithRandomSource:random lowestValue:3 highestValue:18];
* // Roll the dice...
* int diceRoll = [dice3d6 nextInt];
*)


set randomSource to current application's GKRandomSource's alloc()'s init()
set dice3d6 to current application's GKGaussianDistribution's alloc()'s initWithRandomSource:randomSource lowestValue:3 highestValue:18

return (dice3d6's nextInt()) as integer

Applescript:

set randomSource to current application's GKRandomSource's alloc()'s init()
set shuffled to current application's GKShuffledDistribution's alloc()'s initWithRandomSource:randomSource lowestValue:1 highestValue:6

-- return (shuffled's nextInt()) as integer

-- Log the output of 100
set shuffleDist to current application's GKShuffledDistribution's d6
repeat with i from 1 to 100
   log {shuffleDist's nextInt() as integer}
end repeat

Applescript:

-- Reference: https://developer.apple.com/documentation/gameplaykit/gkrandomsource/1501128-arraybyshufflingobjectsinarray
-- Returns an array whose contents are the same as those of the specified array, but in a random order determined by the random source.
set theArray to {1,2, 3, 4, 5, 6, 7, 8, 9}
set theArray to current application's NSArray's arrayWithArray:theArray
set randomSource to current application's GKRandomSource's alloc()'s init()
set shuffled to randomSource's arrayByShufflingObjectsInArray:theArray
return shuffled as list

Last edited by Fredrik71 (2020-10-06 03:08:53 pm)


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#2 2020-10-06 10:06:59 am

robertfern
Member
Registered: 2011-11-29
Posts: 76

Re: Objective-C to ASOC ex4.

Fredrik,

is there a reason you are using text instead of integers in the line:

Applescript:

initWithRandomSource:randomSource lowestValue:"3" highestValue:"18"

upon reading the documentation for GameplayKit, it seems to be expecting numbers like so

Applescript:

initWithRandomSource:randomSource lowestValue:3 highestValue:18

Just wondering

Offline

 

#3 2020-10-06 03:07:01 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 416

Re: Objective-C to ASOC ex4.

No reason, you are right I will change it...


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#4 2020-10-07 01:02:06 pm

robertfern
Member
Registered: 2011-11-29
Posts: 76

Re: Objective-C to ASOC ex4.

I'm currently using

Applescript:

set randomSource to current application's GKRandomSource's alloc()'s init()
set randItem to current application's GKRandomDistribution's alloc()'s initWithRandomSource:randomSource lowestValue:1 highestValue:c

(randItem's nextInt()) as integer

I would like to use

func nextInt(upperBound: Int) -> Int

I dont know how to write it correctly in AppleScriptObjC


** EDIT

I figured it out below

Applescript:

(randItem's nextIntWithUpperBound:i) as integer

Last edited by robertfern (2020-10-07 01:26:29 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)