Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to Game programming

Optimization: Best way to if/elseif 100 items?

All about the StoneScript

Optimization: Best way to if/elseif 100 items?

Postby Selzier » 19 Mar 2012, 21:21

The player in my game shoots a ray out and will hit maybe around 100 different objects in a single scene. Right now I have a if/elseif set up that is going through about 5 items like so...

Code: Select all
local sSensor = object.getModelName ( hHitSensorObject )

if ( sSensor == "door" )
then
    object.sendEvent ( hHitSensorObject, "AI_Door", "onActivateDoor" )               
elseif ( sSensor == "button" )
then
    object.sendEvent ( hHitSensorObject, "AI_Button", "onTouch" )
end


But I do not think using if/elseif is the right thing to do here, my list will be getting very large with 100 items. What is a good way to maybe send the sSensor off to another function without having to run through a if/elseif?
Last edited by Selzier on 19 Mar 2012, 22:13, edited 1 time in total.
Selzier
Platinum Boarder
Platinum Boarder
 
Posts: 517
Location: Boise

Re: Optimization: Best way to if/elseif 100 items?

Postby psychicsoftware » 19 Mar 2012, 21:35

If your other objects are all to be handled similarly to that, then you could give their AIs all a handler named "onDefaultAction" and find the AI of hHitSensorObject using object.getAIModelNameAt ( hObject, nIndex )
User avatar
psychicsoftware
Platinum Boarder
Platinum Boarder
 
Posts: 344
Location: Galway, Ireland

Re: Optimization: Best way to if/elseif 100 items?

Postby Scribe » 19 Mar 2012, 22:05

Selzier wrote:The player is my game shoots a ray out and will hit maybe around 100 different objects in a single scene. Right now I have a if/elseif set up that is going through about 5 items like so...

Code: Select all
local sSensor = object.getModelName ( hHitSensorObject )

if ( sSensor == "door" )
then
    object.sendEvent ( hHitSensorObject, "AI_Door", "onActivateDoor" )               
elseif ( sSensor == "button" )
then
    object.sendEvent ( hHitSensorObject, "AI_Button", "onTouch" )
end


But I do not think using if/elseif is the right thing to do here, my list will be getting very large with 100 items. What is a good way to maybe send the sSensor off to another function without having to run through a if/elseif?


I can think of four more ideal scenarios, three of which can be done in ShiVa and here's the one I recommend by far for speed and organisation:

Code: Select all
local sSensor = object.getModelName ( hHitSensorObject );

local vIndex = string.findFirst ( sSensor , ",", 0 );

local sAIName = string.getSubString ( sSensor , 0, vIndex );
local sHandlerName = string.getSubString ( sSensor , vIndex+1, string.getLength(sSensor)-1-vIndex );

object.sendEvent ( hHitSensorObject, sAIName, sHandlerName );


I appologise if some of my index offsets are off I haven't tested this, you then name your sensors like so: "AI_Door,onActivateDoor".

Now you don't have to perform a single check, enter a load of code or query multiple objects (if it doesn't accept commas in the name use something else).
ShiVa 3D 1.9.1 | UAT 1.3.1
VS 2010 | Xcode 4.1 | iOS 5.0.1
PC: OS Windows 7 x64 | CPU Phenom II X6 | Memory 16GB | GFX Geforce 480GTX
MacMini: OS OSX 10.7 | CPU i7 3.4ghz | Memory 4GB | GFX ATI 6630M
Scribe
Gold Boarder
Gold Boarder
 
Posts: 286
Location: UK

Re: Optimization: Best way to if/elseif 100 items?

Postby Selzier » 20 Mar 2012, 01:31

Thanks to psychicsoftware and Scribe-

I will end up using a combination of both answers and seems to work great- Thanks!
Selzier
Platinum Boarder
Platinum Boarder
 
Posts: 517
Location: Boise


Return to Game programming